racic racic - 1 year ago 111
C Question

What happens to a thread that got woken up by pthread_cond_signal() but lost competition for a mutex

Regarding this:
How To Use Condition Variable

Say we have number of consumer threads that execute such code (copied from the referenced page):

while (TRUE) {
s = pthread_mutex_lock(&mtx);
while (avail == 0) { /* Wait for something to consume */
s = pthread_cond_wait(&cond, &mtx);
while (avail > 0) { /* Consume all available units */
s = pthread_mutex_unlock(&mtx);

I assume that scenario here is: main thread calls pthread_cond_signal() to tell consumer threads to do some work.

As I understand it - subsequent threads call pthread_mutex_lock() and then pthread_cond_wait() (which atomically unlocks the mutex). By now none of the consumer threads is claiming the mutex, they all wait on pthread_cond_wait().

When the main thread calls pthread_cond_signal(), following the manpage, at least one thread is waken up. When any of them returns from pthread_cond_wait() it automatically claims the mutex.

So my question is: what happens now regarding the provided example code? Namely, what does the thread that lost the contest for the mutex do now?

(AFAICT the thread that won the mutex, should run the rest of the code and release the mutex. The one that lost should be stuck waiting on the mutex - somewhere in the 1st nested
loop - while the winner holds it and after it's been released start blocking on pthread_cond_wait() beacuse the
while (avail == 0)
will be satisfied by then. Am I correct?)

Answer Source

Note that pthread_cond_signal() is generally intended to wake up only one waiting thread (that's all that it guarantees). But it could wake more 'accidentally'. The while (avail > 0) loop performs two functions:

  • it allows the one thread guaranteed to be woken up to consume all queued work units
  • it prevents additional 'accidentally' awakened threads from assuming that there's work to be done, when there might not be since the initial thread would have handled all of them.

It also prevents a race condition where a work unit might have been placed on the queue after the while (avail > 0) has completed, but before the worker thread has waited on the condition again - but that race is also handled by the if test just before calling pthread_cond_wait().

Basically when a thread is awakened, all it knows is that there might be work units for it to consume, but there might not (another thread might have consumed them).

So the sequence of events that occurs when pthread_cond_signal() is called is:

  • the system will wake one or more threads waiting on the condition
  • all the threads that are awakened will then try to acquire the mutex - only one of them can acquire it at any particular moment, since that's the purpose of a mutex
  • that thread will then proceed, perform the work in the while (avail > 0) loop, then will release the mutex
  • at that point one of the other threads that were previously woken up will acquire the mutex and work the same loop, then release the mutex. Generally, there will be no work units available anymore (since the first thread would have consumed all of them), but if another thread had added an additional unit (or more), then this thread would handle that work
  • the next thread will acquire the mutex and perform that same set of logic
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download