SleepConditionVariableCS function (synchapi.h)

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

Syntax

BOOL SleepConditionVariableCS(
  [in, out] PCONDITION_VARIABLE ConditionVariable,
  [in, out] PCRITICAL_SECTION   CriticalSection,
  [in]      DWORD               dwMilliseconds
);

Parameters

[in, out] ConditionVariable

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

[in, out] CriticalSection

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

[in] dwMilliseconds

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.

Remarks

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.

Examples

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

Requirements

Requirement Value
Minimum supported client Windows Vista [desktop apps | UWP apps]
Minimum supported server Windows Server 2008 [desktop apps | UWP apps]
Target Platform Windows
Header synchapi.h (include Windows.h on Windows 7, Windows Server 2008 Windows Server 2008 R2)
Library Kernel32.lib
DLL Kernel32.dll

See also

Condition Variables

Synchronization Functions

Vertdll APIs available in VBS enclaves