1.多线程初次使用:
// thread example #include <iostream> #include <thread> using namespace std; void foo() { //do stuff } void bar(int x) { //do stuff } int main() { thread first (foo); //spawn new thread that calls foo() thread second (bar, 0); //spawn new thread that calls bar(0) cout << "main, foo and bar now execute concurrently... "; first.join(); second.join(); cout << "foo and bar completed. "; return 0; }
2.关于g++编译时候需要注意,不同g++版本可能不一样。
g++ thread.cpp -o thread -lpthread -std=c++11