Motion Master
|
Thread-safe FIFO queue with optional capacity limit. More...
#include <thread_safe_queue.h>
Public Member Functions | |
ThreadSafeQueue (size_t capacity=0) | |
Construct a queue with optional capacity. | |
ThreadSafeQueue (const ThreadSafeQueue &)=delete | |
ThreadSafeQueue & | operator= (const ThreadSafeQueue &)=delete |
void | push (T value) |
Push a value into the queue. | |
bool | try_push (T value) |
Try to push a value into the queue without blocking. | |
T | wait_and_pop () |
Wait for and pop a value from the queue. | |
std::optional< T > | try_pop () |
Try to pop a value from the queue without blocking. | |
bool | empty () const |
Check whether the queue is empty. | |
size_t | size () const |
Get the current number of items in the queue. | |
size_t | capacity () const |
Get the maximum capacity of the queue. | |
void | enqueue (T value) |
Enqueue a value into the queue (blocking if bounded and full). | |
bool | try_enqueue (T value) |
Try to enqueue a value without blocking. | |
template<typename... Args> | |
void | emplace (Args &&... args) |
Emplace a value into the queue in place (blocking if bounded and full). | |
template<typename... Args> | |
bool | try_emplace (Args &&... args) |
Try to emplace a value into the queue in place without blocking. | |
T | wait_dequeue () |
Wait for and dequeue a value from the queue. | |
bool | try_dequeue (T &out) |
Try to dequeue a value without blocking. | |
std::optional< T > | try_dequeue () |
Try to dequeue a value without blocking. | |
bool | peek (T &out) const |
Peek at the front item without removing it. | |
std::optional< T > | peek () const |
Peek at the front item without removing it. |
Thread-safe FIFO queue with optional capacity limit.
Compatible with moodycamel::ReaderWriterQueue (refactored so that ThreadSafeQueue is used by default). Multi-producer / multi-consumer safe via a mutex, but all operations are serialized. Use lock-free queues only if very high throughput is needed.
If capacity == 0, the queue is unbounded.
T | Type of elements stored in the queue. |
|
inlineexplicit |
Construct a queue with optional capacity.
|
delete |
|
inline |
Get the maximum capacity of the queue.
|
inline |
Emplace a value into the queue in place (blocking if bounded and full).
Constructs the value directly in the queue using the provided arguments. This is a Moodycamel-compatible method. If the queue is bounded and full, the call will block until space becomes available.
Args | Types of arguments to forward to T's constructor. |
args | Arguments forwarded to construct the value in place. |
|
inline |
Check whether the queue is empty.
This function is thread-safe and returns immediately.
|
inline |
Enqueue a value into the queue (blocking if bounded and full).
This is a Moodycamel-compatible alias for push(). If the queue is bounded and full, the call will block until space becomes available.
value | The value to enqueue. It will be moved into the queue. |
|
delete |
|
inline |
Peek at the front item without removing it.
If the queue is empty, the call returns immediately with std::nullopt. Otherwise, the front item is returned as a copy inside an std::optional<T>.
|
inline |
Peek at the front item without removing it.
If the queue is empty, the call returns immediately with false. Otherwise, the front item is copied (or moved if T supports it) into out.
[out] | out | Reference where the front item will be stored if available. |
|
inline |
Push a value into the queue.
If the queue is bounded and full, this call will block until space becomes available. Once the value is added, any threads waiting to pop are notified.
value | The value to push into the queue. It will be moved. |
|
inline |
Get the current number of items in the queue.
This function is thread-safe and returns immediately.
|
inline |
Try to dequeue a value without blocking.
This is a Moodycamel-compatible alias for try_pop(). If the queue is empty, the call returns immediately with std::nullopt.
|
inline |
Try to dequeue a value without blocking.
This is a Moodycamel-compatible alias for try_pop(). If the queue is empty, the call returns immediately with false.
[out] | out | Reference where the dequeued value will be stored if available. |
|
inline |
Try to emplace a value into the queue in place without blocking.
Constructs the value directly in the queue using the provided arguments. This is a Moodycamel-compatible method. If the queue is bounded and full, the call returns immediately with false.
Args | Types of arguments to forward to T's constructor. |
args | Arguments forwarded to construct the value in place. |
|
inline |
Try to enqueue a value without blocking.
This is a Moodycamel-compatible alias for try_push(). If the queue is bounded and full, the call returns immediately with false.
value | The value to enqueue. It will be moved into the queue. |
|
inline |
Try to pop a value from the queue without blocking.
If the queue is empty, this function returns immediately with std::nullopt. Otherwise, it removes and returns the front item. Any threads waiting to push are notified after a successful pop.
|
inline |
Try to push a value into the queue without blocking.
If the queue is bounded and full, the call will return immediately with false. Otherwise, the value is added and any threads waiting to pop are notified.
value | The value to push into the queue. It will be moved. |
|
inline |
Wait for and pop a value from the queue.
If the queue is empty, this call will block until an item becomes available. Once an item is removed, any threads waiting to push are notified.
|
inline |
Wait for and dequeue a value from the queue.
This is a Moodycamel-compatible alias for wait_and_pop(). If the queue is empty, this call will block until an item becomes available.