OpenRTM  1.0.0
Condition.h
[詳解]
1 // -*- C++ -*-
20 #ifndef COIL_CONDITION_H
21 #define COIL_CONDITION_H
22 
23 #include <pthread.h>
24 #include <algorithm>
25 #include <ctime>
26 
27 
28 namespace coil
29 {
43  template <class M>
44  class Condition
45  {
46  public:
47 
63  Condition(M& mutex)
64  : m_mutex(mutex)
65  {
66  ::pthread_cond_init(&m_cond, 0);
67  }
68 
85  {
86  ::pthread_cond_destroy(&m_cond);
87  }
88 
104  inline void signal()
105  {
106  ::pthread_cond_signal(&m_cond);
107  }
108 
124  inline void broadcast()
125  {
126  ::pthread_cond_broadcast(&m_cond);
127  }
128 
148  bool wait()
149  {
150  return 0 == ::pthread_cond_wait(&m_cond, &m_mutex.mutex_);
151  }
152 
178  bool wait(long second, long nano_second = 0)
179  {
180  timespec abstime;
181  abstime.tv_sec = std::time(0) + second;
182  abstime.tv_nsec = nano_second;
183  return 0 == ::pthread_cond_timedwait(&m_cond, &m_mutex.mutex_, &abstime);
184  }
185 
186  private:
187  Condition(const M&);
188  Condition& operator=(const M &);
189  pthread_cond_t m_cond;
190  M& m_mutex;
191  };
192 };
193 #endif // COIL_CONDITION_H
bool wait()
スレッド実行の待機
Definition: Condition.h:148
void signal()
スレッド実行の再開
Definition: Condition.h:104
bool wait(long second, long nano_second=0)
設定時間のスレッド実行待機
Definition: Condition.h:178
~Condition()
デストラクタ
Definition: Condition.h:84
Condition テンプレートクラス
Definition: Condition.h:44
Condition(M &mutex)
コンストラクタ
Definition: Condition.h:63
void broadcast()
全スレッド実行の再開
Definition: Condition.h:124