WorkDispatcher Class Reference

A work dispatcher runs concurrent tasks. More...

Public Member Functions

WORK_API WorkDispatcher ()
 Construct a new dispatcher. More...
 
WORK_API ~WorkDispatcher ()
 Wait() for any pending tasks to complete, then destroy the dispatcher. More...
 
 WorkDispatcher (WorkDispatcher const &)=delete
 
WorkDispatcheroperator= (WorkDispatcher const &)=delete
 
template<class Callable , class A1 , class A2 , ... class AN>
void Run (Callable &&c, A1 &&a1, A2 &&a2,... AN &&aN)
 Add work for the dispatcher to run. More...
 
WORK_API void Wait ()
 Block until the work started by Run() completes. More...
 
WORK_API void Cancel ()
 Cancel remaining work and return immediately. More...
 

Detailed Description

A work dispatcher runs concurrent tasks.

The dispatcher supports adding new tasks from within running tasks. This suits problems that exhibit hierarchical structured parallelism: tasks that discover additional tasks during their execution.

Typical use is to create a dispatcher and invoke Run() to begin doing work, then Wait() for the work to complete. Tasks may invoke Run() during their execution as they discover additional tasks to perform.

For example,

WorkDispatcher dispatcher;
for (i = 0; i != N; ++i) {
dispatcher.Run(DoSomeWork, workItem[i]);
}
dispatcher.Wait();

Calls to Run() and Cancel() may be made concurrently. Calls to Wait() may also be made concurrently. However, once any calls to Wait() are in-flight, calls to Run() and Cancel() must only be made by tasks already added by Run(). This means that users of this class are responsible to synchronize concurrent calls to Wait() to ensure this requirement is met.

Additionally, Wait() must never be called by a task added by Run(), since that task could never complete.

Definition at line 75 of file dispatcher.h.

Constructor & Destructor Documentation

◆ WorkDispatcher()

WORK_API WorkDispatcher ( )

Construct a new dispatcher.

◆ ~WorkDispatcher()

WORK_API ~WorkDispatcher ( )

Wait() for any pending tasks to complete, then destroy the dispatcher.

Member Function Documentation

◆ Cancel()

WORK_API void Cancel ( )

Cancel remaining work and return immediately.

Calling this function affects task that are being run directly by this dispatcher. If any of these tasks are using their own dispatchers to run tasks, these dispatchers will not be affected and these tasks will run to completion, unless they are also explicitly cancelled.

This call does not block. Call Wait() after Cancel() to wait for pending tasks to complete.

◆ Run()

void Run ( Callable &&  c,
A1 &&  a1,
A2 &&  a2,
... AN &&  aN 
)

Add work for the dispatcher to run.

Before a call to Wait() is made it is safe for any client to invoke Run(). Once Wait() is invoked, it is only safe to invoke Run() from within the execution of tasks already added via Run().

This function does not block, in general. It may block if concurrency is limited to 1. The added work may be not yet started, may be started but not completed, or may be completed upon return. No guarantee is made.

◆ Wait()

WORK_API void Wait ( )

Block until the work started by Run() completes.


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