Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template<typename T>
- class Counter {
- private:
- std::atomic<T> counter_;
- twist::mutex mtx_;
- twist::conditional_variable cv_is_zero_;
- public:
- explicit Counter(T init_value = 0) : counter_(init_value) {};
- void Increment() {
- auto locker = twist::LockUnique(mtx_);
- counter_.fetch_add(1);
- if (!counter_)
- cv_is_zero_.notify_all();
- }
- void Decrement() {
- auto locker = twist::LockUnique(mtx_);
- counter_.fetch_sub(1);
- if (!counter_)
- cv_is_zero_.notify_all();
- }
- 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