1 使用仿函数创建线程:
1 #include<iostream>
2 #include<thread>
3
4 using namespace std;
5
6 class test
7 {
8 public:
9 void operator()()// 不能带参数 仿函数
10 {
11 cout<<"子线程执行到了这里"<<endl;
12 }
13 };
14
15 int main()
16 {
17 test tt;
18 thread t1(tt);//tt是一个可以作为函数的调用的对象
19 t1.join();
20 return 0;
21 }
运行结果:
子线程执行到了这里
2 用lambda创建线程
#include<iostream>
#include<thread>
using namespace std;
auto mylambda = []{
cout<<"线程开始执行"<<endl;
cout<<"线程结束执行"<<endl;
};
int main()
{
thread t(mylambda);
t.join();
cout<<"process end"<<endl;
return 0;
}
运行结果:
线程开始执行
线程结束执行
process end