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_(0),
- upper_semaphore_(capacity),
- like_mutex_(1) {
- }
- void Put(T value) {
- 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 TagLikeMutex {};
- struct TagLowerUpper {};
- private:
- size_t capacity_;
- std::deque<T> buffer_;
- TaggedSemaphore<TagLowerUpper> lower_semaphore_;
- TaggedSemaphore<TagLowerUpper> upper_semaphore_;
- TaggedSemaphore<TagLikeMutex> like_mutex_;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement