一:概述
C++11引入了thread类,大大降低了多线程使用的复杂度,原先使用多线程只能用系统的API,无法解决跨平台问题,一套代码平台移植,对应多线程代码也必须要修改。现在在C++11中只需使用语言层面的thread可以解决这个问题。
所需头文件< thread >
二:构造函数
1.默认构造函数
thread() noexcept
一个空的std::thread执行对象
2.初始化构造函数
template
explicit 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&