Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <twist/stdlike/mutex.hpp>
  4. #include <twist/stdlike/condition_variable.hpp>
  5. #include <twist/support/locking.hpp>
  6.  
  7. namespace solutions {
  8.  
  9. class Semaphore {
  10. public:
  11. explicit Semaphore(const size_t count = 0) : counter_(count) {
  12. }
  13.  
  14. void Acquire() {
  15. std::unique_lock<twist::mutex> lock(mutex_);
  16. while (counter_ == 0) {
  17. is_empty_.wait(lock);
  18. }
  19. --counter_;
  20. }
  21.  
  22. void Release() {
  23. std::unique_lock<twist::mutex> lock(mutex_);
  24. ++counter_;
  25. is_empty_.notify_one();
  26. }
  27.  
  28. private:
  29. size_t counter_;
  30. twist::mutex mutex_;
  31. twist::condition_variable is_empty_;
  32. };
  33.  
  34. } // namespace solutions
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement