General Utility Library for C++17 26.5.0
Trigger.h
Go to the documentation of this file.
1
26#ifndef GUL17_TRIGGER_H_
27#define GUL17_TRIGGER_H_
28
29#include <chrono>
30#include <condition_variable>
31#include <mutex>
32
33#include "gul17/internal.h"
34
35namespace gul17 {
36
114{
115public:
117 explicit Trigger(bool triggered = false) noexcept : triggered_{ triggered }
118 {}
119
124 GUL_EXPORT
126
132
146 GUL_EXPORT
147 operator bool() const noexcept;
148
153 GUL_EXPORT
155
157 GUL_EXPORT
158 void reset() noexcept;
159
164 GUL_EXPORT
165 void trigger() noexcept;
166
171 GUL_EXPORT
172 void wait() const;
173
193 {
194 std::unique_lock<std::mutex> lock(mutex_);
195 return cv_.wait_for(lock, delta_t, [this]{ return triggered_; });
196 }
197
214 template <class Clock, class Duration>
215 bool wait_until(const std::chrono::time_point<Clock, Duration>& t) const
216 {
217 std::unique_lock<std::mutex> lock(mutex_);
218 return cv_.wait_until(lock, t, [this]{ return triggered_; });
219 }
220
221private:
222 mutable std::mutex mutex_; // Protects private data and is used with the condition variable
223 mutable std::condition_variable cv_;
224 bool triggered_ = false;
225};
226
228
229} // namespace gul17
230
231#endif
A class that allows sending triggers and waiting for them across different threads.
Definition Trigger.h:114
GUL_EXPORT void trigger() noexcept
Set the trigger to high (true).
Definition Trigger.cc:63
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...
Definition Trigger.h:192
GUL_EXPORT void reset() noexcept
Set the trigger to low (false).
Definition Trigger.cc:57
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 ha...
Definition Trigger.h:215
GUL_EXPORT void wait() const
Suspend execution of the current thread until the trigger goes high (true).
Definition Trigger.cc:75
Trigger(bool triggered=false) noexcept
Constructor.
Definition Trigger.h:117
GUL_EXPORT ~Trigger() noexcept
Destructor: Send a final trigger signal so that all threads waiting on this object have a chance to s...
Definition Trigger.cc:31
auto constexpr bit_set(unsigned bit) noexcept -> ReturnT
Set a bit in an integral type.
Definition bit_manip.h:124
Definition of macros used internally by GUL.
Namespace gul17 contains all functions and classes of the General Utility Library.
Definition doxygen.h:29