ObzLib concurrency

obz::bounded_blocking_queue

A fixed-capacity blocking queue that gives producer-consumer code backpressure.

Backpressure As Part Of The API

A normal blocking queue is like an order rail that can keep accepting work. A bounded blocking queue is more like a small loading dock. If every space is full, new deliveries have to wait.

That waiting is not a bug. It is the safety mechanism. The queue protects the rest of the system from unbounded buildup.

obz::bounded_blocking_queue<frame> frames_to_encode(3);

frames_to_encode.push(capture_frame());

When Producers Need To Slow Down

Frame encoding is a useful example. A render thread may produce frames faster than an encoder can write them to disk. If every captured frame piles up in memory, the recording system becomes a memory problem.

frame next;
while (frames_to_encode.wait_and_pop(next)) {
    encode_frame(next);
}

With a capacity of three, the producer can get a little ahead, but not infinitely ahead. If the encoder falls behind, the producer waits for space.

Two Conditions, Two Kinds Of Waiting

The implementation names the two waiting conditions directly. Producers wait for space. Consumers wait for data.

std::condition_variable not_empty_;
std::condition_variable not_full_;

That is the main difference from blocking_queue: capacity is now part of the behaviour, not just a detail of storage.

More Libraries