17#include <teiacare/sdk/non_copyable.hpp>
18#include <teiacare/sdk/non_moveable.hpp>
21#include <condition_variable>
67 std::unique_lock
lock(_mutex);
69 _last_item_popped.wait(
lock, [
this] {
return !is_full(); });
72 push_impl(std::move(
lock));
84 std::unique_lock
lock(_mutex);
86 _last_item_popped.wait(
lock, [
this] {
return !is_full(); });
89 push_impl(std::move(
lock));
102 std::unique_lock
lock(_mutex);
107 push_impl(std::move(
lock));
123 std::unique_lock
lock(_mutex);
127 _queue.emplace(
item);
128 push_impl(std::move(
lock));
141 std::unique_lock
lock(_mutex);
143 _first_item_pushed.wait(
lock, [
this] {
return !is_empty(); });
145 T
item = _queue.front();
147 pop_impl(std::move(
lock));
161 std::unique_lock
lock(_mutex);
165 T
item = _queue.front();
167 pop_impl(std::move(
lock));
169 return std::move(std::optional(
item));
178 std::lock_guard
lock(_mutex);
179 return _queue.size();
188 std::lock_guard
lock(_mutex);
199 std::lock_guard
lock(_mutex);
203 while (!_queue.empty())
206 _last_item_popped.notify_all();
207 _first_item_pushed.notify_all();
211 std::queue<T> _queue;
212 mutable std::mutex _mutex;
213 std::condition_variable _last_item_popped;
214 std::condition_variable _first_item_pushed;
215 const size_t _capacity;
217 inline void push_impl(std::unique_lock<std::mutex>&&
lock)
223 _first_item_pushed.notify_all();
226 inline void pop_impl(std::unique_lock<std::mutex>&& lock)
228 const bool is_last_item_popped = _queue.
size() == _capacity - 1;
231 if (is_last_item_popped)
232 _last_item_popped.notify_all();
235 inline bool is_empty()
const
237 return _queue.empty();
240 inline bool is_full()
const
242 return _queue.
size() >= _capacity;
Thread safe, blocking queue.
std::optional< T > try_pop()
Try to retrieve an item from the queue.
void clear()
Clear the queue.
bool try_push(T &&item)
Try to insert an item into the queue.
blocking_queue(size_t capacity)
Constructor.
size_t capacity() const
Get the maximum number of items that the queue can hold.
void push(T &&item)
Insert an item into the queue.
T pop()
Retrieve an item from the queue.
bool try_push(const T &item)
Try to insert an item into the queue.
void push(const T &item)
Insert an item into the queue.
~blocking_queue()=default
Destructor.
size_t size() const
Get the number of items currently in the queue.
Utility class expected to be used as a base class to define non-copyable classes.
Utility class expected to be used as a base class to define non-moveable classes.