General Utility Library for C++14  2.8
Trigger.h
Go to the documentation of this file.
1 
23 #ifndef GUL14_TRIGGER_H_
24 #define GUL14_TRIGGER_H_
25 
26 #include <chrono>
27 #include <condition_variable>
28 #include <mutex>
29 #include "gul14/internal.h"
30 
31 namespace gul14 {
32 
33 
104 class Trigger
105 {
106 public:
108  explicit Trigger(bool triggered = false) noexcept : triggered_{ triggered }
109  {}
110 
115  GUL_EXPORT
116  ~Trigger() noexcept;
117 
119  Trigger(const Trigger&) = delete;
120  Trigger(Trigger&&) = delete;
121  Trigger& operator=(const Trigger&) = delete;
122  Trigger& operator=(Trigger&&) = delete;
123 
137  GUL_EXPORT
138  operator bool() const noexcept;
139 
144  GUL_EXPORT
145  Trigger& operator=(bool interrupt) noexcept;
146 
148  GUL_EXPORT
149  void reset() noexcept;
150 
155  GUL_EXPORT
156  void trigger() noexcept;
157 
162  GUL_EXPORT
163  void wait() const;
164 
182  template <class Rep, class Period>
183  bool wait_for(const std::chrono::duration<Rep, Period>& delta_t) const
184  {
185  std::unique_lock<std::mutex> lock(mutex_);
186  return cv_.wait_for(lock, delta_t, [this]{ return triggered_; });
187  }
188 
205  template <class Clock, class Duration>
206  bool wait_until(const std::chrono::time_point<Clock, Duration>& t) const
207  {
208  std::unique_lock<std::mutex> lock(mutex_);
209  return cv_.wait_until(lock, t, [this]{ return triggered_; });
210  }
211 
212 private:
213  mutable std::mutex mutex_; // Protects private data and is used with the condition variable
214  mutable std::condition_variable cv_;
215  bool triggered_ = false;
216 };
217 
218 } // namespace gul14
219 
220 #endif
A class that allows sending triggers and waiting for them across different threads.
Definition: Trigger.h:105
GUL_EXPORT void reset() noexcept
Set the trigger to low (false).
Definition: Trigger.cc:54
Trigger(bool triggered=false) noexcept
Constructor.
Definition: Trigger.h:108
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:28
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:206
GUL_EXPORT void trigger() noexcept
Set the trigger to high (true).
Definition: Trigger.cc:60
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:183
GUL_EXPORT void wait() const
Suspend execution of the current thread until the trigger goes high (true).
Definition: Trigger.cc:72
Definition of macros used internally by GUL.
Namespace gul14 contains all functions and classes of the General Utility Library.
Definition: doxygen.h:26