MPMCQueue
LibXR::MPMCQueue<T> is a bounded multi-producer / multi-consumer queue.
Compared with SPSCQueue, it is intended for more general concurrent topologies:
- multiple contexts may push;
- multiple contexts may pop;
- capacity is fixed;
- payloads are moved as byte blocks.
Basic Usage
LibXR::MPMCQueue<uint16_t> queue(32);
queue.Push(100);
uint16_t value = 0;
queue.Pop(value);
Main interfaces:
Push(const T&): returnsOK, orFULLwhen full.Pop(T&)/Pop(): returnsOK, orEMPTYwhen empty.MaxSize(),EmptySize(),ElementSize().Size(): an approximate snapshot under concurrent access.
Payload Requirements
MPMCQueue<T> requires:
Tmust be trivially copyable;Tmust be trivially destructible.
This follows from the implementation strategy: the queue moves payloads as raw byte blocks and does not manage complex object lifetime internally.
Typical Use Cases
Good fit:
- shared TX queues with multiple producers;
- public concurrent queues accessed by interrupts and threads, or by multiple threads;
- internal driver queues that need bounded concurrent behavior.
For example, a public queue passing data between tasks and threads where callers can wait or retry later. On a single core, when one context is preempted between claiming a cell and publishing it, other contexts observe transient FULL/EMPTY; when interrupts both read and write and a FULL can only drop data (for example a TX queue shared by an interrupt and a task), use SlotPool instead.
How to Choose Between SPSCQueue, MPMCQueue and SlotPool
- one producer and one consumer: use
SPSCQueue; - multiple producers or consumers: use
MPMCQueue; - interrupts read and write, a FULL can only drop data, and FULL/EMPTY should be accurate: use SlotPool, where an interrupted operation only occupies its own slot; a single enqueue or dequeue is O(1) on
MPMCQueueand O(N) onSlotPool, so large capacities, high multi-core contention and callers that can wait or retry remain a good fit forMPMCQueue.