![]() |
vkmEngine 1.0.0
A C++ game engine · vkmengine.com
|
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. | |
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.
|
static |
Access the process-wide thread pool, constructed on first use.
| 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.
| task | The work to run on a worker thread. |
| 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.
| batch | The batch to run; it and its task must outlive 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.
| batch | The batch handed to addBatch. |
| 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.
|
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.