![]() |
ImFusion C++ SDK 4.5.0
|
#include <ImFusion/Stream/ThreadSafeQueue.h>
Thread-safe queue wrapper around std::queue with blocking wait support. More...
Thread-safe queue wrapper around std::queue with blocking wait support.
Provides basic queue operations with thread-safety guarantees using a mutex and condition variable. Supports both non-blocking (tryPop) and blocking (waitAndPop) pop operations.
The queue can be shut down via shutdown(), which will decline future push operations and wake all waiting threads to cause waitAndPop() to return std::nullopt once the queue is empty.
Example usage with producer and consumer on different threads:
Public Member Functions | |
| ~ThreadSafeQueue () | |
| Destructor ensures that stalling waitAndPop() calls terminate. | |
| bool | push (T value) |
| Pushes an element to the back of the queue if the queue wasn't shut down yet. | |
| std::optional< T > | tryPop () |
| Attempts to pop an element from the front of the queue without blocking. | |
| std::optional< T > | waitAndPop () |
| Blocks until an element becomes available or shutdown() is called. | |
| void | shutdown () |
| Signals shutdown and wakes all threads waiting in waitAndPop(). | |
| bool | empty () const |
| Checks if the queue is empty. | |
| int | size () const |
| Returns the number of elements in the queue. | |
Protected Member Functions | |
| int | waitingConsumers () const |
| Protected Member Functions inherited from ImFusion::Utils::NotCopyable | |
| NotCopyable (NotCopyable &&) noexcept=default | |
| NotCopyable & | operator= (NotCopyable &&) noexcept=default |
| NotCopyable (const NotCopyable &)=delete | |
| NotCopyable & | operator= (const NotCopyable &)=delete |
|
inline |
Pushes an element to the back of the queue if the queue wasn't shut down yet.
Returns if push operation was successful. Notifies one waiting thread that an element is available.
|
inline |
Attempts to pop an element from the front of the queue without blocking.
|
inline |
Blocks until an element becomes available or shutdown() is called.
|
inline |
Signals shutdown and wakes all threads waiting in waitAndPop().
After calling this, no further elements can be added to the queue and waitAndPop() will return std::nullopt for waiting threads if the queue is empty.
|
inline |
Checks if the queue is empty.
|
inline |
Returns the number of elements in the queue.