3D Graphics Engine for ESP32
 
Loading...
Searching...
No Matches
Ragot::Thread_Pool Class Reference

A thread pool for managing concurrent tasks. More...

#include <Thread_Pool.hpp>

Collaboration diagram for Ragot::Thread_Pool:
Collaboration graph

Public Types

using Task = std::function < void (std::stop_token) >
 

Public Member Functions

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.
 

Static Public Member Functions

static Thread_Poolinstance ()
 Singleton instance of the Thread_Pool class.
 

Public Attributes

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.
 

Private Member Functions

 Thread_Pool ()
 Private constructor for the Thread_Pool class.
 
 ~Thread_Pool ()
 Destructor for the Thread_Pool class.
 
 Thread_Pool (Thread_Pool &)=delete
 Construct a new Thread_Pool object (Deleted). This constructor is deleted to prevent copying of the Thread_Pool object.
 
 Thread_Pool (Thread_Pool &&)=delete
 Move constructor for the Thread_Pool class (Deleted). This move constructor is deleted to prevent moving of the Thread_Pool object.
 
Thread_Pooloperator= (Thread_Pool &)=delete
 Assignment operator for the Thread_Pool class (Deleted). This assignment operator is deleted to prevent copying of the Thread_Pool object.
 
Thread_Pooloperator= (Thread_Pool &&)=delete
 Move assignment operator for the Thread_Pool class (Deleted). This move assignment operator is deleted to prevent moving of the Thread_Pool object.
 
void thread_function (std::stop_token)
 The function executed by each thread in the thread pool.
 

Private Attributes

Sync_Queue< Tasktasks
 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.
 

Detailed Description

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.

Member Typedef Documentation

◆ Task

using Ragot::Thread_Pool::Task = std::function < void (std::stop_token) >

Constructor & Destructor Documentation

◆ 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.

Here is the caller graph for this function:

◆ ~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.

Here is the call graph for this function:

◆ Thread_Pool() [2/3]

Ragot::Thread_Pool::Thread_Pool ( Thread_Pool & )
privatedelete

Construct a new Thread_Pool object (Deleted). This constructor is deleted to prevent copying of the Thread_Pool object.

Here is the call graph for this function:

◆ Thread_Pool() [3/3]

Ragot::Thread_Pool::Thread_Pool ( Thread_Pool && )
privatedelete

Move constructor for the Thread_Pool class (Deleted). This move constructor is deleted to prevent moving of the Thread_Pool object.

Here is the call graph for this function:

Member Function Documentation

◆ instance()

static Thread_Pool & Ragot::Thread_Pool::instance ( )
inlinestatic

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.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator=() [1/2]

Thread_Pool & Ragot::Thread_Pool::operator= ( Thread_Pool && )
privatedelete

Move assignment operator for the Thread_Pool class (Deleted). This move assignment operator is deleted to prevent moving of the Thread_Pool object.

Here is the call graph for this function:

◆ operator=() [2/2]

Thread_Pool & Ragot::Thread_Pool::operator= ( Thread_Pool & )
privatedelete

Assignment operator for the Thread_Pool class (Deleted). This assignment operator is deleted to prevent copying of the Thread_Pool object.

Here is the call graph for this function:

◆ 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.

Here is the call graph for this function:

◆ 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.

Here is the caller graph for this function:

◆ 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
FThe type of the function to be executed.
ArgsThe types of the arguments to be passed to the function.
Parameters
fThe function to be executed.
argsThe 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
FThe type of the function to be executed.
ArgsThe types of the arguments to be passed to the function.
Parameters
fThe function to be executed.
argsThe 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_tokenThe stop token used to request cancellation of the task.
Here is the caller graph for this function:

Member Data Documentation

◆ 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

Sync_Queue< Task > Ragot::Thread_Pool::tasks
private

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: