MiniBachelor
Semaphore.hpp
gehe zur Dokumentation dieser Datei
1 #ifndef _SEMAPHORE_HPP
2 #define _SEMAPHORE_HPP 1
3 #include <mutex>
4 #include <atomic>
5 #include <condition_variable>
6 class Semaphore {
7 private:
8  std::mutex _mtx;
9  std::condition_variable _cv;
10  std::atomic<int> _count;
11 public:
12  Semaphore(int count = 0) {
13  _count = count;
14  }
15  void V() {
16  std::unique_lock<std::mutex> lck(_mtx);
17  _count++;
18  _cv.notify_one();
19  }
20  void P() {
21  std::unique_lock<std::mutex> lck(_mtx);
22  while(_count == 0) _cv.wait(lck);
23  _count--;
24  }
25 };
26 #endif
27 
Semaphore(int count=0)
Definition: Semaphore.hpp:12
std::condition_variable _cv
Definition: Semaphore.hpp:9
Definition: Semaphore.hpp:6
std::mutex _mtx
Definition: Semaphore.hpp:8
void V()
Definition: Semaphore.hpp:15
void P()
Definition: Semaphore.hpp:20
std::atomic< int > _count
Definition: Semaphore.hpp:10