Class BlockingQueue
Bounded synchronized FIFO for handing work from producer threads to consumer threads.
class BlockingQueue(T)
if (isCopyable!T);
Use BlockingQueue when the queue has a fixed maximum size, producers must never wait for free capacity, and consumers should sleep while no work is available. A producer learns immediately whether a value was accepted, the queue is temporarily full, or the queue has been closed. A consumer waits until it can receive a value or until a closed queue has been fully drained.
The queue owns runtime-capacity FIFO storage and protects the complete queue state with one mutex and one condition variable. The contract permits multiple producers and multiple consumers. It is not a lock-free queue and it does not provide scheduler, retry, timeout, cancellation, or overflow policy.
close is idempotent. Closing rejects future pushes, leaves values already
queued available in FIFO order, and wakes all blocked consumers. waitPop
reports closed only after the queue is both closed and empty.
The first production contract requires copyable T. Move-only synchronized
transfer remains a separate lifetime-contract problem.
Constructors
| Name | Description |
|---|---|
this
(capacity)
|
Constructs a queue with fixed runtime capacity. |
Properties
| Name | Type | Description |
|---|---|---|
capacity[get]
|
size_t | Returns the fixed queue capacity selected at construction. |
Methods
| Name | Description |
|---|---|
close
()
|
Closes producer admission and wakes every blocked consumer. |
tryPush
(value)
|
Attempts to append one value without waiting for free capacity. |
waitPop
()
|
Waits for the next FIFO value or for final queue closure. |
Parameters
| Name | Description |
|---|---|
| T | copyable element type transferred through the queue |
Allocation
Construction may allocate the RingBuffer backing storage, Mutex, Condition,
and runtime synchronization resources. tryPush, waitPop, and close
never resize or reacquire the FIFO backing storage.
Thread Safety
The public operations may be called concurrently by multiple producer and consumer threads. The queue object itself has synchronized identity and should be shared by reference.
Notes
length, empty, full, and closed snapshots are deliberately not
part of the public API because their values may become stale immediately
after observation.
Example
A bounded work handoff queue keeps producer backpressure explicit.
// A producer never blocks for space. The caller chooses what to do when
// temporary backpressure reports full.
auto jobs = new BlockingQueue!int(2);
assert(jobs .tryPush(10) == BlockingQueuePushResult .pushed);
assert(jobs .tryPush(20) == BlockingQueuePushResult .pushed);
assert(jobs .tryPush(30) == BlockingQueuePushResult .full);
// Closing stops new work but does not discard work already accepted.
assert(jobs .close);
assert(jobs .tryPush(40) == BlockingQueuePushResult .closed);
auto first = jobs .waitPop();
auto second = jobs .waitPop();
auto done = jobs .waitPop();
assert(first .found && first .value == 10);
assert(second .found && second .value == 20);
assert(done .status == BlockingQueuePopStatus .closed);