signal If threads are blocked on the signaled condition variable then one of

Signal if threads are blocked on the signaled

This preview shows page 38 - 41 out of 95 pages.

signal: If threads are blocked on the signaled condition variable, then one of those threads is unblocked. broadcast: Like signal, but unblocks all threads that are blocked on the condition variable. CS350 Operating Systems Spring 2014
Image of page 38
Synchronization 43 Using Condition Variables Condition variables get their name because they allow threads to wait for arbitrary conditions to become true inside of a critical section. Normally, each condition variable corresponds to a particular condition that is of interest to an application. For example, in the bounded buffer producer/consumer example on the following slides, the two conditions are: count> 0 (condition variable notempty ) count<N (condition variable notfull ) when a condition is not true, a thread can wait on the corresponding condition variable until it becomes true when a thread detects that a condition is true, it uses signal or broadcast to notify any threads that may be waiting Note that signalling (or broadcasting to) a condition variable that has no waiters has no effect . Signals do not accumulate. CS350 Operating Systems Spring 2014 Synchronization 44 Waiting on Condition Variables when a blocked thread is unblocked (by signal or broadcast ), it reacquires the lock before returning from the wait call a thread is in the critical section when it calls wait , and it will be in the critical section when wait returns. However, in between the call and the return, while the caller is blocked, the caller is out of the critical section, and other threads may enter. In particular, the thread that calls signal (or broadcast ) to wake up the waiting thread will itself be in the critical section when it signals. The waiting thread will have to wait (at least) until the signaller releases the lock before it can unblock and return from the wait call. This describes Mesa-style condition variables, which are used in OS/161. There are alternative condition variable semantics (Hoare semantics), which differ from the semantics described here. CS350 Operating Systems Spring 2014
Image of page 39
Synchronization 45 Bounded Buffer Producer Using Locks and Condition Variables int volatile count = 0; / * must initially be 0 * / struct lock * mutex; / * for mutual exclusion * / struct cv * notfull, * notempty; / * condition variables * / / * Initialization Note: the lock and cv’s must be created * using lock create() and cv create() before Produce() * and Consume() are called * / Produce(itemType item) { lock acquire(mutex); while (count == N) { cv wait(notfull, mutex); } add item to buffer (call list append()) count = count + 1; cv signal(notempty, mutex); lock release(mutex); } CS350 Operating Systems Spring 2014 Synchronization 46 Bounded Buffer Consumer Using Locks and Condition Variables itemType Consume() { lock acquire(mutex); while (count == 0) { cv wait(notempty, mutex); } remove item from buffer (call list remove front()) count = count - 1; cv signal(notfull, mutex); lock release(mutex); return(item); } Both Produce() and Consume() call cv wait() inside of a while loop. Why?
Image of page 40
Image of page 41

You've reached the end of your free preview.

Want to read all 95 pages?

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture