线程的创建
#include <iostream>
#include <thread> //头文件
using namespace std;
void greeting(){
cout << "Hello multithread!"<<endl;
return;
}
int main(){
thread t{greeting}; //列表初始化
t.join(); //确保主线程在子线程退出之后才退出
return 0;
}
编译执行后的输出
# toto @ tt in ~/Cpp [18:00:53] C:1
$ g++ ./HelloMultithread.cpp -o HelloMultithread --std=c++11 -pthread
(base)
# toto @ tt in ~/Cpp [18:01:12]
$ ./HelloMultithread
Hello multithread!