C++11/std::thread – 可作为线程函数的几种方式总结
本文作者:StubbornHuang
版权声明:本文为站长原创文章,如果转载请注明原文链接!
原文标题:C++11/std::thread – 可作为线程函数的几种方式总结
原文链接:https://www.stubbornhuang.com/775/
发布于:2020年04月01日 10:53:57
修改于:2020年04月01日 10:53:57

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替换字符串中所有指定的子字符串 阅读2140次,点赞1次
- C++ - 使用ffmpeg读取视频旋转角度并使用OpenCV根据旋转角度对视频进行旋转复原 阅读1708次,点赞0次
- C++ - 获取当前进程内存使用情况 阅读8346次,点赞10次
- C++ - RAII机制 阅读242次,点赞0次
- CMake - 设置Debug或者Release编译模式 阅读2124次,点赞0次
- C++ - 一文搞懂std::future、std::promise、std::packaged_task、std::async的使用和相互区别 阅读178次,点赞0次
- C++ - 只有在Debug模式下才使用std::cout输出调试日志,Release发布版本不输出调试日志 阅读4160次,点赞0次
- C++ - 使用宏区分不同系统平台、不同编译器、不同编译模式等编译期宏使用总结 阅读1207次,点赞0次
- C++ - 字节数组byte[]或者unsigned char[]与long的相互转换 阅读798次,点赞0次
- C++ - 函数返回多个返回值的方法总结 阅读1693次,点赞0次
全站随机文章推荐
- 计算机图形学 - Flat Shading、Gouraud Shading、Phong Shading的区别 阅读839次,点赞0次
- 资源分享 - Game AI Pro 360 - Guide to Architecture 英文高清PDF下载 阅读2123次,点赞0次
- 资源分享 – Fluid Simulation for Computer Graphics, Second Edition英文高清PDF下载 阅读3931次,点赞0次
- Pytorch - nn.Transformer、nn.TransformerEncoderLayer、nn.TransformerEncoder、nn.TransformerDecoder、nn.TransformerDecoder参数详解 阅读1922次,点赞1次
- 深度学习 - CTC算法原理详解 阅读545次,点赞0次
- WordPress - 防止用户在登录页面重复点击登录按钮造成重复登录 阅读584次,点赞0次
- 资源分享 - Physics Modeling for Game Programmers 英文高清PDF下载 阅读1553次,点赞0次
- 资源分享 - Mathematics for 3D Game Programming and Computer Graphics, Third Edition英文高清PDF下载 阅读2781次,点赞0次
- 资源分享 - Mathematics for Computer Graphics , Second Edition 英文高清PDF下载 阅读1125次,点赞0次
- 资源分享 - 3D游戏与计算机图形学中的数学方法 第3版 , Mathematics for 3D Game Programming and Computer Graphics, Third Edition 中文版PDF下载 阅读1218次,点赞0次
评论
167