首页 > 编程 > C++ > 正文

c++之std::thread多线程的使用和获取pid/tid

2019-11-08 03:24:41
字体:
来源:转载
供稿:网友
// 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;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选