轉載地址:https://blog.csdn.net/wuhui20091515/article/details/52531202
例子1
#include <iostream>
#include <thread>
using namespce std;
void hello()
{
cout << "hello concurent world!";
}
int main (int argc, char * argv[])
{
thread t(hello);
t.join();
return 0;
}
這個小例子直接用make編譯是無法通過的。會報錯
/tmp/ccYB66pt.o:在函數‘std::thread::thread<void (&)()>(void (&)())’中:
1-1.cpp:(.text._ZNSt6threadC2IRFvvEJEEEOT_DpOT0_[_ZNSt6threadC5IRFvvEJEEEOT_DpOT0_]+0x21):對‘pthread_create’未定義的引用
collect2: 錯誤:ld 返回 1
<builtin>: recipe for target '1-1' failed
make: *** [1-1] Error 1
解決方法是在編譯的時候加上-lpthread參數。這個類用到posix實現的線程了。
g++ -o test test.cpp -lpthread
./test
結果輸出 hello concurent world!