Condition Variables for Synchronization
- Indiscriminate use of locks can result in idling overhead from blocked threads.
- While the function pthread_mutex_trylock removes this overhead, it introduces the overhead of polling for availability of locks.
- For example, if the producer-consumer example is rewritten using pthread_mutex_trylock instead of pthread_mutex_lock,
- the producer and consumer threads would have to periodically poll for availability of lock (and subsequently availability of buffer space or tasks on queue).
- A natural solution to this problem is to suspend the execution of the polling thread until space becomes available.
- An interrupt driven mechanism as opposed to a polled mechanism.
- The availability of space is signaled by the thread that holding the space.
- The functionality to accomplish this is provided by a condition variable.
- A condition variable is a data object used for synchronizing threads and always used in conjunction with a mutex lock.
- While mutexes implement synchronization by controlling thread access to data,
- condition variables allow threads to synchronize based upon the actual value of data.
- This variable allows a thread to block itself until specified data reaches a predefined state.
- pthread_cond_wait
- A thread locks this mutex and tests the predicate defined on the shared variable;
- if the predicate is not true, the thread waits on the condition variable associated with the predicate using this function.
- A call to this function blocks the execution of the thread until it receives a signal from another thread or is interrupted by an OS signal.
- In addition to blocking the thread, the pthread_cond_wait function releases the lock on mutex.
- This is important because otherwise no other thread will be able to work on the shared variable and the predicate would never be satisfied.
- pthread_cond_signal
- When the condition is signaled, pthread_cond_signal, one of these threads in the queue is unblocked,
- and when the mutex becomes available, it is handed to this thread (and the thread becomes runnable).
- When the thread is released on a signal, it waits to reacquire the lock on mutex before resuming execution.
- It is convenient to think of each condition variable as being associated with a queue.
- Threads performing a condition wait on the variable relinquish their lock and enter the queue.
- $&bull#bullet;$
- pthread_cond_init & pthread_cond_destroy
- $&bull#bullet;$
- Function calls for initializing and destroying condition variables.
- $&bull#bullet;$
- Condition variables must be declared with type pthread_cond_t, and must be initialized before they can be used.
- $&bull#bullet;$
- There are two ways to initialize a condition variable:
- 1
- Statically, when it is declared. For example: pthread_cond_t myconvar = PTHREAD_COND_INITIALIZER;
- 2
- Dynamically, with the pthread_cond_init() routine.
- The function pthread_cond_init initializes a condition variable (pointed to by cond).
- The ID of the created condition variable is returned to the calling thread through the condition parameter.
- This method permits setting condition variable object attributes, attr. (NULL assigns default attributes)
- If at some point in a program a condition variable is no longer required, it can be discarded using the function pthread_cond_destroy.
Figure 6.8:
A representative sequence for using condition variables.
|
- When a thread performs a condition wait, it takes itself off the runnable list consequently, it does not use any CPU cycles until it is woken up.
- This is in contrast to a mutex lock which consumes CPU cycles as it polls for the lock.
- pthread_cond_broadcast.
- In some cases, it may be beneficial to wake all threads that are waiting on the condition variable as opposed to a single thread.
- An example of this is in the producer-consumer scenario with large work queues and multiple tasks being inserted into the work queue on each insertion cycle.
- Another example is in the implementation of barriers.
- pthread_cond_timedwait,
- It is often useful to build time-outs into condition waits.
- Using the function a thread can perform a wait on a condition variable until a specified time expires.
- At this point, the thread wakes up by itself if it does not receive a signal or a broadcast.
- If the absolute time abstime specified expires before a signal or broadcast is received, the function returns an error message.
- It also reacquires the lock on mutex when it becomes available.
Cem Ozdogan
2010-12-27