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::string输出双引号到字符串
- C++ 回调函数
- C++ - std::map - 存储动态指针时正确释放内存
- C++11 - 使用std::thread::join()/std::thread::detach()方法需要注意的点
- C++ - 使用C++标准库过滤Windows文件名中的非法字符
- C++ - 控制台程序在控制台窗口可变参数格式化带颜色输出日志信息
- C++ Map中存储动态指针时正确释放内存
- C++ - int转string方法总结
- C++11 - 基于无锁队列的单生产者单消费者模型
- C++ - 字节数组byte与int的相互转换
全站随机文章推荐
- Duilib - RichEdit作为日志输出控件,更新日志内容后并自动跳到最后一行
- WordPress - Windows使用PhpStudy本地部署WordPress
- Python3爬虫 - 下载反盗链图片的方式
- UnrealEngine4 - C++层打印信息到屏幕
- UnrealEngine4 - 关于UObject被自动GC垃圾回收的巨坑
- 资源分享 - 深度学习实战(杨云杜飞 清华大学出版社)高清pdf下载
- WordPress - 在编辑文章发布时弹出是否发布提醒框
- 我的项目 - Windows/Linux动态库加载类
- 资源分享 - Ray Tracing - The Rest of Your Life英文高清PDF下载
- OpenCV - 静态图片人脸检测和摄像头人脸检测