RigsofRods
Soft-body Physics Simulation
|
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< Task > | RunTask (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 ThreadPool * | DetectNumWorkersAndCreate () |
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... | |
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:
Usage example 2:
Definition at line 105 of file ThreadPool.h.
|
inline |
Construct thread pool and launch worker threads.
num_threads | Number of worker threads to use |
Definition at line 129 of file ThreadPool.h.
|
inline |
Definition at line 169 of file ThreadPool.h.
|
inlinestatic |
Definition at line 107 of file ThreadPool.h.
|
inline |
Run collection of tasks in parallel and wait until all have finished.
Definition at line 193 of file ThreadPool.h.
|
inline |
Submit new asynchronous task to thread pool and return Task handle to allow for synchronization.
Definition at line 178 of file ThreadPool.h.
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.
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.
std::mutex RoR::ThreadPool::m_taskqueue_mutex |
Protects task queue from concurrent access.
Definition at line 216 of file ThreadPool.h.
std::atomic_bool RoR::ThreadPool::m_terminate {false} |
Indicates destruction of ThreadPool instance to worker threads.
Definition at line 213 of file ThreadPool.h.
std::vector<std::thread> RoR::ThreadPool::m_threads |
Collection of worker threads to run tasks.
Definition at line 214 of file ThreadPool.h.