|
Air-Trap 1.0.0
A multiplayer R-Type clone game engine built with C++23 and ECS architecture
|
ThreadPool class for managing a pool of threads. More...
#include <ThreadPool.hpp>
Public Member Functions | |
| ThreadPool (const ThreadPool &)=delete | |
| ThreadPool & | operator= (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. | |
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.
|
delete |
|
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.
|
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.
|
static |
Creates a ThreadPool with the specified number of threads.
| numThreads | The number of worker threads to create in the pool |
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.
| 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.
| F | Type of the callable task. |
| Args | Types of the arguments to be passed to the callable. |
| f | The callable object to be executed. |
| args | Arguments to be passed to the callable object. |
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.
|
delete |
|
private |
Starts the worker threads in the pool.
| numThreads | The 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().
|
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.
|
private |
Condition variable for notifying worker threads.
Definition at line 135 of file ThreadPool.hpp.
Referenced by ~ThreadPool().
|
private |
Mutex for synchronizing access to the task queue.
Definition at line 133 of file ThreadPool.hpp.
Referenced by ~ThreadPool().
|
private |
Flag indicating whether the ThreadPool is stopping.
Definition at line 138 of file ThreadPool.hpp.
Referenced by ~ThreadPool().
|
private |
Queue of tasks to be executed.
Definition at line 130 of file ThreadPool.hpp.
Referenced by ~ThreadPool().
|
private |