semaphore.h
Go to the documentation of this file.
00001 
00023 #ifndef MY_SEMAPHORE_H_
00024 #define MY_SEMAPHORE_H_
00025 
00026 #include <iostream>
00027 #include <boost/thread.hpp>
00028 
00029 namespace micros_swarm{
00030     class Semaphore{
00031         public:
00032             Semaphore(int value): value_(value){}
00033             Semaphore(const Semaphore&) = delete;
00034 
00035             void P()
00036             {
00037                 boost::unique_lock<boost::mutex> lock(mutex_);
00038                 while(value_ == 0) {
00039                     condition_.wait(lock);
00040                 }
00041 
00042                 value_--;
00043             }
00044 
00045             void V()
00046             {
00047                 boost::unique_lock<boost::mutex> lock(mutex_);
00048                 value_++;
00049 
00050                 condition_.notify_one();
00051             }
00052         private:
00053             int value_;
00054             boost::mutex mutex_;
00055             boost::condition_variable condition_;
00056     };
00057 };
00058 
00059 #endif


micros_swarm
Author(s):
autogenerated on Thu Jun 6 2019 18:52:14