|
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 an optional capacity limit. | |
| ThreadSafeQueue (const ThreadSafeQueue &)=delete | |
| ThreadSafeQueue & | operator= (const ThreadSafeQueue &)=delete |
| void | abort () |
| Abort the queue and unblock all waiting threads. | |
| bool | aborted () const |
| Check whether the queue has been aborted. | |
| bool | push (T value) |
| Push an element into the queue, blocking if necessary. | |
| bool | try_push (T value) |
| Attempt to push an element into the queue without blocking. | |
| void | wait_for_data () |
| Block until the queue contains at least one element or is aborted. | |
| void | notify_all () |
| Wake all threads currently waiting on the queue. | |
| std::optional< T > | wait_and_pop () |
| Wait for and remove the front element from the queue. | |
| std::optional< T > | try_pop () |
| Attempt to remove and return the front element without blocking. | |
| bool | empty () const |
| Check whether the queue is empty. | |
| size_t | size () const |
| Get the current number of elements in the queue. | |
| size_t | capacity () const |
| Get the maximum number of elements the queue can hold. | |
| void | enqueue (T value) |
| Enqueue an element into the queue (blocking if necessary). | |
| bool | try_enqueue (T value) |
| Attempt to enqueue an element into the queue without blocking. | |
| template<typename... Args> | |
| bool | emplace (Args &&... args) |
| Construct and insert an element into the queue in place. | |
| template<typename... Args> | |
| bool | try_emplace (Args &&... args) |
| Attempt to construct and insert an element into the queue without blocking. | |
| std::optional< T > | wait_dequeue () |
| Wait for and dequeue an element from the queue. | |
| bool | try_dequeue (T &out) |
| Attempt to dequeue an element without blocking. | |
| std::optional< T > | try_dequeue () |
| Attempt to dequeue an element without blocking. | |
| bool | peek (T &out) const |
| Peek at the front element without removing it. | |
| std::optional< T > | peek () const |
| Peek at the front element without removing it. | |
| template<typename Predicate> | |
| void | removeIf (Predicate pred) |
| Remove all elements that satisfy a given predicate. | |
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 an optional capacity limit.
|
delete |
|
inline |
Abort the queue and unblock all waiting threads.
Sets the queue into an aborted (shutdown) state and wakes all threads currently blocked in:
After calling abort():
This function is idempotent and may be called multiple times safely. It does not clear or modify the contents of the queue.
|
inline |
Check whether the queue has been aborted.
Returns true if abort() has been called on this queue.
|
inline |
Get the maximum number of elements the queue can hold.
|
inline |
Construct and insert an element into the queue in place.
This function forwards the given arguments to construct the element directly in the queue, avoiding unnecessary copies or moves.
If the queue is bounded and currently full, this call blocks until space becomes available or the queue is aborted.
After successfully inserting the element, one waiting consumer (if any) is notified.
The operation fails and returns false if the queue has been aborted before insertion.
| Args | Types of arguments to forward to T's constructor. |
| args | Arguments forwarded to construct the element in place. |
|
inline |
Check whether the queue is empty.
This function is thread-safe and returns immediately.
|
inline |
Enqueue an element into the queue (blocking if necessary).
This is a Moodycamel-compatible alias for push(). If the queue is bounded and full, this call will block until space becomes available or the queue is aborted.
After the element is successfully enqueued, one waiting consumer (if any) is notified.
| value | The element to enqueue. It will be moved into the queue. |
|
inline |
Wake all threads currently waiting on the queue.
Notifies all threads blocked on:
This function does not modify the contents of the queue. It is typically used during shutdown or coordination to unblock threads.
|
delete |
|
inline |
Peek at the front element without removing it.
This function allows you to inspect the front element of the queue without modifying the queue.
The operation returns immediately:
|
inline |
Peek at the front element without removing it.
This function allows you to inspect the front element of the queue without modifying the queue.
The operation returns immediately:
| [out] | out | Reference where the front element will be stored if available. |
|
inline |
Push an element into the queue, blocking if necessary.
If the queue is bounded and currently full, this call will block until space becomes available or the queue is aborted.
After the element is successfully pushed, one waiting consumer (if any) is notified.
The operation fails and returns false if the queue has been aborted via abort() before insertion.
| value | The value to push into the queue. It will be moved. |
|
inline |
Remove all elements that satisfy a given predicate.
Iterates over all elements in the queue and removes those for which the predicate returns true. Elements that do not satisfy the predicate are kept in the same order.
| Predicate | A callable type that takes a reference to an element of type T and returns bool. Elements for which the predicate returns true are removed. |
| pred | Predicate function used to determine which elements to remove. |
|
inline |
Get the current number of elements in the queue.
This function is thread-safe and returns immediately.
|
inline |
Attempt to dequeue an element without blocking.
This is a Moodycamel-compatible alias for try_pop(). The operation does not block. If the queue is empty, the function returns immediately with std::nullopt.
After a successful dequeue, one waiting producer (if any) is notified in case the queue is bounded.
|
inline |
Attempt to dequeue an element without blocking.
This is a Moodycamel-compatible alias for try_pop(). The operation does not block. If the queue is empty, the function returns immediately with false.
If an element is successfully dequeued, it is stored in out. After removal, one waiting producer (if any) is notified in case the queue is bounded.
| [out] | out | Reference where the dequeued element will be stored if available. |
|
inline |
Attempt to construct and insert an element into the queue without blocking.
Constructs the element in place using the provided arguments. The operation does not block under any circumstances.
The operation fails and returns false if:
After successfully inserting the element, one waiting consumer (if any) is notified.
| Args | Types of arguments to forward to T's constructor. |
| args | Arguments forwarded to construct the element in place. |
|
inline |
Attempt to enqueue an element into the queue without blocking.
This is a Moodycamel-compatible alias for try_push(). The operation fails and returns false if:
If the element is successfully enqueued, one waiting consumer (if any) is notified.
| value | The element to enqueue. It will be moved into the queue. |
|
inline |
Attempt to remove and return the front element without blocking.
This function checks the queue immediately:
After a successful pop, one waiting producer (if any) is notified in case the queue is bounded.
|
inline |
Attempt to push an element into the queue without blocking.
This function tries to insert the given element immediately. It does not block under any circumstances.
The operation fails and returns false if:
If the element is successfully inserted, one waiting consumer (if any) is notified.
| value | The value to push into the queue. It will be moved. |
|
inline |
Wait for and remove the front element from the queue.
This function blocks until:
If the queue is aborted and empty, this function returns std::nullopt. Otherwise, it removes and returns the front element. After removal, one waiting producer (if any) is notified in case the queue was bounded.
|
inline |
Wait for and dequeue an element from the queue.
This is a Moodycamel-compatible alias for wait_and_pop(). The function blocks until:
If the queue is aborted and empty, this function returns std::nullopt. Otherwise, it removes and returns the front element. After removal, one waiting producer (if any) is notified in case the queue is bounded.
|
inline |
Block until the queue contains at least one element or is aborted.
This function acquires the internal mutex and waits on the condition variable until either:
After returning, the caller can safely check the queue for available data.