Toast Engine
Loading...
Searching...
No Matches
toast::ThreadPool Class Reference

A simple thread pool for executing jobs asynchronously. More...

#include <thread_pool.hpp>

Public Member Functions

void destroy ()
 Destroys the thread pool and waits for all workers to finish.
auto busy () -> bool
 Checks if the pool has pending or in-flight jobs.
void waitIdle ()
 Blocks the calling thread until all queued and active jobs complete.
 ThreadPool (ThreadPool &)=delete
 ThreadPool (ThreadPool &&)=delete
auto operator= (const ThreadPool &) -> ThreadPool &=delete
auto operator= (ThreadPool &&) -> ThreadPool &=delete

Static Public Member Functions

static auto create () noexcept -> std::unique_ptr< ThreadPool >
 Initializes the pool and returns a pointer with ownership.
template<typename T>
static auto push (T &&job) -> std::future< std::invoke_result_t< T > >
 Queues a job for execution by a worker thread.
static void pushRaw (std::move_only_function< void()> &&job)
 Queues a job with no std::future the caller synchronizes completion itself same queue and workers as push().
static auto workerCount () -> size_t
static auto idleWorkers () noexcept -> size_t
static auto onWorkerThread () noexcept -> bool

Detailed Description

A simple thread pool for executing jobs asynchronously.

The ThreadPool manages a fixed number of worker threads that process jobs from a queue. Jobs are executed in FIFO order, though completion order is not guaranteed due to parallel execution.

Usage Example:
ThreadPool pool = ThreadPool::create(); // Create pool with THREAD_COUNT workers
pool.queueJob([]() {
// Do some heavy work...
});
pool.waitIdle(); // Block until all jobs are done
pool.destroy();
void waitIdle()
Blocks the calling thread until all queued and active jobs complete.
Definition thread_pool.cpp:54
static auto create() noexcept -> std::unique_ptr< ThreadPool >
Initializes the pool and returns a pointer with ownership.
Definition thread_pool.cpp:116
void destroy()
Destroys the thread pool and waits for all workers to finish.
Definition thread_pool.cpp:32
Note
The pool should be destroyed before the program exits to ensure all worker threads are properly joined.
See also
World (uses ThreadPool for async scene loading)

Member Function Documentation

◆ busy()

auto toast::ThreadPool::busy ( ) -> bool
nodiscard

Checks if the pool has pending or in-flight jobs.

Returns true if there are jobs in the queue OR if any worker is currently executing a job.

Returns
true if any work is pending or in progress.

◆ create()

auto toast::ThreadPool::create ( ) -> std::unique_ptr< ThreadPool >
staticnoexcept

Initializes the pool and returns a pointer with ownership.

Remember to terminate the pool properly before it gets discarded:

pool.waitIdle();
pool.destroy();

◆ destroy()

void toast::ThreadPool::destroy ( )

Destroys the thread pool and waits for all workers to finish.

Signals all worker threads to stop and joins them. Any jobs still in the queue will NOT be executed.

Warning
This method blocks until all worker threads have terminated.

◆ idleWorkers()

auto toast::ThreadPool::idleWorkers ( ) -> size_t
staticnodiscardnoexcept
Returns
how many workers are waiting for a job right now
Here is the caller graph for this function:

◆ onWorkerThread()

auto toast::ThreadPool::onWorkerThread ( ) -> bool
staticnodiscardnoexcept
Returns
true when the calling thread is one of the pool's workers
Here is the caller graph for this function:

◆ push()

template<typename T>
auto toast::ThreadPool::push ( T && job) -> std::future< std::invoke_result_t< T > >
static

Queues a job for execution by a worker thread.

The job will be executed as soon as a worker thread becomes available. Jobs are processed in FIFO order. The thread pool uses move_only funcions.

Parameters
jobThe function to execute (moved into the queue)
Example:
pool.queueJob([data = std::move(myData)]() {
processData(data);
});
Here is the caller graph for this function:

◆ waitIdle()

void toast::ThreadPool::waitIdle ( )

Blocks the calling thread until all queued and active jobs complete.

More efficient than spinning on busy(); uses a condition variable to sleep until the pool drains completely.


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