A thread pool for managing concurrent tasks.
More...
#include <Thread_Pool.hpp>
|
using | Task = std::function < void (std::stop_token) > |
|
|
void | start () |
| Starts the thread pool by creating threads and binding them to the thread_function.
|
|
void | stop () |
| Stops the thread pool by closing the task queue and requesting all threads to stop.
|
|
template<typename F, typename... Args> |
std::future< std::invoke_result_t< F, Args... > > | submit (F &&f, Args &&... args) |
| Submits a task to the thread pool for execution.
|
|
template<typename F, typename... Args> |
std::future< std::invoke_result_t< F, std::stop_token, Args... > > | submit_with_stop (F &&f, Args &&... args) |
| Submits a task to the thread pool for execution with a stop token.
|
|
|
std::binary_semaphore | sem_mesh_ready {0} |
| Semaphore to signal that the mesh is ready for rendering.
|
|
std::binary_semaphore | sem_render_done {0} |
| Semaphore to signal that the rendering is done.
|
|
|
Sync_Queue< Task > | tasks |
| A synchronized queue for managing tasks in the thread pool.
|
|
std::vector< std::unique_ptr< std::jthread > > | threads |
| A vector of unique pointers to jthread objects representing the threads in the pool.
|
|
std::atomic< bool > | started |
| Flag indicating whether the thread pool has been started or not.
|
|
A thread pool for managing concurrent tasks.
This class provides a thread pool that can execute tasks concurrently using a specified number of threads. It allows submitting tasks and handles synchronization using semaphores.
◆ Task
◆ Thread_Pool() [1/3]
Ragot::Thread_Pool::Thread_Pool |
( |
| ) |
|
|
inlineprivate |
Private constructor for the Thread_Pool class.
Initializes the thread pool with the number of threads equal to the number of hardware cores available. If the number of cores is zero, it defaults to two threads.
◆ ~Thread_Pool()
Ragot::Thread_Pool::~Thread_Pool |
( |
| ) |
|
|
inlineprivate |
Destructor for the Thread_Pool class.
Cleans up resources used by the thread pool, releases semaphores, and stops all threads if they are running.
◆ Thread_Pool() [2/3]
Construct a new Thread_Pool object (Deleted). This constructor is deleted to prevent copying of the Thread_Pool object.
◆ Thread_Pool() [3/3]
Move constructor for the Thread_Pool class (Deleted). This move constructor is deleted to prevent moving of the Thread_Pool object.
◆ instance()
Singleton instance of the Thread_Pool class.
This method provides access to the singleton instance of the Thread_Pool. It ensures that only one instance of the thread pool exists throughout the application.
- Returns
- Thread_Pool& Reference to the singleton instance of the Thread_Pool.
◆ operator=() [1/2]
Move assignment operator for the Thread_Pool class (Deleted). This move assignment operator is deleted to prevent moving of the Thread_Pool object.
◆ operator=() [2/2]
Assignment operator for the Thread_Pool class (Deleted). This assignment operator is deleted to prevent copying of the Thread_Pool object.
◆ start()
void Ragot::Thread_Pool::start |
( |
| ) |
|
|
inline |
Starts the thread pool by creating threads and binding them to the thread_function.
This method initializes the threads in the pool and starts them, allowing them to execute tasks concurrently. It asserts that the thread pool has not already been started to prevent multiple initializations.
◆ stop()
void Ragot::Thread_Pool::stop |
( |
| ) |
|
|
inline |
Stops the thread pool by closing the task queue and requesting all threads to stop.
This method stops the thread pool, ensuring that all threads are requested to stop gracefully. It asserts that the thread pool has been started before attempting to stop it.
◆ submit()
template<typename F, typename... Args>
std::future< std::invoke_result_t< F, Args... > > Ragot::Thread_Pool::submit |
( |
F && | f, |
|
|
Args &&... | args ) |
|
inline |
Submits a task to the thread pool for execution.
This method allows submitting a task to the thread pool, which will be executed by one of the threads. It returns a future that can be used to retrieve the result of the task once it is completed.
- Template Parameters
-
F | The type of the function to be executed. |
Args | The types of the arguments to be passed to the function. |
- Parameters
-
f | The function to be executed. |
args | The arguments to be passed to the function. |
- Returns
- std::future<std::invoke_result_t<F, Args...>> A future representing the result of the task.
◆ submit_with_stop()
template<typename F, typename... Args>
std::future< std::invoke_result_t< F, std::stop_token, Args... > > Ragot::Thread_Pool::submit_with_stop |
( |
F && | f, |
|
|
Args &&... | args ) |
|
inline |
Submits a task to the thread pool for execution with a stop token.
This method allows submitting a task to the thread pool, which will be executed by one of the threads. It accepts a stop token that can be used to request cancellation of the task. It returns a future that can be used to retrieve the result of the task once it is completed.
- Template Parameters
-
F | The type of the function to be executed. |
Args | The types of the arguments to be passed to the function. |
- Parameters
-
f | The function to be executed. |
args | The arguments to be passed to the function. |
- Returns
- std::future<std::invoke_result_t<F, std::stop_token, Args...>> A future representing the result of the task.
◆ thread_function()
void Ragot::Thread_Pool::thread_function |
( |
std::stop_token | stop_token | ) |
|
|
private |
The function executed by each thread in the thread pool.
This function continuously retrieves tasks from the task queue and executes them. It uses a stop token to allow threads to gracefully exit when requested.
- Parameters
-
stop_token | The stop token used to request cancellation of the task. |
◆ sem_mesh_ready
std::binary_semaphore Ragot::Thread_Pool::sem_mesh_ready {0} |
Semaphore to signal that the mesh is ready for rendering.
◆ sem_render_done
std::binary_semaphore Ragot::Thread_Pool::sem_render_done {0} |
Semaphore to signal that the rendering is done.
◆ started
std::atomic< bool > Ragot::Thread_Pool::started |
|
private |
Flag indicating whether the thread pool has been started or not.
◆ tasks
A synchronized queue for managing tasks in the thread pool.
This queue is used to store tasks that need to be executed by the threads in the pool. It provides thread-safe operations for pushing and popping tasks.
◆ threads
std::vector< std::unique_ptr < std::jthread > > Ragot::Thread_Pool::threads |
|
private |
A vector of unique pointers to jthread objects representing the threads in the pool.
This vector holds the threads that will execute tasks concurrently. Each thread is managed by a unique pointer to ensure proper resource management.
The documentation for this class was generated from the following files: