Motion Master
Loading...
Searching...
No Matches
mm::core::containers::ThreadSafeQueue< T > Class Template Reference

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
ThreadSafeQueueoperator= (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.
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.
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.

Detailed Description

template<typename T>
class mm::core::containers::ThreadSafeQueue< T >

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.

Template Parameters
TType of elements stored in the queue.

Constructor & Destructor Documentation

◆ ThreadSafeQueue() [1/2]

template<typename T>
mm::core::containers::ThreadSafeQueue< T >::ThreadSafeQueue ( size_t capacity = 0)
inlineexplicit

Construct a queue with optional capacity.

◆ ThreadSafeQueue() [2/2]

template<typename T>
mm::core::containers::ThreadSafeQueue< T >::ThreadSafeQueue ( const ThreadSafeQueue< T > & )
delete

Member Function Documentation

◆ capacity()

template<typename T>
size_t mm::core::containers::ThreadSafeQueue< T >::capacity ( ) const
inline

Get the maximum capacity of the queue.

Returns
The maximum number of items allowed in the queue. Returns 0 if the queue is unbounded.

◆ emplace()

template<typename T>
template<typename... Args>
void mm::core::containers::ThreadSafeQueue< T >::emplace ( Args &&... args)
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.

Template Parameters
ArgsTypes of arguments to forward to T's constructor.
Parameters
argsArguments forwarded to construct the value in place.

◆ empty()

template<typename T>
bool mm::core::containers::ThreadSafeQueue< T >::empty ( ) const
inline

Check whether the queue is empty.

This function is thread-safe and returns immediately.

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

◆ enqueue()

template<typename T>
void mm::core::containers::ThreadSafeQueue< T >::enqueue ( T value)
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.

Parameters
valueThe value to enqueue. It will be moved into the queue.

◆ operator=()

template<typename T>
ThreadSafeQueue & mm::core::containers::ThreadSafeQueue< T >::operator= ( const ThreadSafeQueue< T > & )
delete

◆ peek() [1/2]

template<typename T>
std::optional< T > mm::core::containers::ThreadSafeQueue< T >::peek ( ) const
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>.

Returns
An std::optional<T> containing the front item, or std::nullopt if the queue is empty.

◆ peek() [2/2]

template<typename T>
bool mm::core::containers::ThreadSafeQueue< T >::peek ( T & out) const
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.

Parameters
[out]outReference where the front item will be stored if available.
Returns
true if an item was available, false if the queue was empty.

◆ push()

template<typename T>
void mm::core::containers::ThreadSafeQueue< T >::push ( T value)
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.

Parameters
valueThe value to push into the queue. It will be moved.

◆ size()

template<typename T>
size_t mm::core::containers::ThreadSafeQueue< T >::size ( ) const
inline

Get the current number of items in the queue.

This function is thread-safe and returns immediately.

Returns
The number of items currently in the queue.

◆ try_dequeue() [1/2]

template<typename T>
std::optional< T > mm::core::containers::ThreadSafeQueue< T >::try_dequeue ( )
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.

Returns
An std::optional<T> containing the dequeued value, or std::nullopt if empty.

◆ try_dequeue() [2/2]

template<typename T>
bool mm::core::containers::ThreadSafeQueue< T >::try_dequeue ( T & out)
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.

Parameters
[out]outReference where the dequeued value will be stored if available.
Returns
true if a value was successfully dequeued, false if the queue was empty.

◆ try_emplace()

template<typename T>
template<typename... Args>
bool mm::core::containers::ThreadSafeQueue< T >::try_emplace ( Args &&... args)
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.

Template Parameters
ArgsTypes of arguments to forward to T's constructor.
Parameters
argsArguments forwarded to construct the value in place.
Returns
true if the value was successfully emplaced, false if the queue was full.

◆ try_enqueue()

template<typename T>
bool mm::core::containers::ThreadSafeQueue< T >::try_enqueue ( T value)
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.

Parameters
valueThe value to enqueue. It will be moved into the queue.
Returns
true if the value was successfully enqueued, false if the queue was full.

◆ try_pop()

template<typename T>
std::optional< T > mm::core::containers::ThreadSafeQueue< T >::try_pop ( )
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.

Returns
An std::optional<T> containing the popped value, or std::nullopt if empty.

◆ try_push()

template<typename T>
bool mm::core::containers::ThreadSafeQueue< T >::try_push ( T value)
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.

Parameters
valueThe value to push into the queue. It will be moved.
Returns
true if the value was successfully pushed, false if the queue was full.

◆ wait_and_pop()

template<typename T>
T mm::core::containers::ThreadSafeQueue< T >::wait_and_pop ( )
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.

Returns
The value popped from the queue.

◆ wait_dequeue()

template<typename T>
T mm::core::containers::ThreadSafeQueue< T >::wait_dequeue ( )
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.

Returns
The value dequeued from the queue.

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