![]() |
Plasma Engine
2.0
|
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 class | Mode { AutoReset , ManualReset } |
enum class | WaitResult { Signaled , Timeout } |
Public Member Functions | |
plThreadSignal (Mode mode=Mode::AutoReset) | |
void | WaitForSignal () const |
Waits until the signal is raised. | |
WaitResult | WaitForSignal (plTime timeout) const |
Waits until either the signal is raised or the timeout is reached. | |
void | RaiseSignal () |
Wakes up one thread that is currently waiting for this signal. | |
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.
plThreadSignal is similar to plConditionVariable but adds some internal state, which makes it more suitable for common use cases. For instance, in contrast to plConditionVariable, one can wait for an plThreadSignal 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 plThreadSignal 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 plThreadSignal is raised again, this has no effect.
|
strong |
void plThreadSignal::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 plThreadSignal::WaitForSignal | ( | ) | const |
Waits until the signal is raised.
The waiting thread is put to sleep in the mean time.
plThreadSignal::WaitResult plThreadSignal::WaitForSignal | ( | plTime | 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.