我有一个线程需要阻塞,直到另一个线程发生某些事情。这听起来很典型,我有这个解决方案。
//thread 1
mux.lock();
//send work to another thread
mux.lock(); //will likely block which I want
//thread 2
//get the work sent over from thread 1
//work on it, then
mux.unlock(); //unblock thread 1 - all good
Run Code Online (Sandbox Code Playgroud)
这似乎在 Linux 上运行良好,并且不需要条件变量 - 除了 C++ 标准说在同一线程中两次获取锁是未定义的行为 - 我在线程 1 中这样做。