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 signalled and when the waiting thread finally ran, another thread ran and changed the condition, causing a race condition. If the thread wakes up second, it will lose the race, and a spurious wakeup will occur.[citation needed]

On many systems, especially multiprocessor systems, the problem of spurious wakeup is exacerbated because if there are several threads waiting on the condition variable when it's signalled, the system may decide to wake them all up, treating 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.[1] If there are ten threads waiting, only one will win and the other nine will experience spurious wakeup.[citation needed]

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 signalled, though it is not clear how many implementations actually 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 `Inter`.[2] The Linux p-thread implementation of condition variables guarantees it will not do that.[3][4]

Because spurious wakeup can happen whenever there's a race and possibly even in the absence of a race or a signal, 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.[citation needed]

## References

1. ↑ Raymond Chen (February 1, 2018). "Spurious wake-ups in Win32 condition variables". https://devblogs.microsoft.com/oldnewthing/20180201-00/?p=97946. Retrieved May 9, 2020.
2. ↑ "Interrupted Waits on Condition Variables (Solaris Threads Only)". Oracle Corporation. https://docs.oracle.com/cd/E19455-01/806-5257/gen-24356/index.html. Retrieved May 9, 2020.
3. ↑ "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]."
4. ↑ "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.

0.00

(0 votes)

Original source: https://en.wikipedia.org/wiki/Spurious wakeup. Read more

|

Retrieved from "https://handwiki.org/wiki/index.php?title=Spurious_wakeup&oldid=254652"