Variable conditions are usually used in such a way that the state to which they refer changes under the mutex. However, when the state is only one flag for installation only, there is no need for a mutex to prevent simultaneous execution. Therefore, you can do something like this:
flag = 1; pthread_cond_broadcast(&cvar);
However, it is safe if pthread_cond_broadcast implies a write memory barrier; otherwise, the waiting thread can see that the state variable is passed before the flag is written. That is, the waiting thread may wake up, consume the cvar signal, but see the flag 0 more.
So my question is: do pthread_cond_broadcast and pthread_cond_signal calls imply a write memory barrier? If so, where is it indicated in the corresponding POSIX (or other) specifications? The spectrum seemed obscure at this point.
Note. I know that in practice this leads to a memory barrier (on Linux, because waking up threads means a complete processor memory barrier, and calling a cross-library function means a compiler memory barrier). However, I'm interested here in what the specifiers are.
source share