C++11/std::thread – 可作为线程函数的几种方式总结
原创文章,作者:StubbornHuang,如若转载,请注明出处:《C++11/std::thread – 可作为线程函数的几种方式总结》https://www.stubbornhuang.com/775/
1 使用普通函数作为线程函数
代码示例:
#include <iostream>
#include <thread>
void ThreadFunction()
{
std::cout<< "线程函数被启动" << std::endl;
}
int main()
{
std::thread thread(ThreadFunction);
thread.join();
getchar();
return 0;
}
2 使用类的成员函数作为线程函数
代码示例:
#include <iostream>
#include <thread>
class ThreadFunc
{
public:
void ThreadFunction()
{
std::cout << "线程函数被启动" << std::endl;
}
};
int main()
{
ThreadFunc m_ThreadFunc;
std::thread thread(&ThreadFunc::ThreadFunction,&m_ThreadFunc);
thread.join();
getchar();
return 0;
}
3 Lambda表达式/匿名函数作为线程函数
代码示例:
#include <iostream>
#include <thread>
int main()
{
std::thread thread{ [] {std::cout << "线程函数被启动" << std::endl; } };
thread.join();
getchar();
return 0;
}
未完待续。
当前分类随机文章推荐
- C++ - 线程安全的std::cout
- C++ - 使用C++标准库过滤Windows文件名中的非法字符
- C++ - 只有在Debug模式下才使用std::cout输出调试日志,Release发布版本不输出调试日志
- C++ 11 - final关键字简要介绍
- C++11 - std::function简要介绍以及可包装函数的几种形式总结
- C++ - vector存储动态指针时正确释放内存
- C++11/std::thread - 可作为线程函数的几种方式总结
- C++ - queue存储动态指针时正确释放内存
- C++ 回调函数
- C++11/std::thread - 线程的基本用法
全站随机文章推荐
- 资源分享 - FFmpeg从入门到精通(刘岐赵文杰著)PDF下载
- Modern OpenGL从零开始 - 多个帧缓存Framebuffer绘制到同一个铺满屏幕四边形Quad上
- PlaySound 播放内存中的音频数据
- 我的开源项目 - 各种搜索引擎收录查询接口(Google/百度/必应/360/搜狗......)
- 旋转矩阵与四元数的转换
- Modern OpenGL - GLSL着色语言4:GLSL中的数据存储限制符
- 三维空间中的旋转变换与旋转矩阵
- 资源分享 - 深度学习框架Pytorch入门与实践(陈云著)PDF下载
- WordPress - 后台登录成功/失败发送邮件给网站管理员
- 资源分享 - Qt5开发及实例(第3版)陆文周主编高清PDF下载