Spurious wakeup

In computing, a spurious wakeup occurs when a thread wakes up from waiting on a condition variable without the variable being satisfied. It is referred to as spurious because the thread has seemingly been awakened for no reason. However, they usually happen because in between the time when the condition variable was signaled and when the waiting thread finally ran, another thread ran and changed the condition, causing a race condition. If the thread wakes up a second, it will lose the race, and a spurious wakeup will occur.

The problem of spurious wakeup can exacerbate 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  to wake one thread as a   to wake all of them, thus breaking any possibly expected 1:1 relationship between signals and wakeup. If ten threads are waiting, only one will win and the other nine will experience spurious wakeup.

To allow for implementation flexibility in dealing with error conditions and races inside the operating system, condition variables may also be allowed to return from a wait even if not signaled, though it is not clear how many implementations do that. In the Solaris implementation of condition variables, a spurious wakeup may occur without the condition being assigned if the process is signal; the wait system call aborts and returns. The Linux p-thread implementation of condition variables guarantees it will not do that.

Because spurious wakeup can happen, when a thread wakes on a condition variable, it should always check that the condition it sought is satisfied. If it is not, it should go back to sleeping on the condition variable, waiting for another opportunity.