<li><ahref="#LaunchAsynchronousTasksFromAnExecutor">Launch Asynchronous Tasks from an Executor</a></li>
<li><ahref="#LaunchAsynchronousTasksFromAnSubflow">Launch Asynchronous Tasks from a Subflow</a></li>
<li><ahref="#LaunchAsynchronousTasksFromARuntime">Launch Asynchronous Tasks from a Runtime</a></li>
</ul>
</nav>
<p>This chapters discusses how to launch tasks asynchronously so that you can incorporate independent, dynamic parallelism in your taskflows.</p><sectionid="LaunchAsynchronousTasksFromAnExecutor"><h2><ahref="#LaunchAsynchronousTasksFromAnExecutor">Launch Asynchronous Tasks from an Executor</a></h2><p>Taskflow executor provides a STL-styled method, <ahref="classtf_1_1Executor.html#a28bdb43837bd6b548e092154e4df5dd9" class="m-doc">tf::<wbr/>Executor::<wbr/>async</a>, for you to run a callable object asynchronously. The method returns a <ahref="https://en.cppreference.com/w/cpp/thread/future">std::<wbr/>future</a> that will eventually hold the result of that function call.</p><preclass="m-code"><spanclass="n">std</span><spanclass="o">::</span><spanclass="n">future</span><spanclass="o"><</span><spanclass="kt">int</span><spanclass="o">></span><spanclass="w"></span><spanclass="n">future</span><spanclass="w"></span><spanclass="o">=</span><spanclass="w"></span><spanclass="n">executor</span><spanclass="p">.</span><spanclass="n">async</span><spanclass="p">([](){</span><spanclass="w"></span><spanclass="k">return</span><spanclass="w"></span><spanclass="mi">1</span><spanclass="p">;</span><spanclass="w"></span><spanclass="p">});</span><spanclass="w"></span>
<spanclass="n">assert</span><spanclass="p">(</span><spanclass="n">future</span><spanclass="p">.</span><spanclass="n">get</span><spanclass="p">()</span><spanclass="w"></span><spanclass="o">==</span><spanclass="w"></span><spanclass="mi">1</span><spanclass="p">);</span><spanclass="w"></span></pre><asideclass="m-note m-info"><h4>Note</h4><p>Unlike std::async, the future object returned from <ahref="classtf_1_1Executor.html#a28bdb43837bd6b548e092154e4df5dd9" class="m-doc">tf::<wbr/>Executor::<wbr/>async</a> does not block on destruction until completing the function.</p></aside><p>If you do not need the return value or use a future to synchronize the execution, you are encouraged to use <ahref="classtf_1_1Executor.html#a878ec1bc337c7efe22619b21ba3ecdf3" class="m-doc">tf::<wbr/>Executor::<wbr/>silent_async</a> which returns nothing and thus has less overhead (i.e., no shared state management) compared to <ahref="classtf_1_1Executor.html#a28bdb43837bd6b548e092154e4df5dd9" class="m-doc">tf::<wbr/>Executor::<wbr/>async</a>.</p><preclass="m-code"><spanclass="n">executor</span><spanclass="p">.</span><spanclass="n">silent_async</span><spanclass="p">([](){</span><spanclass="w"></span>
<spanclass="w"></span><spanclass="c1">// do some work without returning any result</span>
<spanclass="p">});</span><spanclass="w"></span></pre><p>Launching asynchronous tasks from an executor is <em>thread-safe</em> and can be called by multiple threads both inside (i.e., worker) and outside the executor. Our scheduler autonomously detects whether an asynchronous task is submitted from an external thread or a worker thread and schedules its execution using work stealing.</p><preclass="m-code"><spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">Task</span><spanclass="w"></span><spanclass="n">my_task</span><spanclass="w"></span><spanclass="o">=</span><spanclass="w"></span><spanclass="n">taskflow</span><spanclass="p">.</span><spanclass="n">emplace</span><spanclass="p">([</span><spanclass="o">&</span><spanclass="p">](){</span><spanclass="w"></span>
<spanclass="w"></span><spanclass="c1">// launch an asynchronous task from my_task</span>
<spanclass="n">executor</span><spanclass="p">.</span><spanclass="n">wait_for_all</span><spanclass="p">();</span><spanclass="w"></span><spanclass="c1">// wait for all tasks to finish</span></pre><asideclass="m-note m-info"><h4>Note</h4><p>Asynchronous tasks created from an executor does not belong to any taskflows. The lifetime of an asynchronous task is managed automatically by the executor that creates the task.</p></aside><p>You can name an asynchronous task using the overloads, <ahref="classtf_1_1Executor.html#ac757049ee532d51caadd1dffb21fbbe4" class="m-doc">tf::<wbr/>Executor::<wbr/>async(const std::<wbr/>string& name, F&& f)</a> and <ahref="classtf_1_1Executor.html#ab779301a5f3ff57b34e8fd72965a261f" class="m-doc">tf::<wbr/>Executor::<wbr/>silent_async(const std::<wbr/>string& name, F&& f)</a>, that take a string in the first argument. Assigned names will appear in the observers of the executor.</p><preclass="m-code"><spanclass="n">std</span><spanclass="o">::</span><spanclass="n">future</span><spanclass="o"><</span><spanclass="kt">void</span><spanclass="o">></span><spanclass="w"></span><spanclass="n">fu</span><spanclass="w"></span><spanclass="o">=</span><spanclass="w"></span><spanclass="n">executor</span><spanclass="p">.</span><spanclass="n">async</span><spanclass="p">(</span><spanclass="s">"async task"</span><spanclass="p">,</span><spanclass="w"></span><spanclass="p">[](){});</span><spanclass="w"></span>
<spanclass="n">executor</span><spanclass="p">.</span><spanclass="n">silent_async</span><spanclass="p">(</span><spanclass="s">"sileng async task"</span><spanclass="p">,</span><spanclass="w"></span><spanclass="p">[](){});</span><spanclass="w"></span></pre></section><sectionid="LaunchAsynchronousTasksFromAnSubflow"><h2><ahref="#LaunchAsynchronousTasksFromAnSubflow">Launch Asynchronous Tasks from a Subflow</a></h2><p>You can launch asynchronous tasks from <ahref="classtf_1_1Subflow.html" class="m-doc">tf::<wbr/>Subflow</a> using <ahref="classtf_1_1Runtime.html#a5688b13034f179c4a8b2b0ebbb215051" class="m-doc">tf::<wbr/>Subflow::<wbr/>async</a>. Asynchronous tasks are independent tasks spawned during the execution of a subflow. When the subflow joins, all asynchronous tasks are guaranteed to finish. The following code creates 100 asynchronous tasks from a subflow and joins their executions explicitly using <ahref="classtf_1_1Subflow.html#a59fcac1323e70d920088dd37bd0be245" class="m-doc">tf::<wbr/>Subflow::<wbr/>join</a>.</p><preclass="m-code"><spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">Taskflow</span><spanclass="w"></span><spanclass="n">taskflow</span><spanclass="p">;</span><spanclass="w"></span>
<spanclass="w"></span><spanclass="n">sf</span><spanclass="p">.</span><spanclass="n">join</span><spanclass="p">();</span><spanclass="w"></span><spanclass="c1">// all of the 100 asynchronous tasks will finish by this join</span>
<spanclass="n">executor</span><spanclass="p">.</span><spanclass="n">run</span><spanclass="p">(</span><spanclass="n">taskflow</span><spanclass="p">).</span><spanclass="n">wait</span><spanclass="p">();</span><spanclass="w"></span></pre><p>If you do not need the return value or the future to synchronize the execution, you can use <ahref="classtf_1_1Runtime.html#a0ce29efa2106c8c5a1432e4a55ab2e05" class="m-doc">tf::<wbr/>Subflow::<wbr/>silent_async</a> which has less overhead when creating an asynchronous task compared to <ahref="classtf_1_1Runtime.html#a5688b13034f179c4a8b2b0ebbb215051" class="m-doc">tf::<wbr/>Subflow::<wbr/>async</a>.</p><preclass="m-code"><spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">Taskflow</span><spanclass="w"></span><spanclass="n">taskflow</span><spanclass="p">;</span><spanclass="w"></span>
<spanclass="w"></span><spanclass="n">sf</span><spanclass="p">.</span><spanclass="n">join</span><spanclass="p">();</span><spanclass="w"></span><spanclass="c1">// all of the 100 asynchronous tasks will finish by this join</span>
<spanclass="n">executor</span><spanclass="p">.</span><spanclass="n">run</span><spanclass="p">(</span><spanclass="n">taskflow</span><spanclass="p">).</span><spanclass="n">wait</span><spanclass="p">();</span><spanclass="w"></span></pre><asideclass="m-note m-warning"><h4>Attention</h4><p>You should only create asynchronous tasks from a joinable subflow. Launching asynchronous tasks from a detached subflow results in undefined behavior.</p></aside><p>You can assign an asynchronous task a name using the two overloads, <ahref="classtf_1_1Runtime.html#a4b25b60f41d1725fad6928d6b77cfb65" class="m-doc">tf::<wbr/>Subflow::<wbr/>async(const std::<wbr/>string& name, F&& f)</a> and <ahref="classtf_1_1Runtime.html#a8365512d6a34e3ca867c195c97a3f719" class="m-doc">tf::<wbr/>Subflow::<wbr/>silent_async(const std::<wbr/>string& name, F&& f)</a>. Both methods take an additional argument of a string.</p><preclass="m-code"><spanclass="n">taskflow</span><spanclass="p">.</span><spanclass="n">emplace</span><spanclass="p">([](</span><spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">Subflow</span><spanclass="o">&</span><spanclass="w"></span><spanclass="n">sf</span><spanclass="p">){</span><spanclass="w"></span>
<spanclass="w"></span><spanclass="n">std</span><spanclass="o">::</span><spanclass="n">future</span><spanclass="o"><</span><spanclass="kt">void</span><spanclass="o">></span><spanclass="w"></span><spanclass="n">future</span><spanclass="w"></span><spanclass="o">=</span><spanclass="w"></span><spanclass="n">sf</span><spanclass="p">.</span><spanclass="n">async</span><spanclass="p">(</span><spanclass="s">"name of the task"</span><spanclass="p">,</span><spanclass="w"></span><spanclass="p">[](){});</span><spanclass="w"></span>
<spanclass="w"></span><spanclass="n">sf</span><spanclass="p">.</span><spanclass="n">silent_async</span><spanclass="p">(</span><spanclass="s">"another name of the task"</span><spanclass="p">,</span><spanclass="w"></span><spanclass="p">[](){});</span><spanclass="w"></span>
<spanclass="p">});</span><spanclass="w"></span></pre></section><sectionid="LaunchAsynchronousTasksFromARuntime"><h2><ahref="#LaunchAsynchronousTasksFromARuntime">Launch Asynchronous Tasks from a Runtime</a></h2><p>The asynchronous tasking feature of <ahref="classtf_1_1Subflow.html" class="m-doc">tf::<wbr/>Subflow</a> is indeed derived from <ahref="classtf_1_1Runtime.html" class="m-doc">tf::<wbr/>Runtime</a>. You can launch asynchronous tasks from <ahref="classtf_1_1Runtime.html" class="m-doc">tf::<wbr/>Runtime</a> using <ahref="classtf_1_1Runtime.html#a5688b13034f179c4a8b2b0ebbb215051" class="m-doc">tf::<wbr/>Runtime::<wbr/>async</a> or <ahref="classtf_1_1Runtime.html#a0ce29efa2106c8c5a1432e4a55ab2e05" class="m-doc">tf::<wbr/>Runtime::<wbr/>silent_async</a>. The following code creates 100 asynchronous tasks from a runtime and joins their executions explicitly using <ahref="classtf_1_1Runtime.html#a67a1dba6462cc63b1142bcb19ad86df5" class="m-doc">tf::<wbr/>Runtime::<wbr/>join</a>.</p><preclass="m-code"><spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">Taskflow</span><spanclass="w"></span><spanclass="n">taskflow</span><spanclass="p">;</span><spanclass="w"></span>
<spanclass="w"></span><spanclass="n">rt</span><spanclass="p">.</span><spanclass="n">join</span><spanclass="p">();</span><spanclass="w"></span><spanclass="c1">// all of the 100 asynchronous tasks will finish by this join</span>
<spanclass="n">executor</span><spanclass="p">.</span><spanclass="n">run</span><spanclass="p">(</span><spanclass="n">taskflow</span><spanclass="p">).</span><spanclass="n">wait</span><spanclass="p">();</span><spanclass="w"></span></pre><p>Unlike <ahref="classtf_1_1Subflow.html#a59fcac1323e70d920088dd37bd0be245" class="m-doc">tf::<wbr/>Subflow::<wbr/>join</a>, you can call <ahref="classtf_1_1Runtime.html#a67a1dba6462cc63b1142bcb19ad86df5" class="m-doc">tf::<wbr/>Runtime::<wbr/>join</a> multiple times to synchronize the execution of asynchronous tasks between different runs. For example, the following code spawn 100 asynchronous tasks twice and join each execution to assure the spawned 100 asynchronous tasks have properly completed.</p><preclass="m-code"><spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">Taskflow</span><spanclass="w"></span><spanclass="n">taskflow</span><spanclass="p">;</span><spanclass="w"></span>
<spanclass="w"></span><spanclass="n">rt</span><spanclass="p">.</span><spanclass="n">join</span><spanclass="p">();</span><spanclass="w"></span><spanclass="c1">// all of the 100 asynchronous tasks will finish by this join</span>
<spanclass="w"></span><spanclass="n">rt</span><spanclass="p">.</span><spanclass="n">join</span><spanclass="p">();</span><spanclass="w"></span><spanclass="c1">// all of the 100 asynchronous tasks will finish by this join</span>
<spanclass="n">executor</span><spanclass="p">.</span><spanclass="n">run</span><spanclass="p">(</span><spanclass="n">taskflow</span><spanclass="p">).</span><spanclass="n">wait</span><spanclass="p">();</span><spanclass="w"></span></pre><p>By default, <ahref="classtf_1_1Runtime.html" class="m-doc">tf::<wbr/>Runtime</a> does not join like <ahref="classtf_1_1Subflow.html" class="m-doc">tf::<wbr/>Subflow</a>. All pending asynchronous tasks spawned by <ahref="classtf_1_1Runtime.html" class="m-doc">tf::<wbr/>Runtime</a> are no longer controllable when their parent runtime disappears. It is your responsibility to properly synchronize spawned asynchronous tasks using <ahref="classtf_1_1Runtime.html#a67a1dba6462cc63b1142bcb19ad86df5" class="m-doc">tf::<wbr/>Runtime::<wbr/>join</a>.</p><asideclass="m-note m-info"><h4>Note</h4><p>Creating asynchronous tasks from a runtime allows users to efficiently implement parallel algorithms using recursion, such as parallel sort (<ahref="classtf_1_1FlowBuilder.html#a35e180eb63de6c9f28e43185e837a4fa" class="m-doc">tf::<wbr/>Taskflow::<wbr/>sort</a>), that demands dynamic parallelism at runtime.</p></aside></section>
</div>
</div>
</div>
</article></main>
<divclass="m-doc-search" id="search">
<ahref="#!" onclick="return hideSearch()"></a>
<divclass="m-container">
<divclass="m-row">
<divclass="m-col-m-8 m-push-m-2">
<divclass="m-doc-search-header m-text m-small">
<div><spanclass="m-label m-default">Tab</span> / <spanclass="m-label m-default">T</span> to search, <spanclass="m-label m-default">Esc</span> to close</div>