inLimbo
TUI Music Player that keeps you in Limbo.
 
Loading...
Searching...
No Matches
WorkerThreadPool Class Reference

A thread pool that manages a collection of worker threads. More...

#include <workers.hpp>

Public Member Functions

 WorkerThreadPool (size_t thread_count=std::thread::hardware_concurrency())
 Constructs a worker thread pool with a specified number of threads.
 
template<class F, class... Args>
auto enqueue (F &&f, Args &&... args) -> std::future< typename std::result_of< F(Args...)>::type >
 Enqueues a task to be executed by the worker threads.
 
 ~WorkerThreadPool ()
 Gracefully shuts down the thread pool.
 
 WorkerThreadPool (const WorkerThreadPool &)=delete
 
auto operator= (const WorkerThreadPool &) -> WorkerThreadPool &=delete
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ WorkerThreadPool() [1/2]

WorkerThreadPool::WorkerThreadPool ( size_t thread_count = std::thread::hardware_concurrency())
inlineexplicit

Constructs a worker thread pool with a specified number of threads.

Initializes the pool with a given number of threads, which defaults to the number of hardware threads available on the system. Each worker thread will continuously wait for tasks and execute them as they are added to the queue.

Parameters
thread_countThe number of threads to initialize. Defaults to the number of hardware threads.

◆ ~WorkerThreadPool()

WorkerThreadPool::~WorkerThreadPool ( )
inline

Gracefully shuts down the thread pool.

This method will stop accepting new tasks and wait for all worker threads to finish their current tasks before destroying the pool. Once all threads have finished, the destructor will join all threads, ensuring they are properly cleaned up.

◆ WorkerThreadPool() [2/2]

WorkerThreadPool::WorkerThreadPool ( const WorkerThreadPool & )
delete

Member Function Documentation

◆ enqueue()

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
FThe type of the callable (function, lambda, etc.) to be executed.
ArgsThe types of arguments that the callable accepts.
Parameters
fThe function or callable to be executed.
argsThe 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_errorIf the thread pool has been stopped and no more tasks can be enqueued.

◆ operator=()

auto WorkerThreadPool::operator= ( const WorkerThreadPool & ) -> WorkerThreadPool &=delete
delete

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