SleepConditionVariableSRW function (synchapi.h)

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

Syntax

BOOL SleepConditionVariableSRW(
  [in, out] PCONDITION_VARIABLE ConditionVariable,
  [in, out] PSRWLOCK            SRWLock,
  [in]      DWORD               dwMilliseconds,
  [in]      ULONG               Flags
);

Parameters

[in, out] ConditionVariable

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

[in, out] SRWLock

A pointer to the lock. This lock must be held in the manner specified by the Flags parameter.

[in] dwMilliseconds

The time-out interval, in milliseconds. The function returns if the interval elapses. 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.

[in] Flags

If this parameter is CONDITION_VARIABLE_LOCKMODE_SHARED, the SRW lock is in shared mode. Otherwise, the lock is in exclusive mode.

Return value

If the function succeeds, the return value is nonzero.

If the function fails, the return value is 0. To get extended error information, call GetLastError.

If the timeout expires the function returns FALSE and GetLastError returns ERROR_TIMEOUT.

Remarks

If the lock is unlocked when this function is called, the function behavior is undefined.

The thread can be woken using the WakeConditionVariable or WakeAllConditionVariable function. After the thread is woken, it re-acquires the lock 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.

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

Slim Reader/Writer (SRW) Locks

Synchronization Functions

Vertdll APIs available in VBS enclaves