Condition variables should have have a single order with respect to notify()
and unlock_sleep()
operations. To achieve this with arbitrary lockables std::condition_variable_any
implementations typically use another mutex internally (to ensure atomicity and to sleep on)
If the unlock_sleep()
and notify()
operations are not atomic with respect to each other you risk a thread unlocking the mutex, another thread signaling and then the original thread going to sleep and never waking up.
I was reading the libstdc++ and libc++ implementations of std::condition_variable_any and noticed this code in the libc++ implementation
{lock_guard<mutex> __lx(*__mut_);}
__cv_.notify_all();
the internal mutex is locked and then immediately unlocked before the signal operation. Doesn't this risk the problem I described above?
libstdc++ seems to have gotten this right
Aucun commentaire:
Enregistrer un commentaire