![]() |
Taskflow
3.2.0-Master-Branch
|
This chapters discusses how to launch tasks asynchronously so that you can incorporate independent, dynamic parallelism in your taskflows.
Taskflow executor provides a STL-styled method, tf::Executor::async, for you to run a callable object asynchronously. The method returns a tf::Future object derived from std::future that will eventually hold the result of that function call. The result may be optional due to tf::Future::cancel (see Request Cancellation for details).
If you do not need the return value or the future, you can use tf::Executor::silent_async which has less overhead of creating an asynchronous task compared to tf::Executor::async.
Launching asynchronous tasks from an executor is thread-safe and can be called from multiple threads or from the execution of a task. Our scheduler autonomously detects whether an asynchronous task is submitted from an external thread or a worker thread and schedules its execution in an efficient work-stealing loop.
You can name an asynchronous task to facilitate profiling by using the methods tf::Executor::named_async and tf::Executor::named_silent_async.
You can launch asynchronous tasks from a subflow (tf::Subflow) using tf::Subflow::async. Asynchronous tasks created from a subflow are, and only, used with join (tf::Subflow::join) to describe independent tasks that are dynamically spawned during the execution of that subflow. When the subflow joins, all asynchronous tasks are guaranteed to finish. The following code creates 100 asynchronous tasks from a subflow, and these asynchronous tasks will complete by the time the subflow joins.
If you do not need the return value or the future, you can use tf::Subflow::silent_async which has less overhead of creating an asynchronous task compared to tf::Subflow::async.
Creating asynchronous tasks from a subflow allows users to describe, for example, recursive algorithms that define only division without conquering or merging (e.g., parallel quick sort).
Similar to tf::Executor::named_async and tf::Executor::named_silent_async, you can name an asynchronous task in tf::Subflow to facilitate profiling by using the methods tf::Subflow::named_async and tf::Subflow::named_silent_async.