Re: pthread_cond_wait and testing condition explicitly



adramolek@xxxxxxxxx writes:

Everything I read about using pthread_cond_wait shows code example
such as this (pseudo-ish code):

int value = 1;
pthread_cond_t cond;
pthread_mutex_t mutex;

void set_value_to_0 () {
pthread_mutex_lock(&mutex);
value = 0;
pthread_cond_broadcast(&cond);
pthread_mutex_unlock(&mutex);
}

void wait_for_0_value () {
pthread_mutex_lock(&mutex);
while (value != 0)
pthread_cond_wait(&cond, &mutex);
pthread_mutex_unlock(&mutex);
}

I know this is explained in various documentation but for some reason
I am still having a really hard time wrapping my head around it: why
do I have to loop on "while (value != 0)" when calling
pthread_cond_wait...? I set the value to 0 then signal the condition
variable. What kind of situations could happen that would make value
not be 0 after the condition variable was signalled?

There is a simpler reason as yet not explained...

The waiting thread has released the mutex while it is suspended (you
should be able to see that is required -- no thread would ever be able
to signal that value == 0 if the mutex were held by waiting threads).
Assume that there are several threads waiting on the same cond_var.

When a thread finally signals cond_broadcast, more than one thread may
be signalled (and certainly will be if more than one thread is
waiting). These threads will all be runnable, but only one will get
the mutex. Presumably that thread will "destroy" the situation where
value == 0 (you don't show that in your pseudo-code) but eventually it
will leave the critical section protected by the mutex and one of the
other awakened threads will claim it. It will find that value != 0
(the other thread got there first) so it must wait again. If it did
not test, it would assume (incorrectly) that the situation indicated
by the condition variable still existed.

This is just a logical consequence of the design. It is not the same
as spurious wake-up, although the solution is the same. Spurious
wake-up is the inadvertent signaling of more than one thread by
cond_signal.

Even without the possibility of spurious wake-up, the while loop is
correct /even when using cond_signal/ because many algorithms use
cond_signal in such a way that you must re-test. For example, a
thread will put data on a queue and signal "not empty". It may do so
three times before the first awakened consumer thread gets going.
That consumer may eat all the available data so the other threads,
awakened by the other two "not empty" signals must test the real
condition -- they will find the queue is empty again despite having
been signaled.

--
Ben.
.



Relevant Pages

  • Re: [newbie] conditions: signal sent before waiter available?
    ... With a condition variable is a mutex associated. ... In the book, some examples demonstrate condition variables, waiting for signals, sending signals and awakening. ... So the usuale pattern to wait for a predicate is to lock the mutex that synchronize the access to the predicate and than check if that predicate is true. ...
    (comp.programming.threads)
  • Re: bizarre timing results
    ... > Are you using signals, or how precisely are you waiting? ... [wait on state changed condition associated with mutex] ... > but you may pay a MESI communications tax. ...
    (comp.os.linux.development.system)
  • Re: [newbie] conditions: signal sent before waiter available?
    ... > With a condition variable is a mutex associated. ... > the one waiting needs to lock the mutex before calling the wait function, ... > signals, sending signals and awakening. ... You are fundamentally misunderstanding how condition variables *must* be ...
    (comp.programming.threads)
  • Re: conditions: signal sent before waiter available?
    ... > one waiting needs to lock the mutex before ... > signals, sending signals and awakening. ... In the alarm example, the waiting ... > thread (call it w-thread) is created ...
    (comp.programming.threads)
  • Re: conditions: signal sent before waiter available?
    ... there is something I don't understand regarding sending signals. ... > With a condition variable is a mutex associated. ... the waiting thread (call it w-thread) is created ...
    (comp.programming.threads)