Plasma Engine  2.0
Loading...
Searching...
No Matches
plConditionVariable 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.
 
plResult 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 of the threads that are currently waiting for the variable.
 
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 (plTime 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 plConditionVariable 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.

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

See also
plThreadSignal

Member Function Documentation

◆ SignalAll()

void plConditionVariable::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 plConditionVariable::SignalOne ( )

Wakes up one of the threads that are currently waiting for the variable.

If no thread is currently waiting, this has no effect. In rare cases more than one thread can be woken up, called a spurious wake up.

◆ UnlockWaitForSignalAndLock() [1/2]

void plConditionVariable::UnlockWaitForSignalAndLock ( ) const

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

Asserts that the plConditionVariable 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]

plConditionVariable::WaitResult plConditionVariable::UnlockWaitForSignalAndLock ( plTime 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: