上传网站安装教程视频,wordpress大前端主题怎么样,如何开网站呢,金融公司网站方案原标题#xff1a;C11并发编程#xff1a;多线程std:thread一#xff1a;概述C11引入了thread类#xff0c;大大降低了多线程使用的复杂度#xff0c;原先使用多线程只能用系统的API#xff0c;无法解决跨平台问题#xff0c;一套代码平台移植#xff0c;对应多线程代码…原标题C11并发编程多线程std:thread一概述C11引入了thread类大大降低了多线程使用的复杂度原先使用多线程只能用系统的API无法解决跨平台问题一套代码平台移植对应多线程代码也必须要修改。现在在C11中只需使用语言层面的thread可以解决这个问题。所需头文件二构造函数1.默认构造函数thread() noexcept一个空的std::thread执行对象2.初始化构造函数templateexplicit thread(Fn fn, Args... args);创建std::thread执行对象线程调用threadFun函数函数参数为args。void threadFun(int a){cout this is thread fun ! endl;}thread t1(threadFun, 2);3.拷贝构造函数thread(const thread) delete;拷贝构造函数被禁用std::thread对象不可拷贝构造void threadFun(int a){cout this is thread fun ! endl;}int value 2;thread t1(threadFun, std::ref(value));4.Move构造函数thread(thread x)noexcept调用成功原来x不再是std::thread对象void threadFun(int a){cout this is thread fun ! endl;}int value 2;thread t1(threadFun, std::ref(value));thread t2(std::move(t1));t2.join();三成员函数1.get_id()获取线程ID返回类型std::thread::id对象。thread t1(threadFun);thread::id threadId t1.get_id();cout 线程ID threadId endl;//threadId转换成整形值所需头文件ostringstream oss;oss t1.get_id();string strId oss.str();unsigned long long tid stoull(strId);cout 线程ID tid endl;2.join()创建线程执行线程函数调用该函数会阻塞当前线程直到线程执行完join才返回。thread t1(threadFun);t1.join() //阻塞等待3.detach()detach调用之后目标线程就成为了守护线程驻留后台运行与之关联的std::thread对象失去对目标线程的关联无法再通过std::thread对象取得该线程的控制权。4.swap()交换两个线程对象thread t1(threadFun1);thread t2(threadFun2);cout 线程1的ID t1.get_id() endl;cout 线程2的ID t2.get_id() endl;t1.swap(t2);cout 线程1的ID t1.get_id() endl;cout 线程2的ID t2.get_id() endl;5.hardware_concurrency()获得逻辑处理器储量,返回值为int型int coreNum thread::hardware_concurrency();四使用1.创建线程void threadFun1(){cout this is thread fun1 ! endl;}int main(){thread t1(threadFun1);t1.join();getchar();return 1;}2.创建线程传参void threadFun1(int v){cout this is thread fun1 ! endl;cout v endl;}int main(){int value 6;thread t1(threadFun1, value);t1.join();getchar();return 1;}需要注意变量int value 和int v 做变量传递时并不是引用而是对变量做了拷贝所以在传递给int v前int value不能出作用域(释放了内存)join()可以保证int value变量释放内存如果使用detach()可能存在这种情况。3.创建线程引用传参void threadFun1(int v){cout this is thread fun1 ! endl;cout v endl;}int main(){int value 6;thread t1(threadFun1, std::ref(value));t1.join();getchar();return 1;}4.创建建线程线程函数为类成员函数class Object{public:Object(){cout 构造函数 endl;}~Object(){cout 析构函数 endl;}void fun(string info){cout info endl;}};int main(){Object obj;string str 我是一个类的成员函数;thread t1(Object::fun, obj, str);t1.join();getchar();return 1;---------------------作者蜗牛201原文https://blog.csdn.net/woniu211111/article/details/85123281责任编辑