CFlyPGunP

Semaphore.h

Apr 29th, 2019
921
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.55 KB | None | 0 0
  1. #ifndef __INC_METIN_II_SEMAPHORE_H__
  2. #define __INC_METIN_II_SEMAPHORE_H__
  3.  
  4. class CSemaphore
  5. {
  6. private:
  7.     std::mutex m_mutex;
  8.     std::condition_variable m_cv;
  9.     size_t m_count;
  10. public:
  11.     CSemaphore() : m_mutex(), m_count(0)
  12.         {}
  13.    
  14.     inline void Wait() {
  15.         std::unique_lock<std::mutex> lg(m_mutex);
  16.         m_cv.wait(lg, [this] { return this->m_count > 0; });
  17.         m_count--;
  18.     }
  19.  
  20.     inline void Release() {
  21.         m_mutex.lock();
  22.         m_count++;
  23.         m_mutex.unlock();
  24.         m_cv.notify_one();
  25.     }
  26.  
  27.     inline size_t available() const {
  28.         return m_count;
  29.     }
  30. };
  31.  
  32. #endif
Advertisement
Add Comment
Please, Sign In to add comment