A simple thread pool for executing jobs asynchronously.
More...
#include <thread_pool.hpp>
|
| 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 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 |
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:
pool.queueJob([]() {
});
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)
◆ 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
◆ onWorkerThread()
| auto toast::ThreadPool::onWorkerThread |
( |
| ) |
-> bool |
|
staticnodiscardnoexcept |
- Returns
- true when the calling thread is one of the pool's workers
◆ 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
-
| job | The function to execute (moved into the queue) |
- Example:
pool.queueJob([data = std::move(myData)]() {
processData(data);
});
◆ 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: