C++11的新特性std::thread的创建方式主要有:函数指针,类或结构体,lambda表达式。下面将会附上这三种创建线程方式的code。
首先第一个是通过函数指针的方式来创建:
不带参数:
#include <iostream>
#include <thread>
using namespace std;
void A() {
cout << "this is A thread" << endl;
}
int main()
{
thread t(A);
t.join();
cout << "this is main thread" << endl;
return 0;
}
带参数:
#include <iostream>
#include <thread>
using namespace std;
void A(int a,int b) {
cout << "this is A thread" << endl;
}
int main()
{
thread t(A, 2, 3);
t.join();
cout << "this is main thread" << endl;
return 0;
}
第二种是通过类和结构体来创建,那么对于类来说