线程(CPP|线程: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|线程:CPP lambda表达式与多线程)】

    推荐阅读