Air-Trap 1.0.0
A multiplayer R-Type clone game engine built with C++23 and ECS architecture
Loading...
Searching...
No Matches
rtp::thread::ThreadPool Class Referencefinal

ThreadPool class for managing a pool of threads. More...

#include <ThreadPool.hpp>

Collaboration diagram for rtp::thread::ThreadPool:

Public Member Functions

 ThreadPool (const ThreadPool &)=delete
 
ThreadPooloperator= (const ThreadPool &)=delete
 
 ~ThreadPool () noexcept
 Destroys the ThreadPool, ensuring all threads are joined and resources are released.
 
template<std::invocable F, typename... Args>
auto enqueue (F &&f, Args &&...args) -> std::expected< std::future< std::invoke_result_t< F, Args... > >, rtp::Error >
 Enqueues a task for asynchronous execution.
 

Static Public Member Functions

static auto create (size_t numThreads) -> std::expected< std::unique_ptr< ThreadPool >, rtp::Error >
 Creates a ThreadPool with the specified number of threads.
 

Private Member Functions

 ThreadPool (void) noexcept=default
 Private constructor to initialize the ThreadPool.
 
void start (size_t numThreads)
 Starts the worker threads in the pool.
 
void workerThread (std::stop_token stopToken) noexcept
 The worker thread function that continuously processes tasks.
 

Private Attributes

std::vector< std::jthread > _workers
 Vector of worker threads.
 
std::queue< std::move_only_function< void(void)> > _tasks
 Queue of tasks to be executed.
 
std::mutex _queueMutex
 Mutex for synchronizing access to the task queue.
 
std::condition_variable _condition
 Condition variable for notifying worker threads.
 
bool _stop {false}
 Flag indicating whether the ThreadPool is stopping.
 

Detailed Description

ThreadPool class for managing a pool of threads.

The ThreadPool class creates a fixed number of worker threads at construction. Tasks are enqueued to be executed asynchronously, with each worker thread continuously fetching and executing tasks from a shared queue. Synchronization of accesses to the task queue is maintained with a mutex and a condition variable. The destructor takes care of shutting down the worker threads gracefully to ensure proper cleanup of resources.

Definition at line 74 of file ThreadPool.hpp.

Constructor & Destructor Documentation

◆ ThreadPool() [1/2]

rtp::thread::ThreadPool::ThreadPool ( const ThreadPool )
delete

◆ ~ThreadPool()

rtp::thread::ThreadPool::~ThreadPool ( )
noexcept

Destroys the ThreadPool, ensuring all threads are joined and resources are released.

Upon destruction, the ThreadPool stops accepting new tasks and waits for all currently queued tasks to finish execution before joining the worker threads.

Definition at line 88 of file ThreadPool.cpp.

References _condition, _queueMutex, _stop, _tasks, and RTP_VERIFY.

◆ ThreadPool() [2/2]

rtp::thread::ThreadPool::ThreadPool ( void  )
privatedefaultnoexcept

Private constructor to initialize the ThreadPool.

The constructor is private to enforce the use of the static create method for instantiation. It initializes the ThreadPool with the specified number of threads.

Member Function Documentation

◆ create()

auto rtp::thread::ThreadPool::create ( size_t  numThreads) -> std::expected<std::unique_ptr<ThreadPool>, rtp::Error>
static

Creates a ThreadPool with the specified number of threads.

Parameters
numThreadsThe number of worker threads to create in the pool
Returns
std::expected<std::unique_ptr<ThreadPool>, rtp::Error> A unique pointer to the created ThreadPool on success, or an error message on failure

This static method initializes a ThreadPool instance with the given number of threads. It returns a std::expected containing either the ThreadPool instance or an rtp::Error if the creation fails.

Definition at line 59 of file ThreadPool.cpp.

References rtp::Error::failure(), rtp::log::info(), rtp::InternalRuntimeError, rtp::InvalidParameter, start(), and rtp::Unknown.

◆ enqueue()

template<std::invocable F, typename... Args>
auto rtp::thread::ThreadPool::enqueue ( F &&  f,
Args &&...  args 
) -> std::expected< std::future< std::invoke_result_t< F, Args... > >, rtp::Error >

Enqueues a task for asynchronous execution.

Template Parameters
FType of the callable task.
ArgsTypes of the arguments to be passed to the callable.
Parameters
fThe callable object to be executed.
argsArguments to be passed to the callable object.
Returns
A std::future representing the result of the asynchronous task.

The task is wrapped into a std::move_only_function<void(void)> and then stored in the queue. A condition variable is notified to wake up one of the waiting worker threads.

◆ operator=()

ThreadPool & rtp::thread::ThreadPool::operator= ( const ThreadPool )
delete

◆ start()

void rtp::thread::ThreadPool::start ( size_t  numThreads)
private

Starts the worker threads in the pool.

Parameters
numThreadsThe number of worker threads to create.

This method initializes the specified number of worker threads. Each thread runs the workerThread function, which continuously processes tasks from the queue.

Definition at line 105 of file ThreadPool.cpp.

References _workers.

Referenced by create().

◆ workerThread()

void rtp::thread::ThreadPool::workerThread ( std::stop_token  stopToken)
privatenoexcept

The worker thread function that continuously processes tasks.

Each worker thread executes this function in a loop. The function waits for a new task to become available. When notified or when a task is enqueued, the thread locks the queue, retrieves the task, unlocks the queue, and then executes the task. It continues this cycle until the ThreadPool is signaled to stop.

Definition at line 114 of file ThreadPool.cpp.

References rtp::log::error(), and RTP_ASSERT.

Member Data Documentation

◆ _condition

std::condition_variable rtp::thread::ThreadPool::_condition
private

Condition variable for notifying worker threads.

Definition at line 135 of file ThreadPool.hpp.

Referenced by ~ThreadPool().

◆ _queueMutex

std::mutex rtp::thread::ThreadPool::_queueMutex
private

Mutex for synchronizing access to the task queue.

Definition at line 133 of file ThreadPool.hpp.

Referenced by ~ThreadPool().

◆ _stop

bool rtp::thread::ThreadPool::_stop {false}
private

Flag indicating whether the ThreadPool is stopping.

Definition at line 138 of file ThreadPool.hpp.

Referenced by ~ThreadPool().

◆ _tasks

std::queue<std::move_only_function<void(void)> > rtp::thread::ThreadPool::_tasks
private

Queue of tasks to be executed.

Definition at line 130 of file ThreadPool.hpp.

Referenced by ~ThreadPool().

◆ _workers

std::vector<std::jthread> rtp::thread::ThreadPool::_workers
private

Vector of worker threads.

Definition at line 128 of file ThreadPool.hpp.

Referenced by start().


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