A bounded multi-producer multi-consumer concurrent queue for C++20. Build with CMake 3.24 or newer.
-
MPMCQueue<T, Allocator>(std::size_t capacity, const Allocator &allocator = Allocator());
Constructs a queue holding items of type T with the given capacity.
The default allocator is std::allocator<T>. Custom allocators must rebind
to the queue's private slot type, support its alignment, and use raw
pointers after rebinding. The queue uses the allocator for backing storage
and manages element construction and destruction directly.
Allocators that previously named rigtorp::mpmc::Slot<T> should now be
supplied as a rebindable allocator for T. Use rigtorp::MPMCQueue
as the queue type.
-
void emplace(Args &&... args);
Enqueue an item using inplace construction. Blocks if queue is full.
-
bool try_emplace(Args &&... args);
Try to enqueue an item using inplace construction. Returns true on
success and false if queue is full.
-
void push(const T &v);
Enqueue an item using copy construction. Blocks if queue is full.
-
template <typename P> void push(P &&v);
Enqueue an item using move construction. Participates in overload
resolution only if std::is_nothrow_constructible_v<T, P&&>. Blocks if queue is full.
-
bool try_push(const T &v);
Try to enqueue an item using copy construction. Returns true on
success and false if queue is full.
-
template <typename P> bool try_push(P &&v);
Try to enqueue an item using move construction. Participates in
overload resolution only if std::is_nothrow_constructible_v<T, P&&>. Returns true on success and false if queue
is full.
-
void pop(T &v);
Dequeue an item by copying or moving the item into v. Blocks if
queue is empty.
-
bool try_pop(T &v);
Try to dequeue an item by copying or moving the item into
v. Return true on sucess and false if the queue is empty.
-
template <typename Consumer> bool try_consume(Consumer &&consumer);
Tries to dequeue an item and invoke the consumer on it as an rvalue.
Uses std::invoke so the consumer can also be a pointer to a member. The
invocation must be non-throwing. Returns true on success and false if the queue is empty.
-
ssize_t size();
Returns the number of elements in the queue.
The size can be negative when the queue is empty and there is at least one
reader waiting. Since this is a concurrent queue the size is only a best
effort guess until all reader and writer threads have been joined.
-
bool empty();
Returns true if the queue is empty.
Since this is a concurrent queue this is only a best effort guess until all
reader and writer threads have been joined.
All operations except construction and destruction are thread safe.
Testing concurrency algorithms is hard. I'm using two approaches to test the
implementation:
To check MC/DC coverage of the queue header, use GCC 14 or newer with its
matching gcov and a single-config CMake generator:
CTest clears the previous profile before each run. The check combines condition
outcomes across template instantiations and fails on uncovered code or outcomes.