Spurious wakeup
The problem of spurious wakeup can be exacerbated on multiprocessor systems. When several threads are waiting on a single condition variable, the system may decide to wake all threads up when it's signaled. The system treats every signal( ) to wake one thread as a broadcast( ) to wake all of them, thus breaking any possibly expected 1:1 relationship between signals and wakeup.Cite error: Closing </ref> missing for <ref> tag The Linux p-thread implementation of condition variables guarantees that it will not do that.[1][2]
Because spurious wakeup can happen, when a thread wakes after waiting on a condition variable, it should always check that the condition it sought is still satisfied. If it is not, it should go back to sleeping on the condition variable, waiting for another opportunity.[3]
References
- ↑ "pthread_cond_wait(3) - Linux man page". die.net. https://linux.die.net/man/3/pthread_cond_wait. Retrieved May 9, 2020. "These functions shall not return an error code of [EINTR]."
- ↑ "pthread_cond_timedwait, pthread_cond_wait - wait on a condition". The Open Group. 2018. http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_cond_timedwait.html. Retrieved May 9, 2020.
- ↑ Arpaci-Dusseau, Remzi; Arpaci-Dusseau, Andrea (November 2023). "Condition Variables". Operating Systems: Three Easy Pieces. https://pages.cs.wisc.edu/~remzi/OSTEP/threads-cv.pdf.
