|
多线程
多线程编程的头文件
<thread>,<mutex>,<atomic>,<condition_variable>,<future>
<thread>
std::thread的构造
thread类的构造函数:
class thread{
thread(); //默认构造函数
thread(std::Function&& f, Args&&... args); //初始化构造函数
thread(thread&& other);
//...
}
<hr/>初始化构造函数:
void fun1(){
cout<<&#34;thread : fun1&#34;<<endl;
}
void fun2(int i){
cout<<&#34;thread : fun2 &#34; << i * i << endl;
}
int main(){
std::thread t1(fun1);
std::thread t2(fun2,9);
t1.join();
t2.join();
return 0;
}

move构造函数:
thread t(fun1);
thread t1(move(t));
assert(t.joinable() == false);
thread& t2 = t1;
t2.join(); // or t1.join();thread不能拷贝赋值,下面两种方式均错误:
thread t(function);
//thread t1 = t; 错误
//thread t1(t); 错误 |
|