篮之新喜 发表于 2023-9-28 15:32:42

c++并发编程实战-第4章 并发操作的同步

等待事件或等待其他条件

坐车案例

想象一种情况:假设晚上坐车外出,如何才能确保不坐过站又能使自己最轻松?
方法一:不睡觉,时刻关注自己的位置

1 #include2 #include3 #include4 using namespace std; 56 mutex _mtx; 7 bool bFlag = false; 8 void wait_for_flag() 9 {10   auto startTime = chrono::steady_clock::now();11   while (1)12   {13         unique_lock lock(_mtx);14         if (bFlag)15         {16             auto endTime = chrono::steady_clock::now();17             double dCount = chrono::duration(endTime - startTime).count();18             cout
页: [1]
查看完整版本: c++并发编程实战-第4章 并发操作的同步