DDS  ver. 3.6
ConditionEvent.h
Go to the documentation of this file.
1 // Copyright 2014 GSI, Inc. All rights reserved.
2 //
3 //
4 #ifndef _DDS_CONDITIONEVENT_H_
5 #define _DDS_CONDITIONEVENT_H_
6 
7 // STD
8 #include <chrono>
9 #include <condition_variable>
10 #include <mutex>
11 
12 namespace dds::misc
13 {
18  {
19  public:
21  : m_bFlag(false)
22  {
23  }
24 
25  void wait()
26  {
27  std::unique_lock<std::mutex> lock(m_mutex);
28  m_condition.wait(lock, [&]() { return m_bFlag; });
29  }
30 
31  template <class Rep, class Period>
32  bool waitFor(const std::chrono::duration<Rep, Period>& _relTime)
33  {
34  std::unique_lock<std::mutex> lock(m_mutex);
35  return m_condition.wait_for(lock, _relTime, [&]() { return m_bFlag; });
36  }
37 
38  template <class Clock, class Duration>
39  bool waitUntil(const std::chrono::time_point<Clock, Duration>& _timeoutTime)
40  {
41  std::unique_lock<std::mutex> lock(m_mutex);
42  return m_condition.wait_until(lock, _timeoutTime, [&]() { return m_bFlag; });
43  }
44 
45  void notifyAll()
46  {
47  {
48  std::lock_guard<std::mutex> lock(m_mutex);
49  m_bFlag = true;
50  }
51  m_condition.notify_all();
52  }
53 
54  void notifyOne()
55  {
56  {
57  std::lock_guard<std::mutex> lock(m_mutex);
58  m_bFlag = true;
59  }
60  m_condition.notify_one();
61  }
62 
63  void reset()
64  {
65  std::lock_guard<std::mutex> lock(m_mutex);
66  m_bFlag = false;
67  }
68 
69  private:
70  bool m_bFlag;
71  std::mutex m_mutex;
72  std::condition_variable m_condition;
73  };
74 }; // namespace dds::misc
75 #endif /*_DDS_CONDITIONEVENT_H_*/
void notifyAll()
Definition: ConditionEvent.h:45
bool waitUntil(const std::chrono::time_point< Clock, Duration > &_timeoutTime)
Definition: ConditionEvent.h:39
void reset()
Definition: ConditionEvent.h:63
Helper class for conditional events.
Definition: ConditionEvent.h:17
void wait()
Definition: ConditionEvent.h:25
bool waitFor(const std::chrono::duration< Rep, Period > &_relTime)
Definition: ConditionEvent.h:32
void notifyOne()
Definition: ConditionEvent.h:54
CConditionEvent()
Definition: ConditionEvent.h:20
Definition: BoostHelper.h:14