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

#include <task_set.h>

Inherits TaskSetBase.

Public Member Functions

 ConcurrentTaskSet (ThreadPool &pool, ParentCascadeCancel registerForParentCancel, ssize_t stealingLoadMultiplier=kDefaultStealingMultiplier, TaskCost cost=TaskCost::kHeavy)
 
 ConcurrentTaskSet (ThreadPool &p)
 
 ConcurrentTaskSet (ThreadPool &p, TaskCost cost)
 
 ConcurrentTaskSet (ThreadPool &p, ssize_t stealingLoadMultiplier)
 
 ConcurrentTaskSet (ThreadPool &p, TaskCost cost, ssize_t stealingLoadMultiplier)
 
 ConcurrentTaskSet (ConcurrentTaskSet &&other)=delete
 
template<typename F >
void schedule (F &&f, bool skipRecheck=false, float poolRecursiveLoadFactor=kDefaultPoolRecursiveLoadFactor)
 
template<typename F >
void schedule (F &&f, ForceQueuingTag fq)
 
template<typename Generator >
void scheduleBulk (size_t count, Generator &&gen)
 
template<typename Generator >
void scheduleBulk (size_t count, Generator &&gen, ForceQueuingTag)
 
DISPENSO_DLL_ACCESS bool wait ()
 
DISPENSO_DLL_ACCESS bool tryWait (size_t maxToExecute)
 
void cancel ()
 
bool canceled () const
 
 ~ConcurrentTaskSet ()
 

Detailed Description

ConcurrentTaskSet fulfills the same API as TaskSet with one minor difference: It may be used to schedule tasks concurrently from multiple threads (see more below). It is an object that allows scheduling multiple function-like objects to a thread pool, and allows to wait on that set of tasks.

ConcurrentTaskSet is "thread-compatible". This means that you can safely use different ConcurrentTaskSet objects on different threads concurrently. ConcurrentTaskSet also allows multiple threads to concurrently schedule against it. It is an error to call wait() concurrently with schedule() on the same ConcurrentTaskSet.

Definition at line 241 of file task_set.h.

Constructor & Destructor Documentation

◆ ConcurrentTaskSet() [1/5]

dispenso::ConcurrentTaskSet::ConcurrentTaskSet ( ThreadPool & pool,
ParentCascadeCancel registerForParentCancel,
ssize_t stealingLoadMultiplier = kDefaultStealingMultiplier,
TaskCost cost = TaskCost::kHeavy )
inline

Construct a ConcurrentTaskSet with the given backing pool.

Parameters
poolThe backing pool for this ConcurrentTaskSet
registerForParentCancelWhether to register for parent cancellation cascade.
stealingLoadMultiplierAn over-load factor. If this factor of load is reached by the underlying pool, scheduled tasks may run immediately in the calling thread.
costHint about per-task cost; see TaskCost.

Definition at line 252 of file task_set.h.

◆ ConcurrentTaskSet() [2/5]

dispenso::ConcurrentTaskSet::ConcurrentTaskSet ( ThreadPool & p)
inline

Construct a ConcurrentTaskSet with default options.

Parameters
pThe backing pool.

Definition at line 260 of file task_set.h.

◆ ConcurrentTaskSet() [3/5]

dispenso::ConcurrentTaskSet::ConcurrentTaskSet ( ThreadPool & p,
TaskCost cost )
inline

Construct a ConcurrentTaskSet with a task-cost hint.

Parameters
pThe backing pool.
costHint about per-task cost; see TaskCost.

Definition at line 264 of file task_set.h.

◆ ConcurrentTaskSet() [4/5]

dispenso::ConcurrentTaskSet::ConcurrentTaskSet ( ThreadPool & p,
ssize_t stealingLoadMultiplier )
inline

Construct a ConcurrentTaskSet with custom load multiplier.

Parameters
pThe backing pool.
stealingLoadMultiplierThe over-load factor.

Definition at line 268 of file task_set.h.

◆ ConcurrentTaskSet() [5/5]

dispenso::ConcurrentTaskSet::ConcurrentTaskSet ( ThreadPool & p,
TaskCost cost,
ssize_t stealingLoadMultiplier )
inline

Construct a ConcurrentTaskSet with a task-cost hint and custom load multiplier.

Definition at line 271 of file task_set.h.

◆ ~ConcurrentTaskSet()

dispenso::ConcurrentTaskSet::~ConcurrentTaskSet ( )
inline

Destroy the ConcurrentTaskSet, first waiting for all currently scheduled functors to finish execution.

Definition at line 438 of file task_set.h.

Member Function Documentation

◆ cancel()

void dispenso::ConcurrentTaskSet::cancel ( )
inline

Set the ConcurrentTaskSet to canceled state. No unexecuted tasks will execute once this is set. Already executing tasks may check canceled() status to exit early.

Note
This will be reset automatically by wait.

Definition at line 421 of file task_set.h.

◆ canceled()

bool dispenso::ConcurrentTaskSet::canceled ( ) const
inline

Check the canceled status of the ConcurrentTaskSet.

Returns
a boolean indicating whether or not the ConcurrentTaskSet has been canceled.

Definition at line 430 of file task_set.h.

◆ schedule() [1/2]

template<typename F >
void dispenso::ConcurrentTaskSet::schedule ( F && f,
bool skipRecheck = false,
float poolRecursiveLoadFactor = kDefaultPoolRecursiveLoadFactor )
inline

Schedule a functor for execution on the underlying pool. If the load on the underlying pool is high, immediate inline execution may occur on the current thread.

Parameters
fA functor matching signature void(). Best performance will come from passing lambdas, other concrete functors, or OnceFunction, but std::function or similarly type-erased objects will also work.
skipRecheckA poweruser knob that says that if we don't have enough outstanding tasks to immediately work steal, we should bypass the similar check in the ThreadPool.
poolRecursiveLoadFactorControls how aggressively pool threads inline work. Lower values (1.0) cause more inlining (pipeline-like), higher values (3.0) cause more distribution (graph-like).
Note
If f can throw exceptions, then schedule may throw if the task is run inline. Otherwise, exceptions will be caught on the running thread and best-effort propagated to the ConcurrentTaskSet, where the first one from the set is rethrown in wait.

Definition at line 299 of file task_set.h.

◆ schedule() [2/2]

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

Schedule a functor for execution on the underlying pool.

Parameters
fA functor matching signature void(). Best performance will come from passing lambdas, other concrete functors, or OnceFunction, but std::function or similarly type-erased objects will also work.
fqTag to force queuing instead of potential inline execution.
Note
If f can throw exceptions, then exceptions will be caught on the running thread and best-effort propagated to the ConcurrentTaskSet, where the first one from the set is rethrown in wait.

Definition at line 352 of file task_set.h.

◆ scheduleBulk() [1/2]

template<typename Generator >
void dispenso::ConcurrentTaskSet::scheduleBulk ( size_t count,
Generator && gen )
inline

Schedule multiple functors for execution on the underlying pool in bulk. This is more efficient than calling schedule() multiple times when you have many tasks to submit, as it reduces atomic contention and allows for better thread wakeup behavior.

Parameters
countThe number of functors to schedule.
genA generator functor that takes an index and returns a functor to execute. gen(i) will be called for i in [0, count) to produce each task.
Note
Work is processed in chunks, interleaving enqueue and inline execution based on the task set's load factor, preventing both pool thread starvation and excessive overhead.

Definition at line 373 of file task_set.h.

◆ scheduleBulk() [2/2]

template<typename Generator >
void dispenso::ConcurrentTaskSet::scheduleBulk ( size_t count,
Generator && gen,
ForceQueuingTag  )
inline

Schedule multiple functors for execution on the underlying pool in bulk, forcing each task to be queued rather than potentially run inline on the calling thread.

Parameters
countThe number of functors to schedule.
genA generator functor that takes an index and returns a functor to execute. gen(i) will be called for i in [0, count) to produce each task.

Definition at line 390 of file task_set.h.

◆ tryWait()

DISPENSO_DLL_ACCESS bool dispenso::ConcurrentTaskSet::tryWait ( size_t maxToExecute)

See if the currently scheduled functors can be completed while stealing and executing at most maxToExecute of them from the pool. If not used in conjunction with wait, there may be cases that tryWait must be called multiple times with maxToExecute &gt 0 to prevent livelock/deadlock. If exceptions have been propagated since the last call to wait or tryWait, tryWait will propagate the first of them.

Parameters
maxToExecuteThe maximum number of tasks to proactively execute on the current thread.
Returns
true if all currently scheduled functors have been completed prior to returning, and false otherwise (including cancelled cases).

◆ wait()

DISPENSO_DLL_ACCESS bool dispenso::ConcurrentTaskSet::wait ( )

Wait for all currently scheduled functors to finish execution. If exceptions are thrown during execution of the set of tasks, wait will propagate the first exception.


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