Click to Rate and Give Feedback
MSDN
MSDN Library
System Services
Synchronization
 Condition Variables
Condition Variables

Condition variables are synchronization primitives that enable threads to wait until a particular condition occurs. Condition variables are user-mode objects that cannot be shared across processes.

Condition variables enable threads to atomically release a lock and enter the sleeping state. They can be used with critical sections or slim reader/writer (SRW) locks. Condition variables support operations that "wake one" or "wake all" waiting threads. After a thread is woken, it re-acquires the lock it released when the thread entered the sleeping state.

Windows Server 2003 and Windows XP/2000:  Condition variables are not supported.

The following are the condition variable functions.

Condition variable functionDescription
InitializeConditionVariableInitializes a condition variable.
SleepConditionVariableCSSleeps on the specified condition variable and releases the specified critical section as an atomic operation.
SleepConditionVariableSRWSleeps on the specified condition variable and releases the specified SRW lock as an atomic operation.
WakeAllConditionVariableWakes all threads waiting on the specified condition variable.
WakeConditionVariableWakes a single thread waiting on the specified condition variable.

 

The following pseudocode demonstrates the typical usage pattern of condition variables.

CRITICAL_SECTION CritSection;
CONDITION_VARIABLE ConditionVar;

void PerformOperationOnSharedData()
{ 
   EnterCriticalSection(&CritSection);

   // Wait until the predicate is TRUE

   while( TestPredicate() == FALSE )
   {
      SleepConditionVariableCS(&ConditionVar, &CritSection, INFINITE);
   }

   // The data can be changed safely because we own the critical 
   // section and the predicate is TRUE

   ChangeSharedData();

   LeaveCriticalSection(&CritSection);

   // If necessary, signal the condition variable by calling
   // WakeConditionVariable or WakeAllConditionVariable so other
   // threads can wake
}

For example, in an implementation of a reader/writer lock, the TestPredicate function would verify that the current lock request is compatible with the existing owners. If it is, acquire the lock; otherwise, sleep. For a more detailed example, see Using Condition Variables.

Condition variables are subject to spurious wakeups (those not associated with an explicit wake) and stolen wakeups (another thread manages to run before the woken thread). Therefore, you should recheck a predicate (typically in a while loop) after a sleep operation returns.

You can wake other threads using WakeConditionVariable or WakeAllConditionVariable either inside or outside the lock associated with the condition variable. It is usually better to release the lock before waking other threads to reduce the number of context switches.

It is often convenient to use more than one condition variable with the same lock. For example, an implementation of a reader/writer lock might use a single critical section but separate condition variables for readers and writers.

See Also

Using Condition Variables

Send comments about this topic to Microsoft

Build date: 10/8/2009

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
AsyncOp.com      Asaf Shelly   |   Edit   |   Show History
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker