Subj : Re: Question : usage of conditional variables. To : comp.programming.threads From : David Schwartz Date : Thu Jun 09 2005 02:40 pm "Jamie" wrote in message news:1118309979.642664.167300@f14g2000cwb.googlegroups.com... > But, what is this predicate for? You have to be waiting for something. The predicate is what you're waiting for. For example, if the mutex protects a queue of jobs that need to be done, the predicate might be "there is at least one job in the queue". That's what a thread would be waiting for. > My program runs well for a while without predicate(not using a loop), > but it stops and wait a signal forever at some point.. After adding a > predicate and checking it before calling pthread_cond_wait(), it runs > well. But I can't understand why sometimes it works well without > predicate and sometimes not. When you break the rules, you get unpredictable behavior. Sometimes what you get is what you actually wanted. The problem is not spurious wakeups in your case but a race condition caused by a missed wakeup. > What does the spurious wakeups really > mean? > I'm still confused about it.. A spurious wakeup is kind of the opposite of a missed wakeup. A spurious wakeup is caused by a thread that woke up for some reason but by the time it finished waking up, the reason it woke up was no longer true, so it wakes up and can't figure out why. A missed wakeup is one that occured before the thread got to sleep. Condition variables are very simple, primitive elements. They don't store wakeups and they don't guarantee against spurious wakeups. So you must use them only if the predicate is false and in a loop until the predicate is true. DS .