DDS  ver. 3.4
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 MiscCommon
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  void notifyAll()
39  {
40  {
41  std::lock_guard<std::mutex> lock(m_mutex);
42  m_bFlag = true;
43  }
44  m_condition.notify_all();
45  }
46 
47  void notifyOne()
48  {
49  {
50  std::lock_guard<std::mutex> lock(m_mutex);
51  m_bFlag = true;
52  }
53  m_condition.notify_one();
54  }
55 
56  void reset()
57  {
58  std::lock_guard<std::mutex> lock(m_mutex);
59  m_bFlag = false;
60  }
61 
62  private:
63  bool m_bFlag;
64  std::mutex m_mutex;
65  std::condition_variable m_condition;
66  };
67 }; // namespace MiscCommon
68 #endif /*_DDS_CONDITIONEVENT_H_*/
CConditionEvent()
Definition: ConditionEvent.h:20
void wait()
Definition: ConditionEvent.h:25
void reset()
Definition: ConditionEvent.h:56
bool waitFor(const std::chrono::duration< Rep, Period > &_relTime)
Definition: ConditionEvent.h:32
Helper class for conditional events.
Definition: ConditionEvent.h:17
void notifyAll()
Definition: ConditionEvent.h:38
void notifyOne()
Definition: ConditionEvent.h:47
Miscellaneous functions and helpers are located here.
Definition: BOOST_FILESYSTEM.h:21