Message Queue¶
| Spice | |
|---|---|
MessageQueue<T> struct¶
A thread-safe FIFO message queue for passing values between threads.
MessageQueue wraps a Queue<T> with a Mutex, so push(), pop(), tryPop() and the other methods may be called concurrently from any number of threads without external synchronization. pop() blocks until an item becomes available or the queue is closed, spin-waiting via yield() between checks, since the standard library has no condition variable to park on.
Once close() has been called, push() panics and pop()/tryPop() keep draining any items that are still queued, returning an empty Optional only once the queue has run dry. This makes close() + drain-until-empty a safe way for producers to signal shutdown to consumers.
Constructors¶
ctor¶
| Spice | |
|---|---|
Construct an empty, open message queue
Methods¶
push¶
| Spice | |
|---|---|
Push an item to the back of the queue. Safe to call from multiple threads concurrently.
Parameters
| Name | Type | Description |
|---|---|---|
item |
const T& |
Item to enqueue |
pop¶
| Spice | |
|---|---|
Retrieve and remove the item at the front of the queue, blocking until one becomes available. If the queue is closed while waiting, this drains any remaining items first and only then returns an empty Optional instead of blocking forever.
Returns: Optional<T> — The next item, or an empty Optional if the queue was closed and is now drained
tryPop¶
| Spice | |
|---|---|
Retrieve and remove the item at the front of the queue without blocking.
Returns: Optional<T> — The next item, or an empty Optional if the queue is currently empty
close¶
| Spice | |
|---|---|
Close the queue. After this call, push() panics. pop() and tryPop() keep returning any items that were queued before the close, then start reporting an empty Optional once drained.
isClosed¶
| Spice | |
|---|---|
Check whether the queue has been closed via close()
Returns: bool — Closed or not closed
isEmpty¶
| Spice | |
|---|---|
Check whether the queue currently holds no items
Returns: bool — Empty or not empty
getSize¶
| Spice | |
|---|---|
Retrieve the current number of queued items
Returns: long — Current size of the queue