tf::Taskflow class

main entry to create a task dependency graph

A taskflow manages a task dependency graph where each task represents a callable object (e.g., , ) and an edge represents a dependency between two tasks. A task is one of the following types:

  1. static task: the callable constructible from std::function<void()>
  2. dynamic task: the callable constructible from std::function<void(tf::Subflow&)>
  3. condition task: the callable constructible from std::function<int()>
  4. module task: the task constructed from tf::Taskflow::composed_of
  5. cudaFlow task: the callable constructible from std::function<void(tf::cudaFlow&)> or std::function<void(tf::cudaFlowCapturer&)>

Each task is a basic computation unit and is run by one worker thread from an executor. The following example creates a simple taskflow graph of four static tasks, A, B, C, and D, where A runs before B and C and D runs after B and C.

tf::Executor executor;
tf::Taskflow taskflow("simple");

tf::Task A = taskflow.emplace([](){ std::cout << "TaskA\n"; }); 
tf::Task B = taskflow.emplace([](){ std::cout << "TaskB\n"; });
tf::Task C = taskflow.emplace([](){ std::cout << "TaskC\n"; });
tf::Task D = taskflow.emplace([](){ std::cout << "TaskD\n"; });

A.precede(B, C);  // A runs before B and C
D.succeed(B, C);  // D runs after  B and C
                                   
executor.run(taskflow).wait();

Please refer to Cookbook to learn more about each task type and how to submit a taskflow to an executor.

Base classes

class FlowBuilder
building methods of a task dependency graph

Constructors, destructors, conversion operators

Taskflow(const std::string& name)
constructs a taskflow with the given name
Taskflow()
constructs a taskflow
~Taskflow() defaulted
default destructor

Public functions

void dump(std::ostream& ostream) const
dumps the taskflow to a DOT format through a std::ostream target
auto dump() const -> std::string
dumps the taskflow to a std::string of DOT format
auto num_tasks() const -> size_t
queries the number of tasks
auto empty() const -> bool
queries the emptiness of the taskflow
void name(const std::string&)
assigns a name to the taskflow
auto name() const -> const std::string&
queries the name of the taskflow
void clear()
clears the associated task dependency graph
template<typename V>
void for_each_task(V&& visitor) const
applies a visitor to each task in the taskflow

Function documentation

tf::Taskflow::~Taskflow() defaulted

default destructor

When the destructor is called, all tasks and their associated data (e.g., captured data) will be destroyed. It is your responsibility to ensure all submitted execution of this taskflow have completed before destroying it.

void tf::Taskflow::clear()

clears the associated task dependency graph

When you clear a taskflow, all tasks and their associated data (e.g., captured data) will be destroyed. You should never clean a taskflow while it is being run by an executor.

template<typename V>
void tf::Taskflow::for_each_task(V&& visitor) const

applies a visitor to each task in the taskflow

A visitor is a callable that takes an argument of type tf::Task and returns nothing. The following example iterates each task in a taskflow and prints its name:

taskflow.for_each_task([](tf::Task task){
  std::cout << task.name() << '\n';
});