General Utility Library for C++14
2.12
|
A class that allows sending triggers and waiting for them across different threads.
A Trigger object is similar to a digital electric trigger signal. Its state is either high (true) or low (false), and actions are triggered when it is at the high level - that is to say, an arbitrary number of threads can wait until the Trigger object becomes true. Internally, a Trigger is little more than a wrapper around a mutex, a condition variable, and a boolean.
There are two ways to use a Trigger:
To stay in the picture of a digital electric trigger line, high and low states can be assigned and tested like booleans. This allows for easy communication of state:
Sometimes assignment of boolean values does not fit well to a specific task. The member functions trigger() and reset() are identical to assigning true
and false
, respectively:
The unconditional wait() stops execution until the Trigger becomes true or is destructed. If a timeout is required, wait_for() or wait_until() can be used instead.
#include <Trigger.h>
Public Member Functions | |
Trigger (bool triggered=false) noexcept | |
Constructor. | |
GUL_EXPORT | ~Trigger () noexcept |
Destructor: Send a final trigger signal so that all threads waiting on this object have a chance to stop. | |
Trigger (const Trigger &)=delete | |
Not copyable and not movable. | |
Trigger (Trigger &&)=delete | |
Trigger & | operator= (const Trigger &)=delete |
Trigger & | operator= (Trigger &&)=delete |
GUL_EXPORT | operator bool () const noexcept |
Return if the trigger is high==true or low==false. More... | |
GUL_EXPORT Trigger & | operator= (bool interrupt) noexcept |
Set the trigger to high==true or low==false. More... | |
GUL_EXPORT void | reset () noexcept |
Set the trigger to low (false). | |
GUL_EXPORT void | trigger () noexcept |
Set the trigger to high (true). More... | |
GUL_EXPORT void | wait () const |
Suspend execution of the current thread until the trigger goes high (true). More... | |
template<class Rep , class Period > | |
bool | wait_for (const std::chrono::duration< Rep, Period > &delta_t) const |
Suspend execution of the current thread until the trigger goes high (true) or at least the given time span has passed. More... | |
template<class Clock , class Duration > | |
bool | wait_until (const std::chrono::time_point< Clock, Duration > &t) const |
Suspend execution of the current thread until the trigger goes high (true) or the given time point has been passed. More... | |
|
noexcept |
Return if the trigger is high==true or low==false.
Example:
|
noexcept |
Set the trigger to high==true or low==false.
Setting it to true will cause any waiting threads to resume.
|
noexcept |
Set the trigger to high (true).
This causes any waiting threads to resume.
Referenced by ~Trigger().
void gul14::Trigger::wait | ( | ) | const |
Suspend execution of the current thread until the trigger goes high (true).
Execution is also resumed if the object is destructed.
|
inline |
Suspend execution of the current thread until the trigger goes high (true) or at least the given time span has passed.
Execution is also resumed if the object is destructed.
Clock | The type of the underlying clock, e.g. std::chrono::system_clock. |
Duration | The duration type to be used, typically Clock::duration. |
delta_t | A time span to wait. If the trigger still has not become true after (at least) this time, the function returns false. |
Referenced by gul14::sleep().
|
inline |
Suspend execution of the current thread until the trigger goes high (true) or the given time point has been passed.
Execution is also resumed if the object is destructed.
Clock | The type of the underlying clock, e.g. std::chrono::system_clock. |
Duration | The duration type to be used, typically Clock::duration. |
t | A time point after which the function should stop waiting. |