System & concurrency

queue.h

Concurrent FIFO, blocking, priority, and timed work queues.

C++23 mc/queue.h
#include <mc/queue.h>

Choose CLQueue for blocking FIFO consumption, CPriorityQueue for bounded priority order, and CTimedQueue for delayed callbacks.

Stop all queue users before destroying a queue. CPriorityQueue copies payloads and does not own pointed-to objects. CLQueue<T*> deletes pointers still queued at destruction.

Jump to a declaration · 38

CQueue

template<class T> class CQueue

Methods

CQueue

CQueue(size_t capacity = 0);

Creates a concurrent FIFO with an initial node reservation. The capacity argument is not a fixed upper bound on later allocation.

push

void push(const T& v);

Copies an item into the queue. This wrapper does not report the underlying push success flag, so it cannot be used to confirm delivery after resource exhaustion.

operator<<

CQueue& operator<<(const T& v);

Calls push() and returns the queue for chaining. It has the same delivery-reporting limitation as push().

pop

bool pop(T& v);

Attempts to remove the oldest item without waiting. Returns false when no item is available.

empty

bool empty();

Takes a momentary emptiness observation; concurrent producers or consumers can change it immediately. Use the result of pop() to decide whether an item was obtained.

CPriorityQueue

template<class I> requires(std::is_trivially_copyable_v<I> && std::is_trivially_copy_assignable_v<I> && std::is_nothrow_default_constructible_v<I>) class CPriorityQueue

Types, constants & data

using Item = I;

Methods

push

[[nodiscard]] bool push(const Item& item, double priority);

Queues a copy in descending priority order, FIFO for ties. False means storage is occupied or reserved. NaN priorities throw.

pop

bool pop(Item& item, double& priority) noexcept;
bool pop(Item& item) noexcept;

Never waits. False leaves the output arguments unchanged.

empty

bool empty() const noexcept;

Reports whether the queue is empty at the instant observed. A concurrent operation can change the result immediately.

CLQueue

template<class I> class CLQueue

Types, constants & data

using Item = I;
static constexpr bool ItemPointer = std::is_pointer_v<Item>;

Methods

CLQueue

CLQueue();

Creates an empty enabled blocking queue. Consumers wait in pop() until an item arrives or the queue is disabled.

~CLQueue

~CLQueue();

Destroys queued items. For pointer item types, also deletes every pointee left in the queue; stop all producers and consumers before destruction.

pop

bool pop(Item& item);
template<class R, class P> bool pop(Item& item, const std::chrono::duration<R, P>& dt);
bool pop(Item& item, double dt);

Waits for the oldest item and moves it into the output. Returns false when disabled or a timed wait expires; a false result leaves the output unchanged. Numeric timeouts are in seconds.

tryPop

bool tryPop(Item& item);

Removes the oldest item if immediately available and enabled. Returns false without modifying the output when empty or disabled.

push

void push(const Item& v);
void push(Item&& v);

Copies or moves an item into the FIFO and wakes one consumer. Pushes are still accepted after disable(), although those items cannot be popped.

operator<<

CLQueue& operator<<(const Item& v);
CLQueue& operator<<(Item&& v);

Queues an item through push() and returns this queue for chaining.

disable

void disable();

Wakes waiting consumers and prevents further pops, including queued items. Stop producers as well; push does not reject a disabled queue.

CTimedQueue

Types, constants & data

using Func = std::function<void()>;

Methods

start

void start();

Starts the dispatcher; repeated calls while running have no effect. Tasks may be queued before startup.

stop

void stop();

Requests dispatcher shutdown and joins its thread. This is not a guarantee that all scheduled tasks ran or that tasks launched through addFuture() have completed.

add

void add(Func f, double dt = 0.0);

Schedules a callable for approximately dt seconds from now. Execution can be delayed by earlier work; this form supplies no completion handle.

addFuture

std::shared_ptr<Future> addFuture(Func f, double dt = 0.0);

Schedules a callable and returns a shared completion handle. Its await() can wait for dispatch and completion; it does not retrieve a return value or rethrow a callback exception.

clear

void clear();

Discards callbacks that have not been dispatched. Do not wait on completion handles for discarded work.

CTimedQueue::Future

class Future

Methods

await

void await();

Waits for dispatch and callback completion. This waits on the future without retrieving a stored callback exception.

init_

void init_(Future_&& f);

Attaches the asynchronous task and releases the dispatch waiter. This is scheduler support; clients normally obtain a future from addFuture() rather than initialize one themselves.