World Ocean Simulation System (WOSS) library
woss::ThreadPool Class Reference

#include <thread-pool-definitions.h>

Collaboration diagram for woss::ThreadPool:
Collaboration graph

Public Types

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

Public Member Functions

 ThreadPool (size_t n_threads)
 
 ~ThreadPool ()
 
template<class F , class... Args>
auto submit (F &&f, Args &&... args) -> std::future< decltype(std::forward< F >(f)(std::forward< Args >(args)...))>
 Submits a task to the thread pool.
 
size_t getNumThreads ()
 Gets the number of threads in the pool.
 

Private Member Functions

void start ()
 
void stop ()
 

Private Attributes

size_t num_threads
 
std::vector< std::thread > workers
 
std::queue< Tasktasks
 
std::mutex queue_mutex
 
std::condition_variable task_cond_var
 
bool stop_flag
 

Detailed Description

ThreadPool class manages a fixed number of worker threads that pick up and execute tasks from a shared queue.

Member Typedef Documentation

◆ Task

using woss::ThreadPool::Task = std::function< void() >

Task type: A generic callable that takes no arguments and returns nothing.

Constructor & Destructor Documentation

◆ ThreadPool()

woss::ThreadPool::ThreadPool ( size_t  n_threads)
inlineexplicit

Constructor, Initializes the thread pool with a specified number of threads.

Parameters
n_threadsnumber of threads. If set to 0, then system will automatically pick the max allowed number of threads.

References num_threads, and start().

Here is the call graph for this function:

◆ ~ThreadPool()

woss::ThreadPool::~ThreadPool ( )
inline

Destructor, Ensures all worker threads are joined and the pool is properly shut down.

References stop().

Here is the call graph for this function:

Member Function Documentation

◆ getNumThreads()

size_t woss::ThreadPool::getNumThreads ( )
inline

Gets the number of threads in the pool.

Returns
the number of std::thread created in the pool

References num_threads.

◆ start()

void woss::ThreadPool::start ( )
inlineprivate

Creates up to num_threads std::thread and make them wait for work

References num_threads, queue_mutex, stop_flag, task_cond_var, tasks, and workers.

Referenced by ThreadPool().

◆ stop()

void woss::ThreadPool::stop ( )
inlineprivate

Sets the stop_flag, wakes up every thread and wait for them to exit.

References queue_mutex, stop_flag, task_cond_var, and workers.

Referenced by ~ThreadPool().

◆ submit()

template<class F , class... Args>
auto woss::ThreadPool::submit ( F &&  f,
Args &&...  args 
) -> std::future<decltype(std::forward<F>(f)(std::forward<Args>(args)...))>
inline

Submits a task to the thread pool.

Parameters
fis the function type (e.g., a lambda, a regular function pointer, std::function).
argsare the types of the arguments for the function F.
Returns
return type is automatically deduced to be a std::future wrapping the return type of the function F.

References queue_mutex, stop_flag, task_cond_var, and tasks.

Referenced by woss::WossManagerResDbMT::getWossPressure(), woss::WossManagerResDbMT::getWossPressure(), woss::WossManagerResDbMT::getWossTimeArr(), and woss::WossManagerResDbMT::getWossTimeArr().

Member Data Documentation

◆ num_threads

size_t woss::ThreadPool::num_threads
private

The number of threads in the pool.

Referenced by getNumThreads(), start(), and ThreadPool().

◆ queue_mutex

std::mutex woss::ThreadPool::queue_mutex
private

Mutex to protect access to the tasks queue.

Referenced by start(), stop(), and submit().

◆ stop_flag

bool woss::ThreadPool::stop_flag
private

Flag to signal threads to terminate.

Referenced by start(), stop(), and submit().

◆ task_cond_var

std::condition_variable woss::ThreadPool::task_cond_var
private

Condition variable for task synchronization.

Referenced by start(), stop(), and submit().

◆ tasks

std::queue<Task> woss::ThreadPool::tasks
private

Queue of tasks to be executed.

Referenced by start(), and submit().

◆ workers

std::vector<std::thread> woss::ThreadPool::workers
private

The collection of worker threads in the pool.

Referenced by start(), and stop().


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