So I just found out that it\'s legal to signal a condition variable if you\'re not holding the lock in c++11. That seems to open the door to some nasty race condition:
It is not guaranteed - if you don't want to miss the signal then you must lock the mutex prior to notifying. Some applications may be agnostic about missing signals.
From man pthread_signal:
The pthread_cond_signal() or pthread_cond_broadcast() functions may be called by a thread whether or not it currently owns the mutex that threads calling pthread_cond_wait() or pthread_cond_timedwait() have associated with the condition variable during their waits; however, if predictable scheduling behaviour is required, then that mutex is locked by the thread calling pthread_cond_signal() or pthread_cond_broadcast().
Checking the predicate and waiting are not performed atomically in std::condition_variable::wait
(unlocking the lock and sleeping are performed atomically). If it is possible for another thread to change the value of the predicate while this thread holds the mutex, then it is possible for notifications to occur between the predicate check and going to sleep, and effectively be lost.
In your example, if generate_data()
in T2
can alter the result of is_empty()
without holding m_mutex
, it's possible for a notification to happen between T1
checking is_empty()
and sleeping on m_cv
. Holding the mutex at any time between the change to the predicate and the notification is sufficient to guarantee the atomicity of the predicate check and wait
call in the other thread. That could look like:
{
std::lock_guard<std::mutex> lk(m_mutex);
generate_data();
}
m_cv.notify();
or even
generate_data();
std::lock_guard<std::mutex>(m_mutex); // Lock the mutex and drop it immediately
m_cv.notify();