c++11 thread
官方例子
// thread example #include// std::cout #include // std::thread #include using namespace std; void foo() { // do stuff... for(int i = 0 ; i < 10 ; ++i){ cout << i << endl; sleep(1); } } void bar(int x) { // do stuff... for(int i = 0 ; i < 10 ; ++i){ cout << i << endl; sleep(1); } } int main() { std::thread *first = new thread(bar,2); // spawn new thread that calls foo() std::thread second (bar,0); // spawn new thread that calls bar(0) std::cout << "main, foo and bar now execute concurrently...\n"; // synchronize threads: first->join(); // pauses until first finishes second.join(); // pauses until second finishes delete first; std::cout << "foo and bar completed.\n"; return 0; }