1. 编辑源程序
vim hello.cpp
#include <iostream>
#include <thread>
void func(int x)
{
std::cout << x << " new thread\n";
}
int main()
{
std::cout << "hello world\n";
std::thread t(func, 8);
t.join();
std::cout << "end thread\n";
return 0;
}
2. 编译程序
g++ -std=c++0x -pthread hello.cpp -o hello
3. 运行程序
./hello
注意编译的时候所带的参数,若不带-pthread,编译可以通过,运行会出现以下问题。
terminate called after throwing an instance of 'std::system_error'
what(): Operation not permitted
Aborted (core dumped)