dispenso
A library for task parallelism
 
Loading...
Searching...
No Matches
dispenso::ThreadPool Class Reference

#include <thread_pool.h>

Public Member Functions

DISPENSO_DLL_ACCESS ThreadPool (size_t n, size_t poolLoadMultiplier=32)
 
template<class Rep , class Period >
void setSignalingWake (bool enable, const std::chrono::duration< Rep, Period > &sleepDuration=std::chrono::microseconds(kDefaultSleepLenUs))
 
DISPENSO_DLL_ACCESS void resize (ssize_t n)
 
ssize_t numThreads () const
 
template<typename F >
void schedule (F &&f)
 
template<typename F >
void schedule (F &&f, ForceQueuingTag)
 
DISPENSO_DLL_ACCESS ~ThreadPool ()
 

Static Public Member Functions

static voidoperator new (size_t sz)
 
static void operator delete (void *ptr)
 

Friends

class ConcurrentTaskSet
 
class TaskSet
 
class TaskSetBase
 

Detailed Description

The basic executor for dispenso. It provides typical thread pool functionality, plus allows work stealing by related types (e.g. TaskSet, Future, etc...), which prevents deadlock when waiting for pool-recursive tasks.

Definition at line 73 of file thread_pool.h.

Constructor & Destructor Documentation

◆ ThreadPool()

dispenso::ThreadPool::ThreadPool ( size_t  n,
size_t  poolLoadMultiplier = 32 
)

Construct a thread pool.

Parameters
nThe number of threads to spawn at construction.
poolLoadMultiplierA parameter that specifies how overloaded the pool should be before allowing the current thread to self-steal work.

Definition at line 70 of file thread_pool.cpp.

◆ ~ThreadPool()

dispenso::ThreadPool::~ThreadPool ( )

Destruct the pool. This destructor is blocking until all queued work is completed. It is illegal to call the destructor while any other thread makes calls to the pool (as is generally the case with C++ classes).

Definition at line 197 of file thread_pool.cpp.

Member Function Documentation

◆ numThreads()

ssize_t dispenso::ThreadPool::numThreads ( ) const
inline

Get the number of threads backing the pool. If called concurrently to resize, the number returned may be stale.

Returns
The current number of threads backing the pool.

Definition at line 130 of file thread_pool.h.

◆ operator delete()

static void dispenso::ThreadPool::operator delete ( void ptr)
inlinestatic

Definition at line 223 of file thread_pool.h.

◆ operator new()

static void * dispenso::ThreadPool::operator new ( size_t  sz)
inlinestatic

Definition at line 220 of file thread_pool.h.

◆ resize()

DISPENSO_DLL_ACCESS void dispenso::ThreadPool::resize ( ssize_t  n)
inline

Change the number of threads backing the thread pool. This is a blocking and potentially slow operation, and repeatedly resizing is discouraged.

Parameters
nThe number of threads in use after call completion

Definition at line 119 of file thread_pool.h.

◆ schedule() [1/2]

template<typename F >
void dispenso::ThreadPool::schedule ( F &&  f)
inline

Schedule a functor to be executed. If the pool's load factor is high, execution may happen inline by the calling thread.

Parameters
fThe functor to be executed. f's signature must match void(). Best performance will come from passing lambdas, other concrete functors, or OnceFunction, but std::function or similarly type-erased objects will also work.

Definition at line 272 of file thread_pool.h.

◆ schedule() [2/2]

template<typename F >
void dispenso::ThreadPool::schedule ( F &&  f,
ForceQueuingTag   
)
inline

Schedule a functor to be executed. The functor will always be queued and executed by pool threads.

Parameters
fThe functor to be executed. f's signature must match void(). Best performance will come from passing lambdas, other concrete functors, or OnceFunction, but std::function or similarly type-erased objects will also work.

Definition at line 285 of file thread_pool.h.

◆ setSignalingWake()

template<class Rep , class Period >
void dispenso::ThreadPool::setSignalingWake ( bool  enable,
const std::chrono::duration< Rep, Period > &  sleepDuration = std::chrono::microseconds(kDefaultSleepLenUs) 
)
inline

Enable or disable signaling wake functionality. If enabled, this will try to ensure that threads are woken up proactively when work has not been available and it becomes available. This function is blocking and potentially very slow. Repeated use is discouraged.

Parameters
enableIf set true, turns on signaling wake. If false, turns it off.
sleepDurationIf enable is true, this is the length of time a thread will wait for a signal before waking up. If enable is false, this is the length of time a thread will sleep between polling.
Note
It is highly recommended to leave signaling wake enabled on Windows platforms, as sleeping/polling tends to perform poorly for intermittent workloads. For Mac/Linux platforms, it is okay to enable signaling wake, particularly if you wish to set a longer expected duration between work. If signaling wake is disabled, ensure sleepDuration is small (e.g. 200us) for best performance. Most users will not need to call this function, as defaults are reasonable.

Definition at line 103 of file thread_pool.h.

Friends And Related Symbol Documentation

◆ ConcurrentTaskSet

Definition at line 250 of file thread_pool.h.

◆ TaskSet

Definition at line 251 of file thread_pool.h.

◆ TaskSetBase

friend class TaskSetBase
friend

Definition at line 252 of file thread_pool.h.


The documentation for this class was generated from the following files: