Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <twist/ed/stdlike/mutex.hpp>
- #include <twist/ed/stdlike/condition_variable.hpp>
- #include <cstdlib>
- class Semaphore {
- public:
- explicit Semaphore(size_t tokens)
- : tokens_amount_(tokens) {
- }
- void Acquire() {
- std::unique_lock lock(mutex_);
- while (tokens_amount_ == 0) {
- have_tokens_inside_.wait(lock);
- }
- --tokens_amount_;
- }
- void Release() {
- std::lock_guard guard(mutex_);
- ++tokens_amount_;
- have_tokens_inside_.notify_one();
- }
- private:
- twist::ed::stdlike::condition_variable have_tokens_inside_;
- twist::ed::stdlike::mutex mutex_;
- size_t tokens_amount_; // protected by mutex
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement