Library: Foundation
Package: Core
Header: Poco/MPSCQueue.h
Description
A lock-free multi-producer single-consumer (MPSC) bounded queue.
This queue is optimized for the case where multiple threads produce items and exactly one thread consumes them.
The queue uses a bounded ring buffer with atomic operations. Producers use compare-and-swap to reserve slots; the consumer reads committed entries in order.
The queue has a fixed capacity specified at construction time. When full, tryPush() returns false. When empty, tryPop() returns false.
Usage:
MPSCQueue<Message> queue(1024);
// Producer threads (any number):
if (queue.tryPush(msg)) { /* success */ }
// Consumer thread (exactly one):
Message msg;
if (queue.tryPop(msg)) { /* got message */ }
Thread safety:
- Multiple threads may call tryPush/emplace concurrently (producers)
- Exactly one thread may call tryPop (consumer)
- size(), empty(), capacity() may be called from any thread
Note: This queue does not provide blocking operations. For blocking behavior, combine with a semaphore or condition variable.
Member Summary
Member Functions: capacity, emplace, empty, operator =, size, tryPop, tryPush
Constructors
MPSCQueue
explicit MPSCQueue(
std::size_t capacity
);
Creates the queue with the given capacity. Capacity must be at least 1 and will be rounded up to the next power of two for efficient indexing.
MPSCQueue
MPSCQueue(
const MPSCQueue & param292
) = delete;
MPSCQueue
MPSCQueue(
MPSCQueue && param294
) = delete;
Destructor
~MPSCQueue
~MPSCQueue();
Destroys the queue.
Member Functions
capacity
std::size_t capacity() const;
Returns the capacity of the queue.
emplace
template < typename ... Args > bool emplace(
Args && ... args
);
Attempts to construct an item in-place at the back of the queue. Returns true if successful, false if the queue is full. Thread-safe for multiple concurrent producers.
empty
bool empty() const;
Returns true if the queue appears to be empty. This is approximate for the same reason as size().
operator =
MPSCQueue & operator = (
const MPSCQueue & param293
) = delete;
operator =
MPSCQueue & operator = (
MPSCQueue && param295
) = delete;
size
std::size_t size() const;
Returns an approximate count of items in the queue. This is approximate because producers may be modifying the queue concurrently.
tryPop
bool tryPop(
T & item
);
Attempts to pop an item from the queue. If successful, moves the item into the provided reference and returns true. Returns false if the queue is empty. Must be called from exactly one thread (the consumer).
tryPush
bool tryPush(
const T & item
);
Attempts to push a copy of item onto the queue. Returns true if successful, false if the queue is full. Thread-safe for multiple concurrent producers.
tryPush
bool tryPush(
T && item
);
Attempts to push item onto the queue using move semantics. Returns true if successful, false if the queue is full. Thread-safe for multiple concurrent producers.
Variables
CACHE_LINE_SIZE
static constexpr std::size_t CACHE_LINE_SIZE = std::hardware_destructive_interference_size;