previous
 next 
CS 3733 Operating Systems Notes: POSIX Condition Variables
(change semester)

  Files are available by executing ~classque/usp-conditionvariables on the CS network.
 
 

This material is taken from the Section 13.4 of USP
Problem: How do you have a thread wait for a condition without busy waiting?
If the condition is simple, such as waiting for an integer to be greater than 0, we can do this with semaphores.
Example: wait(empty)
When another thread creates a buffer slot, it can execute: signal(empty)
If we have more general condition, such as x!=y we can also do it with semaphore, but not in a simple way.
We cannot solve this problem with mutexes alone.

To do this with mutexes we would have to:
About the name condition variable:
A condition variable is associated with a mutex, not with a condition.
They get their name from the way they are used (or the problem they are used to solve), not from what they are.
There are 2 basic operations you can do with condition variables: wait and signal

Question: In how many different contexts have we seen the names wait and signal

wait:
signal:

POSIX condition variable syntax:

Creating and initializing a condition variable:
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

int pthread_cond_wait(pthread_cond_t *restrict cond,
          pthread_mutex_t *restrict mutex);
int pthread_cond_signal(pthread_cond_t *cond);


Rules
Examples: (no error checking)

Example 13.13: wait for the condition x==y
pthread_mutex_lock(&m);
while (x != y)
   pthread_cond_wait(&v, &m);
/* modify x or y if necessary */
pthread_mutex_unlock(&m);


Example 13.14: modify a shared variable and wake up a thread to check the condition:
pthread_mutex_lock(&m);
x++;
pthread_cond_signal(&v);
pthread_mutex_unlock(&m);


Example 13.22: wait for test_condition() to be true:
static pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t v = PTHREAD_COND_INITIALIZER;

pthread_mutex_lock(&m);
while (!test_condition())                       /* get resource */
   pthread_cond_wait(&v, &m);
    /*  do critical section, possibly changing test_condition() */
pthread_cond_signal(&v);               /* inform another thread */
pthread_mutex_unlock(&m);


A complete example: a barrier
A barrier is a synchronization construct that prevents all threads from continuing until they all have reached a certain place in their execution.
When it does happen, all threads can proceed.
See Program 13.13, tbarrier.c on page 472

It uses pthread_cond_broadcast that wakes up all threads waiting for the condition variable. This is similar to notifyAll in Java monitors.

See Section 13.4 of USP for more information about POSIX condition variables.

Next Notes

Back to CS 3733 Notes Table of Contents