19#ifndef ICEFLOW_CORE_RINGBUFFER_HPP
20#define ICEFLOW_CORE_RINGBUFFER_HPP
22#include <condition_variable>
24#include "boost/circular_buffer.hpp"
58 std::lock_guard<std::mutex> lock(other.m_mutex);
59 m_queue = other.m_queue;
63 std::lock_guard<std::mutex> lock(m_mutex);
64 int x =
static_cast<int>(m_queue.size());
73 void push(
const T &value) {
75 std::unique_lock<std::mutex> lock(m_mutex);
76 m_queue.push_back(value);
79 m_queueCondition.notify_all();
81 void pushData(T &value,
int threshold) {
83 std::unique_lock<std::mutex> lock(m_mutex);
84 while (m_queue.size() >= threshold) {
85 m_queueCondition.wait(lock);
87 m_queue.push_back(value);
90 m_queueCondition.notify_all();
99 std::lock_guard<std::mutex> lock(m_mutex);
100 return m_queue.empty();
113 std::lock_guard<std::mutex> lock(m_mutex);
116 value = m_queue.front();
130 std::lock_guard<std::mutex> lock(m_mutex);
132 return std::shared_ptr<T>();
133 std::shared_ptr<T> value = std::make_shared<T>(m_queue.front());
146 std::unique_lock<std::mutex> lock(m_mutex);
147 m_queueCondition.wait(lock, [
this] {
return !m_queue.empty(); });
148 value = m_queue.front();
161 std::unique_lock<std::mutex> lock(m_mutex);
162 m_queueCondition.wait(lock, [
this] {
return !m_queue.empty(); });
163 std::shared_ptr<T> value = std::make_shared<T>(m_queue.front());
176 std::unique_lock<std::mutex> lock(m_mutex);
177 m_queueCondition.wait(lock, [
this] {
return !m_queue.empty(); });
178 T value = m_queue.front();
181 m_queueCondition.notify_all();
195 std::unique_lock<std::mutex> lock(m_mutex);
196 if (m_queueCondition.wait_for(lock, waitDuration,
197 [
this] { return !m_queue.empty(); })) {
199 value = m_queue.front();
216 std::unique_lock<std::mutex> lock(m_mutex);
217 if (m_queueCondition.wait_for(lock, waitDuration,
218 [
this] { return !m_queue.empty(); })) {
220 std::shared_ptr<T> value = std::make_shared<T>(m_queue.front());
225 return std::shared_ptr<T>();
229 mutable std::mutex m_mutex;
230 boost::circular_buffer<T> m_queue;
232 std::condition_variable m_queueCondition;
This class provides a thread safe implementation of a queue. It uses boost::circular_buffer<T> as an ...
Definition ringbuffer.hpp:37
void push(const T &value)
Pushes a value to the queue.
Definition ringbuffer.hpp:73
bool waitForAndPop(T &value, std::chrono::milliseconds waitDuration)
Waits and pops an item from the queue and copies it to value passed in as an argument....
Definition ringbuffer.hpp:194
T waitAndPopValue()
Waits and pops an item from the queue and returns a copy of the item This is a blocking call and it w...
Definition ringbuffer.hpp:175
std::shared_ptr< T > waitAndPop()
Waits and pops an item from the queue and returns a shared_ptr<T> This is a blocking call and it wait...
Definition ringbuffer.hpp:160
RingBuffer(RingBuffer const &other)
Copy constructor of the RingBuffer class. This enables an instance of this class to be passed around ...
Definition ringbuffer.hpp:57
bool empty() const
Checks if the queue is empty.
Definition ringbuffer.hpp:98
std::shared_ptr< T > waitForAndPop(std::chrono::milliseconds waitDuration)
Waits and pops an item from the queue and returns a std::shared_ptr<T>. This is a blocking call with ...
Definition ringbuffer.hpp:215
bool tryAndPop(T &value)
Tries to pop a value from the queue and copy it to the value passed in as an argument....
Definition ringbuffer.hpp:112
RingBuffer()
Constructor of the RingBuffer class.
Definition ringbuffer.hpp:44
std::shared_ptr< T > tryAndPop()
Tries to pop an item from the queue and returns a std::shared_ptr<T> This is a non blocking call and ...
Definition ringbuffer.hpp:129
void waitAndPop(T &value)
Waits and pops an item from the queue and copies it to value passed in as an argument This is a block...
Definition ringbuffer.hpp:145
RingBuffer & operator=(const RingBuffer &)=delete
Copy assignment.