Taskflow
2.4-master-branch
|
After you create a task dependency graph, you need to submit it to threads for execution. In this chapter, we will show you how to execute a task dependency graph.
To execute a taskflow, you need to create an executor of type tf::Executor. An executor is a thread-safe object that manages a set of worker threads and executes tasks through an efficient work-stealing algorithm. Issuing a call to run a taskflow creates a topology, a data structure to keep track of the execution status of a running graph. tf::Executor takes an unsigned integer to construct with N
worker threads. The default value is std::thread::hardware_concurrency.
An executor can be reused to execute multiple taskflows. In most workloads, you may need only one executor to run multiple taskflows where each taskflow represents a part of a parallel decomposition.
tf::Executor provides a set of run_*
methods, tf::Executor::run, tf::Executor::run_n, and tf::Executor::run_until to run a taskflow for one time, multiple times, or until a given predicate evaluates to true. All methods accept an optional callback to invoke after the execution completes, and return a std::future for users to access the execution status. The code below shows several ways to run a taskflow.
Debrief:
Issuing multiple runs on the same taskflow will automatically synchronize to a sequential chain of executions in the order of run calls.
A key point to notice is a running taskflow must remain alive during its execution. It is your responsibility to ensure a taskflow not being destructed when it is running. For example, the code below can result undefined behavior.
Similarly, you should avoid touching a taskflow while it is running.
A rule of thumb is to always keep a taskflow alive in your function scope while it is participating in an execution.
All run_*
methods are thread-safe. Touching an executor from multiple threads is acceptable. You can have multiple threads call the same executor to run different taskflows.
Again, it is your responsibility to ensure all taskflows from different threads remain alive during their executions; or it can result unexpected behavior or program crash.