Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef __INC_METIN_II_SEMAPHORE_H__
- #define __INC_METIN_II_SEMAPHORE_H__
- class CSemaphore
- {
- private:
- std::mutex m_mutex;
- std::condition_variable m_cv;
- size_t m_count;
- public:
- CSemaphore() : m_mutex(), m_count(0)
- {}
- inline void Wait() {
- std::unique_lock<std::mutex> lg(m_mutex);
- m_cv.wait(lg, [this] { return this->m_count > 0; });
- m_count--;
- }
- inline void Release() {
- m_mutex.lock();
- m_count++;
- m_mutex.unlock();
- m_cv.notify_one();
- }
- inline size_t available() const {
- return m_count;
- }
- };
- #endif
Advertisement
Add Comment
Please, Sign In to add comment