23#ifndef BM_BM_SIM_QUEUE_H_
24#define BM_BM_SIM_QUEUE_H_
26#include <condition_variable>
66 : capacity(capacity), wb(wb), rb(rb) {}
70 std::unique_lock<std::mutex> lock(q_mutex);
71 while (!is_not_full()) {
73 q_not_full.wait(lock);
75 queue.push_front(item);
77 q_not_empty.notify_one();
82 std::unique_lock<std::mutex> lock(q_mutex);
83 while (!is_not_full()) {
85 q_not_full.wait(lock);
87 queue.push_front(std::move(item));
89 q_not_empty.notify_one();
94 std::unique_lock<std::mutex> lock(q_mutex);
95 while (!is_not_empty())
96 q_not_empty.wait(lock);
97 *pItem = std::move(queue.back());
100 q_not_full.notify_one();
105 std::unique_lock<std::mutex> lock(q_mutex);
112 std::unique_lock<std::mutex> lock(q_mutex);
127 bool is_not_empty()
const {
return queue.size() > 0; }
128 bool is_not_full()
const {
return queue.size() < capacity; }
135 mutable std::mutex q_mutex;
136 mutable std::condition_variable q_not_empty;
137 mutable std::condition_variable q_not_full;
Queue & operator=(const Queue &)=delete
Deleted copy assignment operator.
void set_capacity(const size_t c)
Change the capacity of the queue.
Definition queue.h:110
Queue(const Queue &)=delete
Deleted copy constructor.
ReadBehavior
Implementation behavior when an element is popped from an empty queue.
Definition queue.h:52
@ ReadReturn
not implemented yet
Definition queue.h:56
@ ReadBlock
block and wait until the queue becomes non-empty
Definition queue.h:54
size_t size() const
Get queue occupancy.
Definition queue.h:104
void push_front(const T &item)
Makes a copy of item and pushes it to the front of the queue.
Definition queue.h:69
WriteBehavior
Implementation behavior when an item is pushed to a full queue.
Definition queue.h:45
@ WriteBlock
block and wait until a slot is available
Definition queue.h:47
@ WriteReturn
return immediately
Definition queue.h:49
Queue(Queue &&)=delete
Deleted move constructor (class includes mutex)
void pop_back(T *pItem)
Pops an element from the back of the queue: moves the element to *pItem.
Definition queue.h:93
Queue(size_t capacity, WriteBehavior wb=WriteBlock, ReadBehavior rb=ReadBlock)
Constructs a queue with specified capacity and read / write behaviors.
Definition queue.h:64
void push_front(T &&item)
Moves item to the front of the queue.
Definition queue.h:81