#include <iostream> // std::cout
#include <thread> // std::thread
#include <mutex> // std::mutex, std::unique_lock
#include <condition_variable> // std::condition_variable
std::mutex mtx; // 全局互斥锁.
std::condition_variable cv; // 全局条件变量.
bool ready = false; // 全局标志位.
void do_print_id(int id)
{
std::unique_lock <std::mutex> lck(mtx);
std::cout << "begin thread " << id << '\n';
while (!ready)
{
/*
如果标志位不为 true, 则等待...
当前线程被阻塞, 当全局标志位变为 true 之后, 线程被唤醒, 继续往下执行打印线程编号id.
*/
std::cout << "wait: " << id << std::endl;
cv.wait(lck);
}
std::cout << "thread " << id << '\n';
}
void go()
{
std::unique_lock <std::mutex> lck(mtx);
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
ready = true; // 设置全局标志位为 true.
//cv.notify_all(); // 唤醒所有线程.
cv.notify_one();//
}
int main()
{
std::thread threads[10];
// spawn 10 threads:
for (int i = 0; i < 10; ++i)
threads[i] = std::thread(do_print_id, i);
std::cout << "10 threads ready to race...\n";
getchar();
go(); // go!
for (auto& th : threads)
th.join();
getchar();
return 0;
}