eve.rt.pool

Thread Pool for Parallel Task Execution

This module provides a thread pool implementation for executing tasks across multiple worker threads. It integrates with the EVE runtime layer to enable efficient parallel and concurrent execution patterns.

The thread pool manages a fixed number of worker threads that pull tasks from a shared work queue. This amortizes thread creation overhead and provides backpressure when the system is overloaded.

Types 4

enumPoolState : ubyte

State of the thread pool.

CREATEDPool has been created but not yet started.
RUNNINGPool is running and accepting tasks.
STOPPINGPool is shutting down; no new tasks accepted.
STOPPEDPool has fully stopped; all workers have exited.
enumSubmitResult : ubyte

Result of attempting to submit a task to the pool.

SUCCESSTask was successfully queued.
REJECTEDPool is not accepting new tasks (stopping or stopped).
QUEUE_FULLTask queue is full (for bounded pools).
aliasTask = void delegate() @safe

A task to be executed by the thread pool.

Tasks are simple delegates that take no arguments and return nothing. Any results should be communicated through captured variables, futures, or channels.

A pool of worker threads for executing tasks in parallel.

The ThreadPool manages a collection of worker threads that execute submitted tasks. Tasks are queued and executed in approximate FIFO order, though no strict ordering is guaranteed due to concurrent execution.

Example:

auto pool = ThreadPool.create(4);  // 4 worker threads
pool.start();

pool.submit(() @safe {
  // Task code here
});

pool.shutdown();
pool.awaitTermination();

Fields
Thread[] workers
Task[] taskQueue
size_t queueHead
size_t queueTail
size_t queueCount
size_t queueCapacity
Mutex mutex
Condition workAvailable
Condition queueNotFull
PoolState state_
size_t activeWorkers_
ulong tasksCompleted_
ulong tasksSubmitted_
size_t numWorkers_
bool waitForTasks_
Methods
ThreadPool create(size_t numWorkers = 0, size_t queueCapacity = 1024) @trustedCreate a new thread pool with the specified number of workers.
void start() @trustedStart the thread pool, spawning worker threads.
SubmitResult submit(Task task) @trustedSubmit a task for execution by the thread pool.
SubmitResult trySubmit(Task task) @trustedTry to submit a task without blocking.
void shutdown(bool waitForTasks = true) @trustedInitiate graceful shutdown of the thread pool.
void awaitTermination() @trustedWait for all worker threads to terminate.
PoolState state() @property const @trusted nothrow @nogcGet the current state of the pool.
size_t numWorkers() @property const pure @safe nothrow @nogcGet the number of worker threads in the pool.
size_t activeWorkers() @property const @trusted nothrow @nogcGet the number of currently active (executing) workers.
size_t pendingTasks() @property @trustedGet the number of tasks waiting in the queue.
ulong completedTasks() @property const @trusted nothrow @nogcGet the total number of tasks that have been completed.
ulong submittedTasks() @property const @trusted nothrow @nogcGet the total number of tasks that have been submitted.
void workerLoop() @trusted
Constructors
this(size_t numWorkers, size_t queueCapacity)

Functions 1

fnsize_t defaultWorkerCount() @trustedGet the default number of worker threads based on CPU count.