Advertisement
Guest User

Untitled

a guest
May 25th, 2019
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. #include <atomic>
  2. #include <mutex>
  3. class Counter {
  4. private:
  5. std::atomic<int> counter_;
  6. twist::mutex mtx_;
  7. twist::conditional_variable cv_is_zero_;
  8. public:
  9. explicit Counter(int init_value = 0) : counter_(init_value) {};
  10.  
  11. void Increment() {
  12. auto locker = twist::LockUnique(mtx_);
  13. counter_.fetch_add(1);
  14. cv_is_zero_.notify_one(mtx_);
  15. }
  16.  
  17. void Decrement() {
  18. auto locker = twist::LockUnique(mtx_);
  19. counter_.fetch_add(-1);
  20. cv_is_zero_.notify_one(mtx_);
  21. }
  22.  
  23. void AwaitZero() {
  24. auto locker = twist::LockUnique(mtx_);
  25. while (!counter_ == 0) {
  26. cv_is_zero_.wait(locker);
  27. }
  28. }
  29. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement