A fast and lightweight thread pool that uses C++11 threads.
#include <task_thread_pool.hpp>
|
std::vector< std::thread > | threads |
| The worker threads. More...
|
|
std::mutex | thread_mutex |
| A mutex for methods that start/stop threads.
|
|
std::queue< std::packaged_task< void()> > | tasks = {} |
| The task queue. More...
|
|
std::mutex | task_mutex |
| A mutex for all variables related to tasks.
|
|
std::condition_variable | task_cv |
| Used to notify changes to the task queue, such as a new task added, pause/unpause, etc.
|
|
std::condition_variable | task_finished_cv |
| Used to notify of finished tasks.
|
|
bool | pool_running = true |
| A signal for worker threads that the pool is either running or shutting down. More...
|
|
bool | pool_paused = false |
| A signal for worker threads to not pull new tasks from the queue. More...
|
|
bool | notify_task_finish = false |
| A signal for worker threads that they should notify task_finished_cv when they finish a task. More...
|
|
int | num_inflight_tasks = 0 |
| A counter of the number of tasks in-progress by worker threads. More...
|
|
◆ task_thread_pool()
task_thread_pool::task_thread_pool::task_thread_pool |
( |
unsigned int |
num_threads = 0 | ) |
|
|
inlineexplicit |
Create a task_thread_pool and start worker threads.
- Parameters
-
num_threads | Number of worker threads. If 0 then number of threads is equal to the number of physical cores on the machine, as given by std::thread::hardware_concurrency(). |
◆ ~task_thread_pool()
task_thread_pool::task_thread_pool::~task_thread_pool |
( |
| ) |
|
|
inline |
Finish all tasks left in the queue then shut down worker threads.
If the pool is currently paused then it is resumed.
◆ clear_task_queue()
void task_thread_pool::task_thread_pool::clear_task_queue |
( |
| ) |
|
|
inline |
Drop all tasks that have been submitted but not yet started by a worker.
Tasks already in progress continue executing.
◆ get_num_queued_tasks()
NODISCARD size_t task_thread_pool::task_thread_pool::get_num_queued_tasks |
( |
| ) |
const |
|
inline |
Get number of enqueued tasks.
- Returns
- Number of tasks that have been enqueued but not yet started.
◆ get_num_running_tasks()
NODISCARD size_t task_thread_pool::task_thread_pool::get_num_running_tasks |
( |
| ) |
const |
|
inline |
Get number of in-progress tasks.
- Returns
- Approximate number of tasks currently being processed by worker threads.
◆ get_num_tasks()
NODISCARD size_t task_thread_pool::task_thread_pool::get_num_tasks |
( |
| ) |
const |
|
inline |
Get total number of tasks in the pool.
- Returns
- Approximate number of tasks both enqueued and running.
◆ get_num_threads()
NODISCARD unsigned int task_thread_pool::task_thread_pool::get_num_threads |
( |
| ) |
const |
|
inline |
Get number of worker threads.
- Returns
- Number of worker threads.
◆ is_paused()
NODISCARD bool task_thread_pool::task_thread_pool::is_paused |
( |
| ) |
const |
|
inline |
Check whether the pool is paused.
- Returns
- true if pause() has been called without an intervening unpause().
◆ pause()
void task_thread_pool::task_thread_pool::pause |
( |
| ) |
|
|
inline |
Stop executing queued tasks.
Use unpause()
to resume. Note: Destroying the pool will implicitly unpause.
Any in-progress tasks continue executing.
◆ start_threads()
void task_thread_pool::task_thread_pool::start_threads |
( |
const unsigned int |
num_threads | ) |
|
|
inlineprotected |
Start worker threads.
- Parameters
-
num_threads | How many threads to start. |
◆ submit()
template<typename F , typename... A, typename R = typename std::result_of<decay_t<F>(decay_t<A>...>
NODISCARD std::future<R> task_thread_pool::task_thread_pool::submit |
( |
F && |
func, |
|
|
A &&... |
args |
|
) |
| |
|
inline |
Submit a Callable for the pool to execute and return a std::future.
- Parameters
-
func | The Callable to execute. Can be a function, a lambda, std::packaged_task, std::function, etc. |
args | Arguments for func. Optional. |
- Returns
- std::future that can be used to get func's return value or thrown exception.
◆ submit_detach() [1/2]
template<typename F >
void task_thread_pool::task_thread_pool::submit_detach |
( |
F && |
func | ) |
|
|
inline |
Submit a zero-argument Callable for the pool to execute.
- Parameters
-
func | The Callable to execute. Can be a function, a lambda, std::packaged_task, std::function, etc. |
◆ submit_detach() [2/2]
template<typename F , typename... A>
void task_thread_pool::task_thread_pool::submit_detach |
( |
F && |
func, |
|
|
A &&... |
args |
|
) |
| |
|
inline |
Submit a Callable with arguments for the pool to execute.
- Parameters
-
func | The Callable to execute. Can be a function, a lambda, std::packaged_task, std::function, etc. |
◆ wait_for_queued_tasks()
void task_thread_pool::task_thread_pool::wait_for_queued_tasks |
( |
| ) |
|
|
inline |
Block until the task queue is empty.
Some tasks may be in-progress when this method returns.
◆ notify_task_finish
bool task_thread_pool::task_thread_pool::notify_task_finish = false |
|
protected |
A signal for worker threads that they should notify task_finished_cv when they finish a task.
Access protected by task_mutex.
◆ num_inflight_tasks
int task_thread_pool::task_thread_pool::num_inflight_tasks = 0 |
|
protected |
A counter of the number of tasks in-progress by worker threads.
Incremented when a task is popped off the task queue and decremented when that task is complete.
Access protected by task_mutex.
◆ pool_paused
bool task_thread_pool::task_thread_pool::pool_paused = false |
|
protected |
A signal for worker threads to not pull new tasks from the queue.
Access protected by task_mutex.
◆ pool_running
bool task_thread_pool::task_thread_pool::pool_running = true |
|
protected |
A signal for worker threads that the pool is either running or shutting down.
Access protected by task_mutex.
◆ tasks
std::queue<std::packaged_task<void()> > task_thread_pool::task_thread_pool::tasks = {} |
|
protected |
The task queue.
Access protected by task_mutex.
◆ threads
std::vector<std::thread> task_thread_pool::task_thread_pool::threads |
|
protected |
The worker threads.
Access protected by thread_mutex
The documentation for this class was generated from the following file:
- /builds/uwsbel/chrono/src/chrono_thirdparty/fast_matrix_market/thirdparty/task_thread_pool.hpp