线程:CPP lambda表达式与多线程

c++ lambda表达式获取线程ID以及线程等待
#include 
#include
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;
	});




你可能感兴趣的:(线程:CPP lambda表达式与多线程)