官方例子
// 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; }
Original: https://www.cnblogs.com/nanqiang/p/13065881.html
Author: cicero
Title: c++11 thread
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/535378/
转载文章受原作者版权保护。转载请注明原作者出处!