c++ - How to fix race condition for condition variable wait/notify -
answer question wrong has chance deadlock. condition variable - wait/notify race condition i found no solution solving race condition or dead lock issue. imagine have 2 threads. goal follows. first condition: thread 1 waits thread 2 notifies second condition: thread 2 notifies thread 1 should not wait , continue normal execution. how can implemented correctly without having queue notifies? because want part of code run fast possible , use boolean value instead of adding item queue. there 2 threads, use of queue seems overkill me. pseudo code when there race condition: thread 1: lock(x); if(!signaled) { unlock(x); // ******** // still small gap, how avoid? cv.wait(); // forget spurious wakeup sake of simplicity signaled = false; } else // ******** unlock(x); thread 2: lock(x); signaled = true; cv.notify(); unlock(x); now if remove 2 lines commented ******** race condition solved , chance of deadlock introduced thread1 waits while owning lock , t...