A thread-safe, optionally bounded queue with blocking push and pop operations.
More...
|
| SafeQueue (size_t max_size=0) |
| Constructs a SafeQueue with an optional maximum size.
|
|
| SafeQueue (const SafeQueue &)=delete |
| Deleted copy constructor to prevent copying.
|
|
SafeQueue & | operator= (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.
|
|
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
-
T | The type of elements stored in the queue. Must be movable or copyable. |
- Note
- The queue disables copying to avoid concurrent ownership issues.
std::thread producer([&queue]() {
for (int i = 0; i < 20; ++i) {
queue.push(std::move(i));
}
queue.producerFinished();
});
std::thread consumer([&queue]() {
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 at line 65 of file queue.hpp.