ThreadPool class

ThreadPool.

A simple thread pool implementation that allows tasks to be enqueued and executed asynchronously by a fixed number of worker threads.

The pool creates a specified number of worker threads at construction. Tasks can be enqueued using the enqueue() method, which returns a std::future that can be used to retrieve the result or wait for completion.

Constructors, destructors, conversion operators

ThreadPool(size_t)
Construct a new ThreadPool.
~ThreadPool()
Destroy the ThreadPool.

Public functions

template<class F, class... Args>
auto enqueue(F&& f, Args && ... args) >::type > -> auto
Enqueue a new task to the thread pool.

Function documentation

ThreadPool::ThreadPool(size_t)

Construct a new ThreadPool.

Creates a thread pool with the specified number of worker threads.

ThreadPool::~ThreadPool()

Destroy the ThreadPool.

Stops the pool and waits for all worker threads to complete their current tasks. All pending tasks in the queue will be executed before the threads terminate.

template<class F, class... Args>
auto ThreadPool::enqueue(F&& f, Args && ... args) >::type >

Enqueue a new task to the thread pool.

Template parameters
F The callable type of the task
Args The argument types for the task
Parameters
f The callable to execute
args The arguments to pass to the callable
Returns A std::future containing the result of the task
Exceptions
std::runtime_error if enqueue is called after the pool has been stopped

Adds a new work item to the pool's task queue. The task will be executed by one of the worker threads.