A spurious wakeup is when a thread wakes up from waiting on a condition variable without being signalled. Why do spurious wakeups happen?
Mainly writing from the StackOverflow Question
The pthreads API allows for spurious wakeups because it doesn’t change the usage much. For correct usage, anyway the condition should be checked after the wakeup, and allowing spurious wakeups can enable faster implementations of the API.
There doesn’t seem to be any concrete reason for this. This is just the API now and it is what it is. Some people mention the call to futex
in Linux getting interrupted by a signal as a spurious wakeup, but the futex(2)
man page says:
EINTR A FUTEX_WAIT or FUTEX_WAIT_BITSET operation was interrupted by a signal (see signal(7)). Before Linux 2.6.22, this error could also be returned for a spurious wakeup; since Linux 2.6.22, this no longer happens.