Advertisement
Guest User

Untitled

a guest
Jan 24th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. #ifndef SEMAPHORE_SEMAPHORE_H
  2. #define SEMAPHORE_SEMAPHORE_H
  3.  
  4. class Semaphore {
  5. public:
  6. explicit Semaphore(int threads_cnt);
  7. void Wait();
  8. void Notify();
  9. private:
  10. int threads;
  11. std::mutex mut;
  12. std::condition_variable con_var;
  13. };
  14. Semaphore::Semaphore(int threads_cnt) {
  15. threads = threads_cnt;
  16. }
  17. void Semaphore::Wait() {
  18. std::unique_lock<std::mutex> lock(mut);
  19. if (threads == 0) {
  20. con_var.wait(lock);
  21. threads -= 1;
  22. }
  23. }
  24. void Semaphore::Notify() {
  25. std::unique_lock<std::mutex> lock(mut);
  26. if (threads < 0) {
  27. con_var.notify_one();
  28. threads++;
  29. }
  30. }
  31.  
  32. #endif //SEMAPHORE_SEMAPHORE_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement