vkmEngine 1.0.0
A C++ game engine · vkmengine.com
Loading...
Searching...
No Matches
Vkm::Engine::ThreadPool Class Reference

Fixed-size pool of worker threads draining two task queues. More...

#include <thread_pool.h>

Classes

class  Batch
 A run of tasks addressed by index, queued as one entry. More...

Public Member Functions

void addTask (std::function< void()> &&task)
 Enqueue a single task and wake one worker.
void addBatch (Batch &batch)
 Queue every index of batch as one entry and wake all workers.
void waitForBatch (Batch &batch)
 Block the caller until every index of batch has retired.
void shutdown ()
 Join the workers now, ahead of the pool's own destruction.

Static Public Member Functions

static ThreadPool & get ()
 Access the process-wide thread pool, constructed on first use.
static bool isWorkerThread ()
 True when called from a thread owned by the pool.

Detailed Description

Fixed-size pool of worker threads draining two task queues.

Waiting is per-batch, so a caller never blocks on work someone else queued. Batches (the frame is blocked on them) are served before addTask's background tasks. That cannot starve the background for long: only the frame fills the batch queue, and a batch must be joined within the frame that added it.

Member Function Documentation

◆ get()

ThreadPool & Vkm::Engine::ThreadPool::get ( )
static

Access the process-wide thread pool, constructed on first use.

Returns
The single pool.

◆ addTask()

void Vkm::Engine::ThreadPool::addTask ( std::function< void()> && task)

Enqueue a single task and wake one worker.

Refused with a warning after shutdown(), when nothing drains the queue.

Parameters
taskThe work to run on a worker thread.

◆ addBatch()

void Vkm::Engine::ThreadPool::addBatch ( Batch & batch)

Queue every index of batch as one entry and wake all workers.

Refused after shutdown() with the counter left at zero, so waitForBatch() returns.

Parameters
batchThe batch to run; it and its task must outlive waitForBatch().

◆ waitForBatch()

void Vkm::Engine::ThreadPool::waitForBatch ( Batch & batch)

Block the caller until every index of batch has retired.

The caller first runs every index no worker has claimed. An index that throws still retires; the first exception thrown is rethrown here, whichever thread ran it.

Parameters
batchThe batch handed to addBatch.

◆ shutdown()

void Vkm::Engine::ThreadPool::shutdown ( )

Join the workers now, ahead of the pool's own destruction.

Started tasks finish; queued ones are dropped but still retire, so no waiter blocks. Call before main returns: the static's destructor runs after the singletons tasks write into, and on Windows inside DLL unload, where a join hangs. Idempotent.

◆ isWorkerThread()

bool Vkm::Engine::ThreadPool::isWorkerThread ( )
static

True when called from a thread owned by the pool.

parallelFor sweeps serially on a worker. Not for safety (a nested call would finish): the other threads are busy, so nested chunks would only come back at a lock each.

Returns
True on a pool worker, false on any other thread.

The documentation for this class was generated from the following file: