Subj : Re: Real cause of spurius wakeups To : comp.programming.threads From : Joe Seigh Date : Thu Mar 31 2005 12:13 pm On Thu, 31 Mar 2005 18:43:04 +0400, Vladimir Prus wrote: > > Hello, > > can somebody explain the real reason why spurious wakeups are possible. That > is, why call to pthread_cond_wait can return without > pthread_cond_broadcast/pthread_cond_signal. I'm specifically talking about > spurious wakeup caused by pthread implementation, not the one caused by > loose predicate checking on the notifying side, or "stolen wakeup". I > looked in archives for this group, and in the FAQ, but found only vague > statements that on some SMP systems avoiding spurious wakeup is too hard. > > I understand that pthread_cond_wait need to be wrapped in a loop even if > spurious wakeups did not exist. I'm asking just because the wakeups are > mentioned everywhere, but I never seen a complete explanations. > It allows flexibility in the implementation. A strict definition with no spurious wakeups allowed could possibly require overly expensive synchronization on some platforms. You get to make a tradeoff. The occasional spurious wakeup for much better performance. You can write your own condition variable that does not have spurious wakeups, but it will not perform as well. The Linux futex based condition variables return spurious interrupts on signal interruptions. Not really for performance reasons, AFAICT. Just because then can do it. I've written a faster futex based condition variable that wouldn't be possible if spurious wakeups were not allowed. So it is useful to have condition variables defined that way. -- Joe Seigh .