ezEngine Release 26.3
Loading...
Searching...
No Matches
ezConditionVariable Class Reference

Condition variables are used to put threads to sleep and wake them up upon certain events. More...

#include <ConditionVariable.h>

Public Types

enum class  WaitResult { Signaled , Timeout }
 

Public Member Functions

void Lock ()
 Locks the internal mutex. Recursive locking is allowed.
 
ezResult TryLock ()
 Tries to lock the internal mutex. Recursive locking is allowed.
 
void Unlock ()
 Unlocks the internal mutex. Must be called as often as it was locked.
 
void SignalOne ()
 Wakes up one waiting thread.
 
void SignalAll ()
 Wakes up all the threads that are currently waiting for the variable.
 
void UnlockWaitForSignalAndLock () const
 Puts the calling thread to sleep and waits for the variable to get signaled.
 
WaitResult UnlockWaitForSignalAndLock (ezTime timeout) const
 Same as UnlockWaitForSignalAndLock() but with an additional timeout condition.
 

Detailed Description

Condition variables are used to put threads to sleep and wake them up upon certain events.

The ezConditionVariable works in conjunction with a mutex. When waiting for a signal, the OS typically puts the waiting thread to sleep. Using SignalOne() or SignalAll() other threads can wake up one or all threads that are currently waiting on the condition variable.

When a thread is woken up, it automatically holds the lock on the condition variable's mutex, which can be used to safely access or modify certain state.

ezConditionVariable is a low-level threading construct. Higher level functionality such as ezThreadSignal may be more suitable for most use cases.

See also
ezThreadSignal, ezMutex

Member Enumeration Documentation

◆ WaitResult

Enumerator
Signaled 

A signal was received before the timeout.

Timeout 

The timeout period elapsed without receiving a signal.

Member Function Documentation

◆ SignalAll()

void ezConditionVariable::SignalAll ( )

Wakes up all the threads that are currently waiting for the variable.

If no thread is currently waiting, this has no effect.

◆ SignalOne()

void ezConditionVariable::SignalOne ( )

Wakes up one waiting thread.

If no threads are waiting, this has no effect. Due to OS scheduling behavior, spurious wakeups may occasionally wake more than one thread.

◆ UnlockWaitForSignalAndLock() [1/2]

void ezConditionVariable::UnlockWaitForSignalAndLock ( ) const

Puts the calling thread to sleep and waits for the variable to get signaled.

Asserts that the ezConditionVariable is locked when the function is called. The mutex will be unlocked and the thread is put to sleep. When the signal arrives, the thread is woken up and the mutex is locked again.

◆ UnlockWaitForSignalAndLock() [2/2]

ezConditionVariable::WaitResult ezConditionVariable::UnlockWaitForSignalAndLock ( ezTime  timeout) const

Same as UnlockWaitForSignalAndLock() but with an additional timeout condition.

If the timeout is reached before the signal arrived, the function returns with WaitResult::Timeout.

Note
If the timeout is reached, the mutex will still get locked!

The documentation for this class was generated from the following files: