LibraryLink Utilities  3.0.1
Modern C++ wrapper over LibraryLink and WSTP
LLU::Async::BasicThreadPool< Queue > Class Template Reference

Description

template<typename Queue>
class LLU::Async::BasicThreadPool< Queue >

Simple thread pool class with a single queue.

Threads block on the queue if there is no work to do.

Template Parameters
Queue- any threadsafe queue class that provides push and waitPop methods

#include <ThreadPool.h>

Public Types

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

Public Member Functions

 BasicThreadPool ()
 Create a BasicThreadPool with the default number of threads (equal to the hardware concurrency) More...
 
 BasicThreadPool (unsigned threadCount)
 Create a BasicThreadPool with given number of threads. More...
 
 BasicThreadPool (const BasicThreadPool &)=delete
 
BasicThreadPooloperator= (const BasicThreadPool &)=delete
 
 BasicThreadPool (BasicThreadPool &&)=delete
 
BasicThreadPooloperator= (BasicThreadPool &&)=delete
 
 ~BasicThreadPool ()
 Destructor sets the "done" flag and unblocks all blocked threads by queuing a proper number of special tasks. 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...
 

Type aliases documentation

◆ TaskType

template<typename Queue >
using LLU::Async::BasicThreadPool< Queue >::TaskType = typename Queue::value_type

Type of the tasks processed by the Queue.

Constructor & Destructor Documentation

◆ BasicThreadPool() [1/2]

template<typename Queue >
LLU::Async::BasicThreadPool< Queue >::BasicThreadPool ( )
inline

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

◆ BasicThreadPool() [2/2]

template<typename Queue >
LLU::Async::BasicThreadPool< Queue >::BasicThreadPool ( unsigned  threadCount)
inlineexplicit

Create a BasicThreadPool with given number of threads.

Parameters
threadCount- requested number of threads in the pool

◆ ~BasicThreadPool()

template<typename Queue >
LLU::Async::BasicThreadPool< Queue >::~BasicThreadPool ( )
inline

Destructor sets the "done" flag and unblocks all blocked threads by queuing a proper number of special tasks.

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

Member Function Documentation

◆ runPendingTask()

template<typename Queue >
void LLU::Async::BasicThreadPool< Queue >::runPendingTask ( )
inline

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

◆ submit()

template<typename Queue >
template<typename FunctionType , typename... Args>
std::future<std::invoke_result_t<FunctionType, Args...> > LLU::Async::BasicThreadPool< Queue >::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