![]() |
ezEngine
Release 25.03
|
Waiting on a thread signal puts the waiting thread to sleep. Other threads can wake it up by raising the signal. More...
#include <ThreadSignal.h>
Public Types | |
enum | Mode { Mode::AutoReset, Mode::ManualReset } |
enum | WaitResult { Signaled, Timeout } |
Public Member Functions | |
ezThreadSignal (Mode mode=Mode::AutoReset) | |
void | WaitForSignal () const |
Waits until the signal is raised. More... | |
WaitResult | WaitForSignal (ezTime timeout) const |
Waits until either the signal is raised or the timeout is reached. More... | |
void | RaiseSignal () |
Wakes up one thread that is currently waiting for this signal. More... | |
void | ClearSignal () |
Mostly relevant for ManualReset mode, to reset the signal state. | |
Waiting on a thread signal puts the waiting thread to sleep. Other threads can wake it up by raising the signal.
ezThreadSignal is similar to ezConditionVariable but adds some internal state, which makes it more suitable for common use cases. For instance, in contrast to ezConditionVariable, one can wait for an ezThreadSignal and get awoken, even if the signal was raised before a thread tried to wait on it. At any given time the thread signal is either 'raised' or 'cleared'. Waiting for a 'raised' signal will return immediately. This makes it easier to implement a simple producer/consumer scenario.
Once a waiting thread is woken up, the signal state is cleared automatically, unless the ezThreadSignal uses 'ManualReset' mode. Thus, in AutoReset mode, it is guaranteed that exactly one thread will be woken up (or not even put to sleep) for every signal.
If an already raised ezThreadSignal is raised again, this has no effect.
|
strong |
void ezThreadSignal::RaiseSignal | ( | ) |
Wakes up one thread that is currently waiting for this signal.
If no thread is currently waiting for the signal, it stays set, and the next thread that calls 'WaitForSignal' will continue uninterrupted.
In AutoReset mode, if more than one thread is waiting for the signal, one of them is awoken (randomly), while the others stay asleep. The signal is reset immediately after awakening one thread, so it must be signaled again to awaken another thread.
In ManualReset mode all threads that called WaitForSignal may get awoken. All future threads calling WaitForSignal will immediately continue and not even got to sleep. Only once ClearSignal() is executed, will threads be put to sleep again.
void ezThreadSignal::WaitForSignal | ( | ) | const |
Waits until the signal is raised.
The waiting thread is put to sleep in the mean time.
ezThreadSignal::WaitResult ezThreadSignal::WaitForSignal | ( | ezTime | timeout | ) | const |
Waits until either the signal is raised or the timeout is reached.
The waiting thread is put to sleep in the mean time.