Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <twist/stdlike/mutex.hpp>
- #include <twist/stdlike/condition_variable.hpp>
- #include <twist/support/locking.hpp>
- namespace solutions {
- class Semaphore {
- public:
- explicit Semaphore(const size_t count = 0) : counter_(count) {
- }
- void Acquire() {
- std::unique_lock<twist::mutex> lock(mutex_);
- while (counter_ == 0) {
- is_empty_.wait(lock);
- }
- --counter_;
- }
- void Release() {
- std::unique_lock<twist::mutex> lock(mutex_);
- ++counter_;
- is_empty_.notify_one();
- }
- private:
- size_t counter_;
- twist::mutex mutex_;
- twist::condition_variable is_empty_;
- };
- } // namespace solutions
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement