17 #ifndef COM_BORA_SOFTWARE__BALAU_CONTAINER__ARRAY_BLOCKING_QUEUE 18 #define COM_BORA_SOFTWARE__BALAU_CONTAINER__ARRAY_BLOCKING_QUEUE 23 #include <condition_variable> 45 public:
void enqueue(T && element)
override {
46 std::unique_lock<std::mutex> lock(mutex);
49 enqueueCondition.wait(lock);
52 elements[head] = std::move(element);
58 dequeueCondition.notify_one();
62 std::unique_lock<std::mutex> lock(mutex);
65 dequeueCondition.wait(lock);
68 T element = std::move(elements[tail]);
74 enqueueCondition.notify_one();
79 return tryDequeue(std::chrono::milliseconds(0));
82 public: T
tryDequeue(std::chrono::milliseconds waitTime)
override {
83 std::unique_lock<std::mutex> lock(mutex);
86 dequeueCondition.wait_for(lock, waitTime);
93 T element = std::move(elements[tail]);
99 enqueueCondition.notify_one();
103 public:
bool full()
const override {
104 return count == elements.size();
107 public:
bool empty()
const override {
113 private:
void increment(
size_t & ptr) {
115 ptr = ptr - (ptr == elements.size()) * elements.size();
118 private: std::vector<T> elements;
119 private:
size_t count;
120 private:
size_t head;
121 private:
size_t tail;
122 private: std::mutex mutex;
123 private: std::condition_variable enqueueCondition;
124 private: std::condition_variable dequeueCondition;
129 #endif // COM_BORA_SOFTWARE__BALAU_CONTAINER__ARRAY_BLOCKING_QUEUE Base interface for blocking queues.
bool full() const override
Returns true if the queue is full.
Definition: ArrayBlockingQueue.hpp:103
Various container classes, apart from interprocess containers.
Definition: ArrayBlockingQueue.hpp:25
void enqueue(T &&element) override
Enqueue an object, waiting for space to be available if the queue is full.
Definition: ArrayBlockingQueue.hpp:45
T tryDequeue(std::chrono::milliseconds waitTime) override
Try to dequeue an object, waiting for the specified time if the queue is empty.
Definition: ArrayBlockingQueue.hpp:82
Base interface for blocking queues.
Definition: BlockingQueue.hpp:29
Core includes, typedefs and functions.
T dequeue() override
Dequeue an object, waiting for an object to become available if the queue is empty.
Definition: ArrayBlockingQueue.hpp:61
bool empty() const override
Returns true if the queue is empty.
Definition: ArrayBlockingQueue.hpp:107
ArrayBlockingQueue(unsigned int capacity)
Create an array blocking queue with the specified capacity.
Definition: ArrayBlockingQueue.hpp:39
A blocking queue that uses wait/notify and an array to hold the elements.
Definition: ArrayBlockingQueue.hpp:35
T tryDequeue() override
Try to dequeue an object.
Definition: ArrayBlockingQueue.hpp:78