Menu

Exception safety in an SPSC ring: a batch push rolls back, a batch pop cannot
📰

Exception safety in an SPSC ring: a batch push rolls back, a batch pop cannot

DEV Community: cpp·polycratia·14 days ago
#bMaQIPSY
#devtocpp#devto#dev#element#queue
Long article (2172 words) — no AI summary yet.
Reading 0:00
15s threshold

A batch push into a ring buffer and a batch pop out of it look like mirror images, and for exception safety they are not. If the element type can throw while it is being constructed or moved, the push can act as though the batch never happened, while the pop cannot. That is not a matter of taste. At the instant of the throw, the two ends of the queue disagree about who owns the element, and the recovery has to follow the ownership instead of the symmetry.

I keep a single-producer single-consumer queue, spscring , as a written-down reference: the ordinary queue with the reasoning attached rather than a cleverer one. Its two batch operations, try_push_n and try_pop_n , are both members of detail::ring_core in include/spscring/spscring.hpp . They sit a few dozen lines apart and they recover in opposite directions.

The protocol the recovery has to fit into

Two counters, never reset. write_ advances on a push, read_ on a pop, and a slot is the counter masked down to the capacity.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.