代码示例
#include<iostream>
#include<thread>
using namespace std;
class obj{
public:
obj(int i){cout<<"构造函数"<<endl;}
obj(const obj&){cout<<"拷贝构造函数"<<endl;}
void operator()(){
cout<<"thread start"<<endl;
cout<<"thread end"<<endl;
}
~obj(){cout<<"obj的析构函数执行"<<endl;}
};
void func(){
cout<<"thread start1"<<endl;
cout<<"thread start2"<<endl;
cout<<"thread start3"<<endl;
cout<<"thread end"<<endl;
}
int main()
{
obj a(6);
auto fun = [](){
cout<<"thread start"<<endl;
};
thread myThread(fun);
myThread.join();
cout<<"main thread end1"<<endl;
cout<<"main thread end2"<<endl;
cout<<"main thread end3"<<endl;
}
编译运行
g++ -std=c++11 -o thread thread.cpp -lpthread
