A thread pool that manages a collection of worker threads.
This class allows the management of a pool of threads to execute tasks concurrently. The pool will execute submitted tasks asynchronously using worker threads. The tasks are queued and workers are assigned to execute them. Once a task is completed, the worker is available for another task. The pool supports graceful shutdown and ensures no task is left unfinished when the pool is stopped.
template<class F, class... Args>
auto WorkerThreadPool::enqueue |
( |
F && | f, |
|
|
Args &&... | args ) -> std::future<typename std::result_of<F(Args...)>::type>
|
|
inline |
Enqueues a task to be executed by the worker threads.
This method allows submitting a task to the pool. The task will be executed by one of the available worker threads. The function returns a std::future that can be used to retrieve the result of the task once it is completed.
- Template Parameters
-
F | The type of the callable (function, lambda, etc.) to be executed. |
Args | The types of arguments that the callable accepts. |
- Parameters
-
f | The function or callable to be executed. |
args | The arguments to pass to the callable. |
- Returns
- std::future<typename std::result_of<F(Args...)>::type> A future representing the result of the task once it finishes executing.
- Exceptions
-
std::runtime_error | If the thread pool has been stopped and no more tasks can be enqueued. |