Cpp-Taskflow
2.1.0
|
In many situations, you will need to execute a task dependency multiple times to create a reusable and stateful execution flow. tf::Framework is designed for this purpose. This chapter introduces the concept of a framework and shows its usage.
The tf::Framework inherits tf::FlowBuilder and contains a task dependency graph that is independent of a taskflow object. The big difference from dispatching a graph from taskflow is executing a framework will not make the graph disappear. You can reuse the framework multiple times. The following example demonstrates how to create a framework.
To execute a framework, you use one of following methods: tf::Taskflow::run, tf::Taskflow::run_n, or tf::Taskflow::run_until to choose to run a framework for one time, multiple times, or until reaching a certain condition. All methods accept an optional callback to invoke after the execution completes. The code below shows how to run a framework.
Debrief:
cnt
variable becomes 10Since a taskflow object does not own a framework, a running framework must remain alive until the execution finishes. That is, it is your responsibility to ensure a framework object retain its life during it is running on a taskflow object. To make sure all frameworks have completed, you can use tf::Taskflow::wait_for_all to block the program until all tasks finish.
A useful feature of framework is that you can customize your own application framework by inheriting the tf::Framework class. By deriving from the tf::Framework, you can use the same task creation APIs to build a task dependency graph for your own application and call run_*
methods to execute your framework.
Although tf::Framework enables efficient reuse of a task dependency graph, there can be many potential pitfalls. First, a framework object is NOT thread-safe. Trying to touch a framework while it is running can result in undefined behavior.
Second, subgraphs spawned during dynamic tasking will be cleaned up automatically at the beginning of each execution of the framework.
Third, connecting the tasks from different frameworks can result in undefined behaviors.
We are still experimenting tf::Framework to develop a safe interface. Please stay tuned with Master Branch (GitHub).