Hello, there is an abstract task: three asynchronous threads are created, the first - the manager - waits 3 seconds, then uses the condition variable to start outputting two threads waiting for a signal:
namespace mutexes { std::mutex m; std::condition_variable cond; std::atomic<bool> at = ATOMIC_VAR_INIT(0); void thread_1() { while(true) { static int i = 0; std::cout<<"thread 1\n"; std::this_thread::sleep_for (std::chrono::seconds(1)); if(i==5) break; if(i++==2) { at = 1; cond.notify_all(); } } at = 0; } void thread_2() { while(true) { std::unique_lock<std::mutex> lk(m); cond.wait(lk,[]{ return at == 1;}); std::cout<<"thread 2\n"; std::this_thread::sleep_for (std::chrono::milliseconds(500)); } } void thread_3() { while(true) { std::unique_lock<std::mutex> lk(m); cond.wait(lk,[]{ return at == 1;}); std::cout<<"thread 3\n"; std::this_thread::sleep_for (std::chrono::milliseconds(500)); } } } //namespace mutexes int main() { //запускаем все три потока асинхронно std::thread([]{ std::thread thrd1(&mutexes::thread_1); std::thread thrd2(&mutexes::thread_2); std::thread thrd3(&mutexes::thread_3); thrd1.detach(); thrd2.detach(); thrd3.detach(); std::this_thread::sleep_for (std::chrono::seconds(6)); }).join(); } as a result, the output goes only from the second (thread_2) ... I cannot understand why, apparently the dead lock of the mutex ... help to make the output to the console go from both threads