When to unlock for using pthread_cond_signal()?
POSIX specifically allows that a condition variable may be signalled or broadcast with the associated mutex either locked or unlocked. (Or even locked by someone else.) It simply doesn’t matter. At least, signalling while not holding the mutex doesn’t make the program in any way illegal. A condition variable is just a communication mechanism to inform waiters of changes in shared data “predicate” conditions. The predicate itself IS shared data, and must be changed in a way that’s thread-safe. In most cases, this means that you must hold the mutex when you change the data. (But you could also have a predicate like “read() returns data”, so that you could write data, signal the condition variable — and the waiter(s) would simply loop on the condition wait until read() returns some data.) The signal doesn’t need to be synchronized with the predicate value. What you DO need to synchronize is SETTING the predicate and TESTING the predicate. Given that basic and obvious requirement (it’s sh