从C++11开始,C++标准库已经支持了线程库了,其实在底层,仍旧使用的是平台相关的线程API
有了std::thread之后,我们就不用在不同的平台使用不同的API了,比如Unix平台使用pthread, windows平台使用WinSDK的CreateThread了,接口使用去掉了平台差异性,使得项目开发具有更好的移植性
#include
//1. 直接使用函数
void thread1_process(int code)
{
std::cout << "code: " << code << std::endl;
}
int code = 0; // get code from somewhere
std::thread thread1(thread1_process, code);
thread1.join(); // 等待线程结束
//tread1.detach(); // 将线程脱离thread1的管理
/**********************************************************************/
//2. 使用类成员函数
struct Task
{
void doSomething(int task_type) {
std::cout << "task_type: " << task_type<< std::endl;
// TODO
}
};
Task task1;
std::thread thread2(&Task::doSomething, &task1, 1);
thread2.join(); // 等待线程结束
//tread2.detach(); // 将线程脱离thread2的管理
/***********************************************************************/
//3. 使用std::bind
Task task2;
std::thread thread3(std::bind(&Task::doSomething, &task2, 2));
thread3.join(); // 等待线程结束
//tread3.detach(); // 将线程脱离thread3的管理
/***********************************************************************/
//4. 使用lambda表达式
std::thread thread4([](){
std::cout << "lambda thread called." <// 等待线程结束
//tread4.detach(); // 将线程脱离thread4的管理
由于Unix平台std::thread底层实现仍旧是pthread, 所以需要增加编译参数 -lpthread
比如一下代码:
void task1(); //具体实现这里忽略
int main(int argc, char **argv)
{
std::thread thr1(task1);
// std::thread thr2 = thr1; //将会编译报错
thr1.join();
return 0;
}
其中的std::terminate()就是用来终止进程的调用。由于std::thread创建后默认是joinable的, 所以需要调用join()或者detach() 来使线程变成非joinable的状态, 具体使用join()还是detach() 取决于实际需求, 如果需要等待线程完成才能继续,那就使用join()来等待, 如果需要立刻返回继续其他操作, 那就调用detach()来脱离对线程的管理, 两者必须选一个