Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <atomic>
- #include <mutex>
- class Counter {
- private:
- std::atomic<int> counter_;
- twist::mutex mtx_;
- twist::conditional_variable cv_is_zero_;
- public:
- explicit Counter(int init_value = 0) : counter_(init_value) {};
- void Increment() {
- auto locker = twist::LockUnique(mtx_);
- counter_.fetch_add(1);
- cv_is_zero_.notify_one(mtx_);
- }
- void Decrement() {
- auto locker = twist::LockUnique(mtx_);
- counter_.fetch_add(-1);
- cv_is_zero_.notify_one(mtx_);
- }
- void AwaitZero() {
- auto locker = twist::LockUnique(mtx_);
- while (!counter_ == 0) {
- cv_is_zero_.wait(locker);
- }
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement