SleepConditionVariableCS function

Sleeps on the specified condition variable and releases the specified critical section as an atomic operation.


BOOL WINAPI SleepConditionVariableCS(
  _Inout_ PCONDITION_VARIABLE ConditionVariable,
  _Inout_ PCRITICAL_SECTION   CriticalSection,
  _In_    DWORD               dwMilliseconds


ConditionVariable [in, out]

A pointer to the condition variable. This variable must be initialized using the InitializeConditionVariable function.

CriticalSection [in, out]

A pointer to the critical section object. This critical section must be entered exactly once by the caller at the time SleepConditionVariableCS is called.

dwMilliseconds [in]

The time-out interval, in milliseconds. If the time-out interval elapses, the function re-acquires the critical section and returns zero. If dwMilliseconds is zero, the function tests the states of the specified objects and returns immediately. If dwMilliseconds is INFINITE, the function's time-out interval never elapses. For more information, see Remarks.

Return value

If the function succeeds, the return value is nonzero.

If the function fails or the time-out interval elapses, the return value is zero. To get extended error information, call GetLastError. Possible error codes include ERROR_TIMEOUT, which indicates that the time-out interval has elapsed before another thread has attempted to wake the sleeping thread.


A thread that is sleeping on a condition variable can be woken before the specified time-out interval has elapsed using the WakeConditionVariable or WakeAllConditionVariable function. In this case, the thread wakes when the wake processing is complete, and not when its time-out interval elapses. After the thread is woken, it re-acquires the critical section it released when the thread entered the sleeping state.

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.


For an example that uses this function, see Using Condition Variables.


Minimum supported client

Windows Vista [desktop apps | UWP apps]

Minimum supported server

Windows Server 2008 [desktop apps | UWP apps]

Minimum supported phone

Windows Phone 8


WinBase.h on Windows Vista, Windows 7, Windows Server 2008 and Windows Server 2008 R2 (include Windows.h);
Synchapi.h on Windows 8 and Windows Server 2012





See also

Condition Variables
Synchronization Functions