AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
anch::ThreadPool Class Reference

Thread pool utility class. More...

#include <threadPool.hpp>

Collaboration diagram for anch::ThreadPool:

Public Member Functions

 ThreadPool (uint32_t maxThreads=0)
 ThreadPool (const ThreadPool &)=delete
 ThreadPool (ThreadPool &&)=delete
 ~ThreadPool ()
ThreadPool & operator= (const ThreadPool &)=delete
ThreadPool & operator= (ThreadPool &&)=delete
void start ()
void stop ()
template<typename R, typename P = std::ratio<1>>
void awaitTermination (std::chrono::duration< R, P > duration)
template<typename Func, typename... Args>
bool add (Func function, Args... args)

Detailed Description

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

Constructor & Destructor Documentation

◆ ThreadPool() [1/3]

anch::ThreadPool::ThreadPool ( uint32_t maxThreads = 0)

ThreadPool constructor

Parameters
maxThreadsthe number of worker threads (default number of processor or 1 if not defined)

◆ ThreadPool() [2/3]

anch::ThreadPool::ThreadPool ( const ThreadPool & )
delete

Prohibits ThreadPool copy constructor

Here is the call graph for this function:

◆ ThreadPool() [3/3]

anch::ThreadPool::ThreadPool ( ThreadPool && )
delete

Prohibits ThreadPool move constructor

Here is the call graph for this function:

◆ ~ThreadPool()

anch::ThreadPool::~ThreadPool ( )

ThreadPool destructor
Queued tasks are dropped ; running tasks are awaited.

Member Function Documentation

◆ 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
functionthe function which will be called in a worker thread
argsthe 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
durationthe maximum duration to wait for queued tasks. If <1 wait indefinitly.

◆ operator=() [1/2]

ThreadPool & anch::ThreadPool::operator= ( const ThreadPool & )
delete

Prohibits ThreadPool copy assignment

Here is the call graph for this function:

◆ operator=() [2/2]

ThreadPool & anch::ThreadPool::operator= ( ThreadPool && )
delete

Prohibits ThreadPool move assignment

Here is the call graph for this function:

◆ 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: