|
|
Condition VariableA condition variable is a type representing a queue of suspended (delayed) processes or threads waiting on some condition. It can be considered as part of the monitor synchronization mechanism, and it used by a process or thread to delay until the monitor's state satisfies some condition; it is also used to awaken a delayed process when the condition becomes true. Two basic operations are defined: - wait: enqueue the calling process on the condition variable.
- signal: wake up one process waiting on the condition variable (if there is one). (Different implementations of signal are possible; it may be guaranteed to awaken the process at the head of the queue, or it may be simply guaranteed to awaken some process from the queue.)
Example: A monitor for the producer-consumer problem. monitor ProducerConsumer { ItemType bufferslots; Condition bufferHasSpace, bufferHasData; procedure putItem(ItemType & item) { while(itemCount == slots) bufferHasSpace.wait(); buffer.add(item); bufferHasData.signal(); } procedure getItem(ItemType & item) { while(itemCount == 0) bufferHasData.wait(); item = buffer.removeFirst(); bufferHasSpace.signal(); } }
|
 |