4. 识别线程

识别线程

  • 线程表示类型为std::id可以通过两种方式进行检索

  • 第一种可以通过std::thread 的对象成员函数get_id()来直接获取
  • 第二种是在当前线程中调用std::get_id()
void func(){
	cout<<"print id in son process"<<std::this_thread::get_id()<<endl;
}
int main(){
	std::thread t(fucn);
	cout<<"print id in father process"<<t.get_id()<<endl;
	system("pause");
	return 0;
}

  • std::id既可以排序,也可以作为键值进行标识,满足普通的大小比较,如 a < b , b < c , a < c;
std::thread:: id master_id;
void do_work(){
	if(std::this_thread::gete_id() == master_id){
		dowork1();
	}else{
		dowork2();
	}
}
posted @ 2023-01-03 17:29  ^江流儿^  阅读(38)  评论(0)    收藏  举报