C++11引入了thread类,大大降低了多线程使用的复杂度,原先使用多线程只能用系统的API,无法解决跨平台问题,一套代码平台移植,对应多线程代码也必须要修改。现在在C++11中只需使用语言层面的thread可以解决这个问题。
#include <thread>
#include <iostream>
struct Task
{
void doSomething(int task_type)
{
std::cout<<"task_type:"<<task_type<<std::endl;
}
};
int main()
{
Task task1;
std::thread thread1(&Task::doSomething, &task1, 123);
thread1.join();
return 0;
}
Output:
task_type:123