Advertisement
Guest User

Untitled

a guest
Feb 18th, 2011
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.77 KB | None | 0 0
  1. #ifndef BOOST_THREAD_BINARY_SEMAPHORE
  2. #define BOOST_THREAD_BINARY_SEMAPHORE
  3.  
  4. #include <boost/thread/condition.hpp>
  5. #include <boost/thread/mutex.hpp>
  6.  
  7. class binary_semaphore
  8. {
  9.     typedef boost::mutex::scoped_lock lock;
  10.  
  11. public:
  12.     explicit binary_semaphore( bool initial_set = false )
  13.     : _set( initial_set )
  14.     {}
  15.  
  16.     void signal()
  17.     {
  18.         lock lk( _mutex );
  19.  
  20.         _set = true;
  21.  
  22.         _condition.notify_one();
  23.     }
  24.  
  25.     void wait()
  26.     {
  27.         lock lk( _mutex );
  28.  
  29.         while( _set == false )
  30.         {
  31.              _condition.wait( lk );
  32.         }
  33.  
  34.         _set = false ;
  35.     }
  36.  
  37. private:
  38.  
  39.     bool _set;
  40.  
  41.     boost::mutex _mutex;
  42.  
  43.     boost::condition_variable _condition;
  44. };
  45.  
  46.  
  47. #endif // BOOST_THREAD_BINARY_SEMAPHORE
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement