G++下如何編譯執行C++11多執行緒程式碼
安裝G++的指令:
1. 編輯源程式:sudo apt-get install g++
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)