Thread pool utility class.
More...
#include <threadPool.hpp>
Thread pool utility class.
The pool runs a fixed number of worker threads which execute the queued tasks. Number of threads can be parameterized in constructor. If not set, it will be equal to the number of processors (or 1 if it can not be detected).
Tasks are queued with add and executed once the pool has been started with start. Exceptions thrown by tasks are ignored.
stop and awaitTermination must not be called from a task (they wait for running tasks).
The destructor drops queued tasks and waits for running ones.
- Author
- Vincent Lachenal
- Since
- 0.1
◆ ThreadPool() [1/3]
| anch::ThreadPool::ThreadPool |
( |
uint32_t | maxThreads = 0 | ) |
|
ThreadPool constructor
- Parameters
-
| maxThreads | the number of worker threads (default number of processor or 1 if not defined) |
◆ ThreadPool() [2/3]
| anch::ThreadPool::ThreadPool |
( |
const ThreadPool & | | ) |
|
|
delete |
◆ ThreadPool() [3/3]
| anch::ThreadPool::ThreadPool |
( |
ThreadPool && | | ) |
|
|
delete |
◆ ~ThreadPool()
| anch::ThreadPool::~ThreadPool |
( |
| ) |
|
ThreadPool destructor
Queued tasks are dropped ; running tasks are awaited.
◆ add()
template<typename Func, typename... Args>
| bool anch::ThreadPool::add |
( |
Func | function, |
|
|
Args... | args ) |
Add a new task in queue.
Function and arguments are moved into the task: they can be move-only.
- Parameters
-
| function | the function which will be called in a worker thread |
| args | the function arguments |
- Returns
true if the task has been queued, false if the pool is awaiting its termination
◆ awaitTermination()
template<typename R, typename P = std::ratio<1>>
| void anch::ThreadPool::awaitTermination |
( |
std::chrono::duration< R, P > | duration | ) |
|
Await thread pool termination.
Wait for queued tasks execution until duration is reached. Then queued tasks which have not been started are dropped, running tasks are awaited (they can not be interrupted) and the pool is stopped.
If the pool is not started, queued tasks are dropped.
New tasks are rejected while awaiting termination.
- Parameters
-
| duration | the maximum duration to wait for queued tasks. If <1 wait indefinitly. |
◆ operator=() [1/2]
◆ operator=() [2/2]
◆ start()
| void anch::ThreadPool::start |
( |
| ) |
|
Start executing tasks in queue
◆ stop()
| void anch::ThreadPool::stop |
( |
| ) |
|
Stop executing tasks in queue.
Wait for running tasks. Queued tasks are kept and will be executed on next start call.
The documentation for this class was generated from the following file: