IRSOL
C++ code implementing socket server for interacting with Baumer camera.
irsol::utils::SafeQueue< T > Class Template Reference

A thread-safe, optionally bounded queue with blocking push and pop operations. More...

#include <queue.hpp>

Public Member Functions

 SafeQueue (size_t max_size=0)
 Constructs a SafeQueue with an optional maximum size.
 
 SafeQueue (const SafeQueue &)=delete
 Deleted copy constructor to prevent copying.
 
SafeQueueoperator= (const SafeQueue &)=delete
 Deleted copy assignment operator to prevent copying.
 
void push (T &&item)
 Push an item into the queue.
 
bool pop (T &out)
 Pop an item from the queue.
 
void producerFinished ()
 Signals that the producer has finished producing items.
 
size_t size () const
 Returns the current size of the queue.
 
bool full () const
 Checks if the queue is full.
 
bool empty () const
 Checks if the queue is empty.
 
bool done () const
 Returns whether the queue is marked done.
 

Private Attributes

std::mutex m_mutex
 Mutex protecting the queue and state.
 
std::condition_variable m_producerConditionVariable
 Condition variable for producer blocking.
 
std::condition_variable m_consumerConditionVariable
 Condition variable for consumer blocking.
 
std::queue< T > m_queue
 Underlying queue holding the data.
 
size_t m_maxSize
 Maximum queue size (0 means unbounded).
 
bool m_done
 Flag indicating the queue is done.
 

Detailed Description

template<typename T>
class irsol::utils::SafeQueue< T >

A thread-safe, optionally bounded queue with blocking push and pop operations.

This template class implements a producer-consumer queue protected by mutex and condition variables to safely share data between threads. The queue can be bounded by specifying a maximum size or unbounded by leaving the default max_size parameter as 0.

The queue supports:

  • Blocking push: waits when full (if bounded) until space becomes available.
  • Blocking pop: waits when empty until an item is available or the queue is marked done.
  • Notification when the producer finishes to unblock consumers.
Template Parameters
TThe type of elements stored in the queue. Must be movable or copyable.
Note
The queue disables copying to avoid concurrent ownership issues.
irsol::utils::SafeQueue<int> queue(10); // bounded queue with max size 10
// Producer thread
std::thread producer([&queue]() {
for (int i = 0; i < 20; ++i) {
queue.push(std::move(i));
}
queue.producerFinished();
});
// Consumer thread
std::thread consumer([&queue]() {
int value;
while (queue.pop(value)) {
std::cout << "Got value: " << value << std::endl;
}
});
producer.join();
consumer.join();
A thread-safe, optionally bounded queue with blocking push and pop operations.
Definition queue.hpp:66
auto value

Definition at line 65 of file queue.hpp.

Constructor & Destructor Documentation

◆ SafeQueue() [1/2]

template<typename T >
irsol::utils::SafeQueue< T >::SafeQueue ( size_t  max_size = 0)
inlineexplicit

Constructs a SafeQueue with an optional maximum size.

Parameters
max_sizeThe maximum number of elements the queue can hold. Zero (default) means the queue is unbounded.

Definition at line 73 of file queue.hpp.

73: m_maxSize(max_size), m_done(false) {}
bool m_done
Flag indicating the queue is done.
Definition queue.hpp:208
size_t m_maxSize
Maximum queue size (0 means unbounded).
Definition queue.hpp:207

◆ SafeQueue() [2/2]

template<typename T >
irsol::utils::SafeQueue< T >::SafeQueue ( const SafeQueue< T > &  )
delete

Deleted copy constructor to prevent copying.

Member Function Documentation

◆ done()

template<typename T >
bool irsol::utils::SafeQueue< T >::done ( ) const
inline

Returns whether the queue is marked done.

Returns
true if the producer has called producerFinished(), otherwise false.

Definition at line 195 of file queue.hpp.

196 {
197 return m_done;
198 }

◆ empty()

template<typename T >
bool irsol::utils::SafeQueue< T >::empty ( ) const
inline

Checks if the queue is empty.

Returns
true if the queue contains no items, otherwise false.

Definition at line 185 of file queue.hpp.

186 {
187 std::scoped_lock<std::mutex> lock(m_mutex);
188 return m_queue.empty();
189 }
std::queue< T > m_queue
Underlying queue holding the data.
Definition queue.hpp:206
std::mutex m_mutex
Mutex protecting the queue and state.
Definition queue.hpp:201

◆ full()

template<typename T >
bool irsol::utils::SafeQueue< T >::full ( ) const
inline

Checks if the queue is full.

Returns
true if the queue has reached its maximum size (only if bounded). false if unbounded or not full.

Definition at line 173 of file queue.hpp.

174 {
175 if(m_maxSize == 0) {
176 return false;
177 }
178 return m_queue.size() >= m_maxSize;
179 }

◆ operator=()

template<typename T >
SafeQueue & irsol::utils::SafeQueue< T >::operator= ( const SafeQueue< T > &  )
delete

Deleted copy assignment operator to prevent copying.

◆ pop()

template<typename T >
bool irsol::utils::SafeQueue< T >::pop ( T &  out)
inline

Pop an item from the queue.

Blocks if the queue is empty until an item becomes available or the queue is marked done.

Parameters
outReference to a variable where the popped item will be stored.
Returns
true if an item was successfully popped, false if the queue is done and empty.
Exceptions
irsol::AssertionExceptionerror if called after the queue is marked done.
Note
The popped item is moved to the provided output variable.

Definition at line 122 of file queue.hpp.

123 {
124 std::unique_lock<std::mutex> lock(m_mutex);
125
126 IRSOL_ASSERT_ERROR(!m_done, "SafeQueue::pop() called on an already done queue");
127
128 m_consumerConditionVariable.wait(lock, [&]() { return m_done || !m_queue.empty(); });
129
130 if(m_queue.empty())
131 return false; // done() was called
132
133 out = std::move(m_queue.front());
134 m_queue.pop();
135 m_producerConditionVariable.notify_one();
136 return true;
137 }
std::condition_variable m_consumerConditionVariable
Condition variable for consumer blocking.
Definition queue.hpp:205
std::condition_variable m_producerConditionVariable
Condition variable for producer blocking.
Definition queue.hpp:203
#define IRSOL_ASSERT_ERROR
Error-level assertion macro.
Definition assert.hpp:134

◆ producerFinished()

template<typename T >
void irsol::utils::SafeQueue< T >::producerFinished ( )
inline

Signals that the producer has finished producing items.

After calling this method, no more items should be pushed. It will unblock any waiting consumers and producers.

Exceptions
irsol::AssertionExceptionerror if called more than once.

Definition at line 147 of file queue.hpp.

148 {
149 std::scoped_lock<std::mutex> lock(m_mutex);
150
151 IRSOL_ASSERT_ERROR(!m_done, "SafeQueue::producerFinished() called on an already done queue");
152
153 m_done = true;
154 m_consumerConditionVariable.notify_all();
155 m_producerConditionVariable.notify_all();
156 }

◆ push()

template<typename T >
void irsol::utils::SafeQueue< T >::push ( T &&  item)
inline

Push an item into the queue.

Blocks if the queue is bounded and currently full until space becomes available.

Parameters
itemAn rvalue reference to the item to push into the queue.
Exceptions
irsol::AssertionExceptionerror if the queue is marked done.
Note
This method uses move semantics to efficiently transfer ownership of the item.

Definition at line 92 of file queue.hpp.

93 {
94 std::unique_lock<std::mutex> lock(m_mutex);
95
96 IRSOL_ASSERT_ERROR(!m_done, "SafeQueue::push() called on an already done queue");
97
99 lock, [&]() { return m_done || m_maxSize == 0 || m_queue.size() < m_maxSize; });
100
101 if(m_done)
102 return;
103
104 m_queue.push(std::move(item));
105 m_consumerConditionVariable.notify_one();
106 }

◆ size()

template<typename T >
size_t irsol::utils::SafeQueue< T >::size ( ) const
inline

Returns the current size of the queue.

Returns
Number of items currently stored in the queue.

Definition at line 162 of file queue.hpp.

163 {
164 std::scoped_lock<std::mutex> lock(m_mutex);
165 return m_queue.size();
166 }

Member Data Documentation

◆ m_consumerConditionVariable

template<typename T >
std::condition_variable irsol::utils::SafeQueue< T >::m_consumerConditionVariable
private

Condition variable for consumer blocking.

Definition at line 205 of file queue.hpp.

◆ m_done

template<typename T >
bool irsol::utils::SafeQueue< T >::m_done
private

Flag indicating the queue is done.

Definition at line 208 of file queue.hpp.

◆ m_maxSize

template<typename T >
size_t irsol::utils::SafeQueue< T >::m_maxSize
private

Maximum queue size (0 means unbounded).

Definition at line 207 of file queue.hpp.

◆ m_mutex

template<typename T >
std::mutex irsol::utils::SafeQueue< T >::m_mutex
mutableprivate

Mutex protecting the queue and state.

Definition at line 201 of file queue.hpp.

◆ m_producerConditionVariable

template<typename T >
std::condition_variable irsol::utils::SafeQueue< T >::m_producerConditionVariable
private

Condition variable for producer blocking.

Definition at line 203 of file queue.hpp.

◆ m_queue

template<typename T >
std::queue<T> irsol::utils::SafeQueue< T >::m_queue
private

Underlying queue holding the data.

Definition at line 206 of file queue.hpp.


The documentation for this class was generated from the following file: