According to Wikipedia article on spurious wakeups has this tidbit:
Thepthread_cond_wait()
function in Linux is implemented using thefutex
system call. Each blocking system call on Linux returns abruptly withEINTR
when the process receives a signal. ...pthread_cond_wait()
can't restart the waiting because it may miss a real wakeup in the little time it was outside thefutex
system call. This race condition can only be avoided by the caller checking for an invariant. A POSIX signal will therefore generate a spurious wakeup.
For understanding what is spurious wakeup this is java doc comment from Object.java :
* A thread can also wake up without being notified, interrupted, or
* timing out, a so-called <i>spurious wakeup</i>. While this will rarely
* occur in practice, applications must guard against it by testing for
* the condition that should have caused the thread to be awakened, and
* continuing to wait if the condition is not satisfied. In other words,
* waits should always occur in loops, like this one:
* <pre>
* synchronized (obj) {
* while (<condition does not hold>)
* obj.wait(timeout);
* ... // Perform action appropriate to condition
* }
* </pre>
No comments:
Post a Comment