LibraryLink Utilities  3.0.1
Modern C++ wrapper over LibraryLink and WSTP
LLU::Async::GenericThreadPool< PoolQueue, LocalQueue > Class Template Reference

Description

template<typename PoolQueue, typename LocalQueue>
class LLU::Async::GenericThreadPool< PoolQueue, LocalQueue >

Thread pool class with support of per-thread queues and work stealing.

Based on A. Williams "C++ Concurrency in Action" 2nd Edition, chapter 9.

Template Parameters
PoolQueue- any threadsafe queue class that provides push and tryPop methods
LocalQueue- any threadsafe queue class that provides push, tryPop and trySteal methods
+ Inheritance diagram for LLU::Async::GenericThreadPool< PoolQueue, LocalQueue >:
+ Collaboration diagram for LLU::Async::GenericThreadPool< PoolQueue, LocalQueue >:

#include <ThreadPool.h>

Public Types

using TaskType = typename PoolQueue::value_type
 Type of the tasks processed by the Queue. More...
 

Public Member Functions

 GenericThreadPool ()
 Create a GenericThreadPool with the default number of threads (equal to the hardware concurrency) More...
 
 GenericThreadPool (unsigned threadCount)
 Create a GenericThreadPool with given number of threads. More...
 
 GenericThreadPool (const GenericThreadPool &)=delete
 
GenericThreadPooloperator= (const GenericThreadPool &)=delete
 
 GenericThreadPool (GenericThreadPool &&)=delete
 
GenericThreadPooloperator= (GenericThreadPool &&)=delete
 
 ~GenericThreadPool ()
 Destructor sets the "done" flag and notifies all paused threads. More...
 
template<typename FunctionType , typename... Args>
std::future< std::invoke_result_t< FunctionType, Args... > > submit (FunctionType &&f, Args &&... args)
 Main function of the pool which accepts tasks to be evaluated by the worker threads. More...
 
void runPendingTask ()
 This is the function that each worker thread runs in a loop. More...
 
void checkPause ()
 This is the function worker threads will call to see if the work has been paused. More...
 
void pause () noexcept
 Signal to pause work. More...
 
void resume () noexcept
 Signal to resume work and notify waiting worker threads. More...
 

Type aliases documentation

◆ TaskType

template<typename PoolQueue , typename LocalQueue >
using LLU::Async::GenericThreadPool< PoolQueue, LocalQueue >::TaskType = typename PoolQueue::value_type

Type of the tasks processed by the Queue.

Constructor & Destructor Documentation

◆ GenericThreadPool() [1/2]

template<typename PoolQueue , typename LocalQueue >
LLU::Async::GenericThreadPool< PoolQueue, LocalQueue >::GenericThreadPool ( )
inline

Create a GenericThreadPool with the default number of threads (equal to the hardware concurrency)

◆ GenericThreadPool() [2/2]

template<typename PoolQueue , typename LocalQueue >
LLU::Async::GenericThreadPool< PoolQueue, LocalQueue >::GenericThreadPool ( unsigned  threadCount)
inlineexplicit

Create a GenericThreadPool with given number of threads.

Parameters
threadCount- requested number of threads in the pool

◆ ~GenericThreadPool()

template<typename PoolQueue , typename LocalQueue >
LLU::Async::GenericThreadPool< PoolQueue, LocalQueue >::~GenericThreadPool ( )
inline

Destructor sets the "done" flag and notifies all paused threads.

Worker threads are joined in the destructor of Async::ThreadJoiner member

Member Function Documentation

◆ checkPause()

void LLU::Async::Pausable::checkPause ( )
inlineinherited

This is the function worker threads will call to see if the work has been paused.

If it has, they will wait on the condition variable.

◆ pause()

void LLU::Async::Pausable::pause ( )
inlinenoexceptinherited

Signal to pause work.

◆ resume()

void LLU::Async::Pausable::resume ( )
inlinenoexceptinherited

Signal to resume work and notify waiting worker threads.

◆ runPendingTask()

template<typename PoolQueue , typename LocalQueue >
void LLU::Async::GenericThreadPool< PoolQueue, LocalQueue >::runPendingTask ( )
inline

This is the function that each worker thread runs in a loop.

◆ submit()

template<typename PoolQueue , typename LocalQueue >
template<typename FunctionType , typename... Args>
std::future<std::invoke_result_t<FunctionType, Args...> > LLU::Async::GenericThreadPool< PoolQueue, LocalQueue >::submit ( FunctionType &&  f,
Args &&...  args 
)
inline

Main function of the pool which accepts tasks to be evaluated by the worker threads.

A task is simply a deferred evaluation of a function call.

Template Parameters
FunctionType- type of the function to be called in a worker thread
Args- argument types of the submitted task
Parameters
f- function to be called as the task
args- argument to the function call
Returns
a future result of calling f on args