Cpp-Taskflow  2.3.0
tf::Executor Class Reference

The executor class to run a taskflow graph. More...

#include <executor.hpp>

Public Member Functions

 Executor (unsigned n=std::thread::hardware_concurrency())
 constructs the executor with N worker threads
 
 ~Executor ()
 destructs the executor
 
std::future< void > run (Taskflow &taskflow)
 runs the taskflow once More...
 
template<typename C >
std::future< void > run (Taskflow &taskflow, C &&callable)
 runs the taskflow once and invoke a callback upon completion More...
 
std::future< void > run_n (Taskflow &taskflow, size_t N)
 runs the taskflow for N times More...
 
template<typename C >
std::future< void > run_n (Taskflow &taskflow, size_t N, C &&callable)
 runs the taskflow for N times and then invokes a callback More...
 
template<typename P >
std::future< void > run_until (Taskflow &taskflow, P &&pred)
 runs the taskflow multiple times until the predicate becomes true and then invokes a callback More...
 
template<typename P , typename C >
std::future< void > run_until (Taskflow &taskflow, P &&pred, C &&callable)
 runs the taskflow multiple times until the predicate becomes true and then invokes the callback More...
 
void wait_for_all ()
 wait for all pending graphs to complete
 
size_t num_workers () const
 queries the number of worker threads (can be zero) More...
 
template<typename Observer , typename... Args>
Observer * make_observer (Args &&... args)
 constructs an observer to inspect the activities of worker threads More...
 
void remove_observer ()
 removes the associated observer
 
int this_worker_id () const
 queries the id of the caller thread in this executor More...
 

Detailed Description

The executor class to run a taskflow graph.

An executor object manages a set of worker threads and implements an efficient work-stealing scheduling algorithm to run a task graph.

Member Function Documentation

◆ make_observer()

template<typename Observer , typename... Args>
Observer * tf::Executor::make_observer ( Args &&...  args)

constructs an observer to inspect the activities of worker threads

Each executor manages at most one observer at a time through std::unique_ptr. Createing multiple observers will only keep the lastest one.

Template Parameters
Observerobserver type derived from tf::ExecutorObserverInterface
ArgsT...argument parameter pack
Parameters
argsarguments to forward to the constructor of the observer
Returns
a raw pointer to the observer associated with this executor

◆ num_workers()

size_t tf::Executor::num_workers ( ) const
inline

queries the number of worker threads (can be zero)

Returns
the number of worker threads

◆ run() [1/2]

std::future< void > tf::Executor::run ( Taskflow taskflow)
inline

runs the taskflow once

Parameters
taskflowa tf::Taskflow object
Returns
a std::future to access the execution state of the taskflow

◆ run() [2/2]

template<typename C >
std::future< void > tf::Executor::run ( Taskflow taskflow,
C &&  callable 
)

runs the taskflow once and invoke a callback upon completion

Parameters
taskflowa tf::Taskflow object
callablea callable object to be invoked after this run
Returns
a std::future to access the execution state of the taskflow

◆ run_n() [1/2]

std::future< void > tf::Executor::run_n ( Taskflow taskflow,
size_t  N 
)
inline

runs the taskflow for N times

Parameters
taskflowa tf::Taskflow object
Nnumber of runs
Returns
a std::future to access the execution state of the taskflow

◆ run_n() [2/2]

template<typename C >
std::future< void > tf::Executor::run_n ( Taskflow taskflow,
size_t  N,
C &&  callable 
)

runs the taskflow for N times and then invokes a callback

Parameters
taskflowa tf::Taskflow
Nnumber of runs
callablea callable object to be invoked after this run
Returns
a std::future to access the execution state of the taskflow

◆ run_until() [1/2]

template<typename P >
std::future< void > tf::Executor::run_until ( Taskflow taskflow,
P &&  pred 
)

runs the taskflow multiple times until the predicate becomes true and then invokes a callback

Parameters
taskflowa tf::Taskflow
preda boolean predicate to return true for stop
Returns
a std::future to access the execution state of the taskflow

◆ run_until() [2/2]

template<typename P , typename C >
std::future< void > tf::Executor::run_until ( Taskflow taskflow,
P &&  pred,
C &&  callable 
)

runs the taskflow multiple times until the predicate becomes true and then invokes the callback

Parameters
taskflowa tf::Taskflow
preda boolean predicate to return true for stop
callablea callable object to be invoked after this run
Returns
a std::future to access the execution state of the taskflow

◆ this_worker_id()

int tf::Executor::this_worker_id ( ) const
inline

queries the id of the caller thread in this executor

Each worker has an unique id from 0 to N-1 exclusive to the associated executor. If the caller thread does not belong to the executor, -1 is returned.


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