SpatialOps
Semaphore.h
1 #ifndef Nebo_Semaphore_h
2 #define Nebo_Semaphore_h
3 
4 #include <boost/thread/mutex.hpp>
5 #include <boost/thread/condition_variable.hpp>
6 
7 namespace SpatialOps{
8 
20  struct Semaphore {
21  public:
22 
26  Semaphore(int initial=0) : val_(initial){}
27 
29  inline void post() {
30  boost::lock_guard<boost::mutex> lock(mut_);
31  ++val_;
32  cond_.notify_one();
33  }
34 
38  inline void wait() {
39  boost::unique_lock<boost::mutex> lock(mut_);
40  while (val_ <= 0) {
41  cond_.wait(lock);
42  }
43  --val_;
44  }
45 
46  private:
47  boost::condition_variable cond_;
48  boost::mutex mut_;
49  int val_;
50  };
51 }
52 #endif // Nebo_Semaphore_h
Semaphore(int initial=0)
Definition: Semaphore.h:26
void post()
release a resource
Definition: Semaphore.h:29
Provide resource management for multithreaded situations.
Definition: Semaphore.h:20
void wait()
Wait until a resource is available (a call to post is made).
Definition: Semaphore.h:38