Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include "tagged_semaphore.hpp"
- #include <deque>
- // Bounded Blocking Multi-Producer/Multi-Consumer (MPMC) Queue
- template <typename T>
- class BoundedBlockingQueue {
- public:
- explicit BoundedBlockingQueue(size_t capacity)
- : capacity_(capacity),
- lower_semaphore_(capacity),
- upper_semaphore_(capacity),
- like_mutex_(1) {}
- void Put(T value) {
- // TaggedSemaphore<Tag>::Token token(std::move(upper_semaphore_.Acquire())); ?????
- auto token(std::move(upper_semaphore_.Acquire()));
- auto lock(std::move(like_mutex_.Acquire()));
- buffer_.push_back(std::move(value));
- like_mutex_.Release(std::move(lock)); // нужен ли тут мув?
- lower_semaphore_.Release(std::move(token));
- }
- T Take() {
- auto token(std::move(lower_semaphore_.Acquire()));
- auto lock(std::move(like_mutex_.Acquire()));
- T front(std::move(buffer_.front()));
- buffer_.pop_front();
- like_mutex_.Release(std::move(lock));
- upper_semaphore_.Release(std::move(token));
- return front;
- }
- private:
- // Tags
- struct LowerBoundTag {};
- struct UpperBoundTag {};
- struct Tag {};
- private:
- size_t capacity_;
- std::deque<T> buffer_;
- TaggedSemaphore<Tag> lower_semaphore_;
- TaggedSemaphore<Tag> upper_semaphore_;
- TaggedSemaphore<Tag> like_mutex_;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement