RigsofRods
Soft-body Physics Simulation
Public Member Functions | Static Public Member Functions | Data Fields
RoR::ThreadPool Class Reference

Facilitates execution of (small) tasks on separate threads. More...

#include <ThreadPool.h>

Public Member Functions

 ThreadPool (int num_threads)
 Construct thread pool and launch worker threads. More...
 
 ~ThreadPool ()
 
std::shared_ptr< TaskRunTask (const std::function< void()> &task_func)
 Submit new asynchronous task to thread pool and return Task handle to allow for synchronization. More...
 
void Parallelize (const std::vector< std::function< void()>> &task_funcs)
 Run collection of tasks in parallel and wait until all have finished. More...
 

Static Public Member Functions

static ThreadPoolDetectNumWorkersAndCreate ()
 

Data Fields

std::atomic_bool m_terminate {false}
 Indicates destruction of ThreadPool instance to worker threads. More...
 
std::vector< std::thread > m_threads
 Collection of worker threads to run tasks. More...
 
std::queue< std::shared_ptr< Task > > m_taskqueue
 Queue of submitted tasks pending for execution. More...
 
std::mutex m_taskqueue_mutex
 Protects task queue from concurrent access. More...
 
std::condition_variable m_task_available_cv
 Used to signal threads that a new task was submitted and is ready to run. More...
 

Detailed Description

Facilitates execution of (small) tasks on separate threads.

Implements a "rent-a-thread" model where each submitted task is assigned to one of several worker threads managed by the thread pool instance. This is especially useful for short running tasks as it avoids the runtime cost of creating and launching a new thread. Notice, there still is a certain overhead present due to the synchronization of threads required in the internal implementation.

Usage example 1:

auto task_handle = tp.RunTask([]{ SomeWork() }; // Start asynchronous task
SomeOtherWork();
task_handle.join(); // Wait for async task to finish

Usage example 2:

auto task1 = []{ ... };
auto task2 = std::bind(my_func, arg1, arg2);
tp.Parallelize({task1, task2}); // Run tasks in parallel and wait until all have finished
See also
Task

Definition at line 105 of file ThreadPool.h.

Constructor & Destructor Documentation

◆ ThreadPool()

RoR::ThreadPool::ThreadPool ( int  num_threads)
inline

Construct thread pool and launch worker threads.

Parameters
num_threadsNumber of worker threads to use

Definition at line 129 of file ThreadPool.h.

+ Here is the caller graph for this function:

◆ ~ThreadPool()

RoR::ThreadPool::~ThreadPool ( )
inline

Definition at line 169 of file ThreadPool.h.

Member Function Documentation

◆ DetectNumWorkersAndCreate()

static ThreadPool* RoR::ThreadPool::DetectNumWorkersAndCreate ( )
inlinestatic

Definition at line 107 of file ThreadPool.h.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Parallelize()

void RoR::ThreadPool::Parallelize ( const std::vector< std::function< void()>> &  task_funcs)
inline

Run collection of tasks in parallel and wait until all have finished.

Definition at line 193 of file ThreadPool.h.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ RunTask()

std::shared_ptr<Task> RoR::ThreadPool::RunTask ( const std::function< void()> &  task_func)
inline

Submit new asynchronous task to thread pool and return Task handle to allow for synchronization.

Definition at line 178 of file ThreadPool.h.

+ Here is the caller graph for this function:

Field Documentation

◆ m_task_available_cv

std::condition_variable RoR::ThreadPool::m_task_available_cv

Used to signal threads that a new task was submitted and is ready to run.

Definition at line 217 of file ThreadPool.h.

◆ m_taskqueue

std::queue<std::shared_ptr<Task> > RoR::ThreadPool::m_taskqueue

Queue of submitted tasks pending for execution.

Definition at line 215 of file ThreadPool.h.

◆ m_taskqueue_mutex

std::mutex RoR::ThreadPool::m_taskqueue_mutex

Protects task queue from concurrent access.

Definition at line 216 of file ThreadPool.h.

◆ m_terminate

std::atomic_bool RoR::ThreadPool::m_terminate {false}

Indicates destruction of ThreadPool instance to worker threads.

Definition at line 213 of file ThreadPool.h.

◆ m_threads

std::vector<std::thread> RoR::ThreadPool::m_threads

Collection of worker threads to run tasks.

Definition at line 214 of file ThreadPool.h.


The documentation for this class was generated from the following file:
RoR::ThreadPool::ThreadPool
ThreadPool(int num_threads)
Construct thread pool and launch worker threads.
Definition: ThreadPool.h:129