// thread example#include <iostream> // std::cout#include <thread> // std::thread#ifdef _WIN32#include <PRocess.h>#define getpid _getpid#else#include <unistd.h>#endifvoid foo(){ // do stuff... int pid = getpid(); std::cout << "pid=" <<pid<<",tid=" << std::this_thread::get_id() << ",first thread foo/n"; }void bar(int x){ // do stuff... int pid = getpid(); std::cout << "pid=" << pid << ",tid=" << std::this_thread::get_id() << ",second thread bar/n";}//c++之std::thread多线程的使用和获取pid/tidint main(){ std::thread first(foo); // spawn new thread that calls foo() std::thread second(bar, 0); // spawn new thread that calls bar(0) int pid = getpid(); std::cout << "pid=" << pid<<",tid=" << std::this_thread::get_id()<<",main, foo and bar now execute concurrently.../n"; // synchronize threads: first.join(); // pauses until first finishes second.join(); // pauses until second finishes std::cout << "foo and bar completed./n"; system("pause"); return 0;}
新闻热点
疑难解答
图片精选