c++ lambda表达式获取线程ID以及线程等待
#include <thread>
#include<chrono>
thread th1([]()
{
this_thread::sleep_for(chrono::seconds(3));
cout << this_thread::get_id() << endl;
});
thread th2([]()
{
this_thread::sleep_for(chrono::seconds(5));
cout << this_thread::get_id() << endl;
});
thread th3([]()
{
this_thread::sleep_for(chrono::seconds(3));//等待3秒 显示ID
this_thread::yield();//让CPU先执行其他线程 空闲时再执行自己
cout << this_thread::get_id() << endl;
//this_thread::sleep_until();//sleep_until 一直等待到某一时刻
});
thread th4([]()
{
this_thread::sleep_for(chrono::seconds(5));//等待5秒 显示线程ID
cout << this_thread::get_id() << endl;
});
更多资料 · 微信公众号搜索【CTO Plus】关注后,获取更多,我们一起学习交流。
关于公众号的描述访问如下链接