Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include "semaphore.hpp"
- #include <twist/support/compiler.hpp>
- #include <deque>
- namespace solutions {
- template <typename T>
- class BufferedChannel {
- public:
- explicit BufferedChannel(size_t capacity)
- : send_semaphore_(capacity), recv_semaphore_(0), mutex_(1) {
- }
- void Send(T item) {
- send_semaphore_.Acquire();
- mutex_.Acquire();
- storage_.push_back(std::move(item));
- mutex_.Release();
- recv_semaphore_.Release();
- }
- T Recv() {
- recv_semaphore_.Acquire();
- mutex_.Acquire();
- T item = storage_.front();
- storage_.pop_front();
- mutex_.Release();
- send_semaphore_.Release();
- return item;
- }
- private:
- std::deque<T> storage_;
- Semaphore send_semaphore_;
- Semaphore recv_semaphore_;
- Semaphore mutex_;
- };
- } // namespace solutions
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement