<?xml version='1.0' encoding='UTF-8' standalone='no'?>
<doxygen xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="compound.xsd" version="1.9.1" xml:lang="en-US">
  <compounddef id="ConditionalTasking" kind="page">
    <compoundname>ConditionalTasking</compoundname>
    <title>Conditional Tasking</title>
    <tableofcontents>
      <tocsect>
        <name>Create a Condition Task</name>
        <reference>ConditionalTasking_1CreateAConditionTask</reference>
    </tocsect>
      <tocsect>
        <name>Understand our Task-level Scheduling</name>
        <reference>ConditionalTasking_1TaskSchedulingPolicy</reference>
    <tableofcontents>
      <tocsect>
        <name>Example</name>
        <reference>ConditionalTasking_1TaskLevelSchedulingExample</reference>
    </tocsect>
    </tableofcontents>
    </tocsect>
      <tocsect>
        <name>Avoid Common Pitfalls</name>
        <reference>ConditionalTasking_1AvoidCommonPitfalls</reference>
    </tocsect>
      <tocsect>
        <name>Implement Control-flow Graphs</name>
        <reference>ConditionalTasking_1ImplementControlFlowGraphs</reference>
    <tableofcontents>
      <tocsect>
        <name>Implement If-Else Control Flow</name>
        <reference>ConditionalTasking_1ImplementIfElseControlFlow</reference>
    </tocsect>
      <tocsect>
        <name>Implement Switch Control Flow</name>
        <reference>ConditionalTasking_1ImplementSwitchControlFlow</reference>
    </tocsect>
      <tocsect>
        <name>Implement Do-While-Loop Control Flow</name>
        <reference>ConditionalTasking_1ImplementDoWhileLoopControlFlow</reference>
    </tocsect>
      <tocsect>
        <name>Implement While-Loop Control Flow</name>
        <reference>ConditionalTasking_1ImplementWhileLoopControlFlow</reference>
    </tocsect>
    </tableofcontents>
    </tocsect>
      <tocsect>
        <name>Create a Multi-condition Task</name>
        <reference>ConditionalTasking_1CreateAMultiConditionTask</reference>
    </tocsect>
    </tableofcontents>
    <briefdescription>
    </briefdescription>
    <detaileddescription>
<para>Parallel workloads often require making control-flow decisions across dependent tasks. Taskflow supports a very efficient interface of conditional tasking for users to implement general control flow such as dynamic flow, cycles and conditionals that are otherwise difficult to do with existing frameworks.</para>
<sect1 id="ConditionalTasking_1CreateAConditionTask">
<title>Create a Condition Task</title>
<para>A condition task evaluates a set of instructions and returns an integer index of the next successor task to execute. The index is defined with respect to the order of its successor construction. The following example creates an if-else block using a single condition task.</para>
<para><programlisting filename=".cpp"><codeline><highlight class="normal"><sp/>1:<sp/><ref refid="classtf_1_1Taskflow" kindref="compound">tf::Taskflow</ref><sp/>taskflow;</highlight></codeline>
<codeline><highlight class="normal"><sp/>2:</highlight></codeline>
<codeline><highlight class="normal"><sp/>3:<sp/></highlight><highlight class="keyword">auto</highlight><highlight class="normal"><sp/>[init,<sp/>cond,<sp/>yes,<sp/>no]<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>(</highlight></codeline>
<codeline><highlight class="normal"><sp/>4:<sp/><sp/>[]<sp/>()<sp/>{<sp/>},</highlight></codeline>
<codeline><highlight class="normal"><sp/>5:<sp/><sp/>[]<sp/>()<sp/>{<sp/></highlight><highlight class="keywordflow">return</highlight><highlight class="normal"><sp/>0;<sp/>},</highlight></codeline>
<codeline><highlight class="normal"><sp/>6:<sp/><sp/>[]<sp/>()<sp/>{<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/>&lt;&lt;<sp/></highlight><highlight class="stringliteral">&quot;yes\n&quot;</highlight><highlight class="normal">;<sp/>},</highlight></codeline>
<codeline><highlight class="normal"><sp/>7:<sp/><sp/>[]<sp/>()<sp/>{<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/>&lt;&lt;<sp/></highlight><highlight class="stringliteral">&quot;no\n&quot;</highlight><highlight class="normal">;<sp/>}</highlight></codeline>
<codeline><highlight class="normal"><sp/>8:<sp/>);</highlight></codeline>
<codeline><highlight class="normal"><sp/>9:</highlight></codeline>
<codeline><highlight class="normal">10:<sp/>cond.<ref refid="classtf_1_1Task_1a331b1b726555072e7c7d10941257f664" kindref="member">succeed</ref>(init)</highlight></codeline>
<codeline><highlight class="normal">11:<sp/><sp/><sp/><sp/><sp/>.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(yes,<sp/>no);<sp/><sp/></highlight><highlight class="comment">//<sp/>executes<sp/>yes<sp/>if<sp/>cond<sp/>returns<sp/>0</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">12:<sp/><sp/><sp/><sp/><sp/><sp/><sp/><sp/><sp/><sp/><sp/><sp/><sp/><sp/><sp/><sp/><sp/><sp/><sp/><sp/><sp/><sp/><sp/><sp/><sp/></highlight><highlight class="comment">//<sp/>executes<sp/>no<sp/><sp/>if<sp/>cond<sp/>returns<sp/>1</highlight></codeline>
</programlisting></para>
<para><dotfile name="/home/thuang295/Code/taskflow/doxygen/images/conditional-tasking-if-else.dot"></dotfile>
</para>
<para>Line 5 creates a condition task <computeroutput>cond</computeroutput> and line 11 creates two dependencies from <computeroutput>cond</computeroutput> to two other tasks, <computeroutput>yes</computeroutput> and <computeroutput>no</computeroutput>. With this order, when <computeroutput>cond</computeroutput> returns 0, the execution moves on to task <computeroutput>yes</computeroutput>. When <computeroutput>cond</computeroutput> returns 1, the execution moves on to task <computeroutput>no</computeroutput>.</para>
<para><simplesect kind="attention"><para>It is your responsibility to ensure the return of a condition task goes to a correct successor task. If the return falls beyond the range of the successors, the executor will not schedule any tasks.</para>
</simplesect>
Condition task can go cyclic to describe <emphasis>iterative</emphasis> control flow. The example below implements a simple yet commonly used feedback loop through a condition task (line 7-10) that returns a random binary value. If the return value from <computeroutput>cond</computeroutput> is <computeroutput>0</computeroutput>, it loops back to itself, or otherwise to <computeroutput>stop</computeroutput>.</para>
<para><programlisting filename=".cpp"><codeline><highlight class="normal"><sp/>1:<sp/><ref refid="classtf_1_1Taskflow" kindref="compound">tf::Taskflow</ref><sp/>taskflow;</highlight></codeline>
<codeline><highlight class="normal"><sp/>2:<sp/></highlight></codeline>
<codeline><highlight class="normal"><sp/>3:<sp/><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/>init<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([](){}).name(</highlight><highlight class="stringliteral">&quot;init&quot;</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"><sp/>4:<sp/><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/>stop<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([](){}).name(</highlight><highlight class="stringliteral">&quot;stop&quot;</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"><sp/>5:</highlight></codeline>
<codeline><highlight class="normal"><sp/>6:<sp/></highlight><highlight class="comment">//<sp/>creates<sp/>a<sp/>condition<sp/>task<sp/>that<sp/>returns<sp/>0<sp/>or<sp/>1</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"><sp/>7:<sp/><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/>cond<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([](){</highlight></codeline>
<codeline><highlight class="normal"><sp/>8:<sp/><sp/><sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/>&lt;&lt;<sp/></highlight><highlight class="stringliteral">&quot;flipping<sp/>a<sp/>coin\n&quot;</highlight><highlight class="normal">;</highlight></codeline>
<codeline><highlight class="normal"><sp/>9:<sp/><sp/><sp/></highlight><highlight class="keywordflow">return</highlight><highlight class="normal"><sp/><ref refid="cpp/numeric/random/rand" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::rand</ref>()<sp/>%<sp/>2;</highlight></codeline>
<codeline><highlight class="normal">10:<sp/>}).name(</highlight><highlight class="stringliteral">&quot;cond&quot;</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal">11:</highlight></codeline>
<codeline><highlight class="normal">12:<sp/></highlight><highlight class="comment">//<sp/>creates<sp/>a<sp/>feedback<sp/>loop<sp/>{0:<sp/>cond,<sp/>1:<sp/>stop}</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">13:<sp/>init.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(cond);</highlight></codeline>
<codeline><highlight class="normal">14:<sp/>cond.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(cond,<sp/>stop);<sp/><sp/></highlight><highlight class="comment">//<sp/>returns<sp/>0<sp/>to<sp/>&apos;cond&apos;<sp/>or<sp/>1<sp/>to<sp/>&apos;stop&apos;</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">15:</highlight></codeline>
<codeline><highlight class="normal">16:<sp/>executor.run(taskflow).wait();</highlight></codeline>
</programlisting></para>
<para><dotfile name="/home/thuang295/Code/taskflow/doxygen/images/conditional-tasking-1.dot"></dotfile>
</para>
<para>A taskflow of complex control flow often just takes a few lines of code to implement, and different control flow blocks may run in parallel. The code below creates another taskflow with three condition tasks.</para>
<para><programlisting filename=".cpp"><codeline><highlight class="normal"><ref refid="classtf_1_1Taskflow" kindref="compound">tf::Taskflow</ref><sp/>taskflow;</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/>A<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([](){}).name(</highlight><highlight class="stringliteral">&quot;A&quot;</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/>B<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([](){}).name(</highlight><highlight class="stringliteral">&quot;B&quot;</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/>C<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([](){}).name(</highlight><highlight class="stringliteral">&quot;C&quot;</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/>D<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([](){}).name(</highlight><highlight class="stringliteral">&quot;D&quot;</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/>E<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([](){}).name(</highlight><highlight class="stringliteral">&quot;E&quot;</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/>F<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([](){}).name(</highlight><highlight class="stringliteral">&quot;F&quot;</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/>G<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([](){}).name(</highlight><highlight class="stringliteral">&quot;G&quot;</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/>H<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([](){}).name(</highlight><highlight class="stringliteral">&quot;H&quot;</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/>I<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([](){}).name(</highlight><highlight class="stringliteral">&quot;I&quot;</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/>K<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([](){}).name(</highlight><highlight class="stringliteral">&quot;K&quot;</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/>L<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([](){}).name(</highlight><highlight class="stringliteral">&quot;L&quot;</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/>M<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([](){}).name(</highlight><highlight class="stringliteral">&quot;M&quot;</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/>cond_1<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([](){<sp/></highlight><highlight class="keywordflow">return</highlight><highlight class="normal"><sp/><ref refid="cpp/numeric/random/rand" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::rand</ref>()%2;<sp/>}).name(</highlight><highlight class="stringliteral">&quot;cond_1&quot;</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/>cond_2<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([](){<sp/></highlight><highlight class="keywordflow">return</highlight><highlight class="normal"><sp/><ref refid="cpp/numeric/random/rand" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::rand</ref>()%2;<sp/>}).name(</highlight><highlight class="stringliteral">&quot;cond_2&quot;</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/>cond_3<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([](){<sp/></highlight><highlight class="keywordflow">return</highlight><highlight class="normal"><sp/><ref refid="cpp/numeric/random/rand" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::rand</ref>()%2;<sp/>}).name(</highlight><highlight class="stringliteral">&quot;cond_3&quot;</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">A.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(B,<sp/>F);</highlight></codeline>
<codeline><highlight class="normal">B.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(C);</highlight></codeline>
<codeline><highlight class="normal">C.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(D);</highlight></codeline>
<codeline><highlight class="normal">D.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(cond_1);</highlight></codeline>
<codeline><highlight class="normal">E.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(K);</highlight></codeline>
<codeline><highlight class="normal">F.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(cond_2);</highlight></codeline>
<codeline><highlight class="normal">H.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(I);</highlight></codeline>
<codeline><highlight class="normal">I.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(cond_3);</highlight></codeline>
<codeline><highlight class="normal">L.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(M);</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">cond_1.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(B,<sp/>E);<sp/><sp/><sp/><sp/><sp/><sp/><sp/></highlight><highlight class="comment">//<sp/>return<sp/>0<sp/>to<sp/>&apos;B&apos;<sp/>or<sp/>1<sp/>to<sp/>&apos;E&apos;</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">cond_2.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(G,<sp/>H);<sp/><sp/><sp/><sp/><sp/><sp/><sp/></highlight><highlight class="comment">//<sp/>return<sp/>0<sp/>to<sp/>&apos;G&apos;<sp/>or<sp/>1<sp/>to<sp/>&apos;H&apos;</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">cond_3.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(cond_3,<sp/>L);<sp/><sp/></highlight><highlight class="comment">//<sp/>return<sp/>0<sp/>to<sp/>&apos;cond_3&apos;<sp/>or<sp/>1<sp/>to<sp/>&apos;L&apos;</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">taskflow.<ref refid="classtf_1_1Taskflow_1ac433018262e44b12c4cc9f0c4748d758" kindref="member">dump</ref>(<ref refid="cpp/io/basic_ostream" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref>);</highlight></codeline>
</programlisting></para>
<para>The above code creates three condition tasks: (1) a condition task <computeroutput>cond_1</computeroutput> that loops back to <computeroutput>B</computeroutput> on returning <computeroutput>0</computeroutput>, or proceeds to <computeroutput>E</computeroutput> on returning <computeroutput>1</computeroutput>, (2) a condition task <computeroutput>cond_2</computeroutput> that goes to <computeroutput>G</computeroutput> on returning <computeroutput>0</computeroutput>, or <computeroutput>H</computeroutput> on returning <computeroutput>1</computeroutput>, (3) a condition task <computeroutput>cond_3</computeroutput> that loops back to itself on returning <computeroutput>0</computeroutput>, or proceeds to <computeroutput>L</computeroutput> on returning <computeroutput>1</computeroutput> </para>
<para><dotfile name="/home/thuang295/Code/taskflow/doxygen/images/conditional-tasking-2.dot"></dotfile>
</para>
<para>You can use condition tasks to create cycles as long as the graph does not introduce task race during execution. However, cycles are not allowed in non-condition tasks.</para>
<para><simplesect kind="attention"><para>Conditional tasking lets you make in-task control-flow decisions to enable <emphasis>end-to-end</emphasis> parallelism, instead of resorting to client-side partition or synchronizing your task graph at the decision points of control flow.</para>
</simplesect>
</para>
</sect1>
<sect1 id="ConditionalTasking_1TaskSchedulingPolicy">
<title>Understand our Task-level Scheduling</title>
<para>In order to understand how an executor schedules condition tasks, we define two dependency types, <emphasis>strong dependency</emphasis> and <emphasis>weak dependency</emphasis>. A strong dependency is a preceding link from a non-condition task to another task. A weak dependency is a preceding link from a condition task to another task. The number of dependents of a task is the sum of strong dependency and weak dependency. The table below lists the strong dependency and weak dependency numbers of each task in the previous example.</para>
<para> <table rows="16" cols="4"><row>
<entry thead="yes" align='center'><para>task   </para>
</entry><entry thead="yes" align='center'><para>strong dependency   </para>
</entry><entry thead="yes" align='center'><para>weak dependency   </para>
</entry><entry thead="yes"><para>dependents    </para>
</entry></row>
<row>
<entry thead="no" align='center'><para>A   </para>
</entry><entry thead="no" align='center'><para>0   </para>
</entry><entry thead="no" align='center'><para>0   </para>
</entry><entry thead="no"><para>0    </para>
</entry></row>
<row>
<entry thead="no" align='center'><para>B   </para>
</entry><entry thead="no" align='center'><para>1   </para>
</entry><entry thead="no" align='center'><para>1   </para>
</entry><entry thead="no"><para>2    </para>
</entry></row>
<row>
<entry thead="no" align='center'><para>C   </para>
</entry><entry thead="no" align='center'><para>1   </para>
</entry><entry thead="no" align='center'><para>0   </para>
</entry><entry thead="no"><para>1    </para>
</entry></row>
<row>
<entry thead="no" align='center'><para>D   </para>
</entry><entry thead="no" align='center'><para>1   </para>
</entry><entry thead="no" align='center'><para>0   </para>
</entry><entry thead="no"><para>1    </para>
</entry></row>
<row>
<entry thead="no" align='center'><para>E   </para>
</entry><entry thead="no" align='center'><para>0   </para>
</entry><entry thead="no" align='center'><para>1   </para>
</entry><entry thead="no"><para>1    </para>
</entry></row>
<row>
<entry thead="no" align='center'><para>F   </para>
</entry><entry thead="no" align='center'><para>1   </para>
</entry><entry thead="no" align='center'><para>0   </para>
</entry><entry thead="no"><para>1    </para>
</entry></row>
<row>
<entry thead="no" align='center'><para>G   </para>
</entry><entry thead="no" align='center'><para>0   </para>
</entry><entry thead="no" align='center'><para>1   </para>
</entry><entry thead="no"><para>1    </para>
</entry></row>
<row>
<entry thead="no" align='center'><para>H   </para>
</entry><entry thead="no" align='center'><para>0   </para>
</entry><entry thead="no" align='center'><para>1   </para>
</entry><entry thead="no"><para>1    </para>
</entry></row>
<row>
<entry thead="no" align='center'><para>I   </para>
</entry><entry thead="no" align='center'><para>1   </para>
</entry><entry thead="no" align='center'><para>0   </para>
</entry><entry thead="no"><para>1    </para>
</entry></row>
<row>
<entry thead="no" align='center'><para>K   </para>
</entry><entry thead="no" align='center'><para>1   </para>
</entry><entry thead="no" align='center'><para>0   </para>
</entry><entry thead="no"><para>1    </para>
</entry></row>
<row>
<entry thead="no" align='center'><para>L   </para>
</entry><entry thead="no" align='center'><para>0   </para>
</entry><entry thead="no" align='center'><para>1   </para>
</entry><entry thead="no"><para>1    </para>
</entry></row>
<row>
<entry thead="no" align='center'><para>M   </para>
</entry><entry thead="no" align='center'><para>1   </para>
</entry><entry thead="no" align='center'><para>0   </para>
</entry><entry thead="no"><para>1    </para>
</entry></row>
<row>
<entry thead="no" align='center'><para>cond_1   </para>
</entry><entry thead="no" align='center'><para>1   </para>
</entry><entry thead="no" align='center'><para>0   </para>
</entry><entry thead="no"><para>1    </para>
</entry></row>
<row>
<entry thead="no" align='center'><para>cond_2   </para>
</entry><entry thead="no" align='center'><para>1   </para>
</entry><entry thead="no" align='center'><para>0   </para>
</entry><entry thead="no"><para>1    </para>
</entry></row>
<row>
<entry thead="no" align='center'><para>cond_3   </para>
</entry><entry thead="no" align='center'><para>1   </para>
</entry><entry thead="no" align='center'><para>1   </para>
</entry><entry thead="no"><para>2   </para>
</entry></row>
</table>
</para>
<para>You can query the number of strong dependents, the number of weak dependents, and the number of dependents of a task.</para>
<para><programlisting filename=".cpp"><codeline><highlight class="normal">1:<sp/><ref refid="classtf_1_1Taskflow" kindref="compound">tf::Taskflow</ref><sp/>taskflow;</highlight></codeline>
<codeline><highlight class="normal">2:<sp/></highlight></codeline>
<codeline><highlight class="normal">3:<sp/><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/>task<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([](){});</highlight></codeline>
<codeline><highlight class="normal">4:<sp/></highlight></codeline>
<codeline><highlight class="normal">5:<sp/></highlight><highlight class="comment">//<sp/>...<sp/>add<sp/>more<sp/>tasks<sp/>and<sp/>preceding<sp/>links</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">6:</highlight></codeline>
<codeline><highlight class="normal">7:<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/>&lt;&lt;<sp/>task.<ref refid="classtf_1_1Task_1a974dc1d738b62b829ad261beeafbd67c" kindref="member">num_dependents</ref>()<sp/>&lt;&lt;<sp/></highlight><highlight class="charliteral">&apos;\n&apos;</highlight><highlight class="normal">;</highlight></codeline>
<codeline><highlight class="normal">8:<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/>&lt;&lt;<sp/>task.<ref refid="classtf_1_1Task_1ad49a92e8858c3c298bed0215e341b66b" kindref="member">num_strong_dependents</ref>()<sp/>&lt;&lt;<sp/></highlight><highlight class="charliteral">&apos;\n&apos;</highlight><highlight class="normal">;<sp/></highlight></codeline>
<codeline><highlight class="normal">9:<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/>&lt;&lt;<sp/>task.<ref refid="classtf_1_1Task_1af3bf886291af7f39957d43d17083fe07" kindref="member">num_weak_dependents</ref>()<sp/>&lt;&lt;<sp/></highlight><highlight class="charliteral">&apos;\n&apos;</highlight><highlight class="normal">;</highlight></codeline>
</programlisting></para>
<para>When you submit a task to an executor, the scheduler starts with tasks of <emphasis>zero dependents</emphasis> (both zero strong and weak dependencies) and continues to execute successive tasks whenever their <emphasis>strong dependencies</emphasis> are met. However, the scheduler skips this rule when executing a condition task and jumps directly to its successors indexed by the return value.</para>
<para><dotfile name="/home/thuang295/Code/taskflow/doxygen/images/task_level_scheduling.dot"></dotfile>
</para>
<para>Each task has an <emphasis>atomic</emphasis> join counter to keep track of strong dependents that are met at runtime. When a task completes, the join counter is restored to the task&apos;s strong dependency number in the graph, such that the subsequent execution can reuse the counter again.</para>
<sect2 id="ConditionalTasking_1TaskLevelSchedulingExample">
<title>Example</title>
<para>Let&apos;s take a look at an example to understand how task-level scheduling works. Suppose we have the following taskflow of one condition task <computeroutput>cond</computeroutput> that forms a loop to itself on returning <computeroutput>0</computeroutput> and moves on to <computeroutput>stop</computeroutput> on returning <computeroutput>1</computeroutput>:</para>
<para><dotfile name="/home/thuang295/Code/taskflow/doxygen/images/conditional-tasking-1.dot"></dotfile>
</para>
<para>The scheduler starts with <computeroutput>init</computeroutput> task because it has no dependencies (both strong and weak dependencies). Then, the scheduler moves on to the condition task <computeroutput>cond</computeroutput>. If <computeroutput>cond</computeroutput> returns <computeroutput>0</computeroutput>, the scheduler enqueues <computeroutput>cond</computeroutput> and runs it again. If <computeroutput>cond</computeroutput> returns <computeroutput>1</computeroutput>, the scheduler enqueues <computeroutput>stop</computeroutput> and then moves on.</para>
</sect2>
</sect1>
<sect1 id="ConditionalTasking_1AvoidCommonPitfalls">
<title>Avoid Common Pitfalls</title>
<para>Condition tasks are handy in creating dynamic and cyclic control flows, but they are also easy to make mistakes. It is your responsibility to ensure a taskflow is properly conditioned. Top things to avoid include <emphasis>no source tasks</emphasis> to start with and <emphasis>task race</emphasis>. The figure below shows common pitfalls and their remedies.</para>
<para><dotfile name="/home/thuang295/Code/taskflow/doxygen/images/conditional-tasking-pitfalls.dot"></dotfile>
</para>
<para>In the <computeroutput>error1</computeroutput> scenario, there is no source task for the scheduler to start with, and the simplest fix is to add a task <computeroutput>S</computeroutput> that has no dependents. In the <computeroutput>error2</computeroutput> scenario, <computeroutput>D</computeroutput> might be scheduled twice by <computeroutput>E</computeroutput> through the strong dependency and <computeroutput>C</computeroutput> through the weak dependency (on returning <computeroutput>1</computeroutput>). To fix this problem, you can add an auxiliary task <computeroutput>D-aux</computeroutput> to break the mixed use of strong dependency and weak dependency. In the risky scenario, task <computeroutput>X</computeroutput> may be raced by <computeroutput>M</computeroutput> and <computeroutput>P</computeroutput> if <computeroutput>M</computeroutput> returns <computeroutput>0</computeroutput> and P returns <computeroutput>1</computeroutput>.</para>
<para><simplesect kind="attention"><para>It is your responsibility to ensure a written taskflow graph is properly conditioned. We suggest that you <ref refid="ConditionalTasking_1TaskSchedulingPolicy" kindref="member">Understand our Task-level Scheduling</ref> and infer if task race exists in the execution of your graph.</para>
</simplesect>
</para>
</sect1>
<sect1 id="ConditionalTasking_1ImplementControlFlowGraphs">
<title>Implement Control-flow Graphs</title>
<sect2 id="ConditionalTasking_1ImplementIfElseControlFlow">
<title>Implement If-Else Control Flow</title>
<para>You can use conditional tasking to implement if-else control flow. The following example creates a nested if-else control flow diagram that executes three condition tasks to check the range of <computeroutput>i</computeroutput>.</para>
<para><programlisting filename=".cpp"><codeline><highlight class="normal"><ref refid="classtf_1_1Taskflow" kindref="compound">tf::Taskflow</ref><sp/>taskflow;</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keywordtype">int</highlight><highlight class="normal"><sp/>i;</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="comment">//<sp/>create<sp/>three<sp/>condition<sp/>tasks<sp/>for<sp/>nested<sp/>control<sp/>flow</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keyword">auto</highlight><highlight class="normal"><sp/>initi<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([&amp;](){<sp/>i=3;<sp/>});<sp/></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keyword">auto</highlight><highlight class="normal"><sp/>cond1<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([&amp;](){<sp/></highlight><highlight class="keywordflow">return</highlight><highlight class="normal"><sp/>i&gt;1<sp/>?<sp/>1<sp/>:<sp/>0;<sp/>});<sp/></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keyword">auto</highlight><highlight class="normal"><sp/>cond2<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([&amp;](){<sp/></highlight><highlight class="keywordflow">return</highlight><highlight class="normal"><sp/>i&gt;2<sp/>?<sp/>1<sp/>:<sp/>0;<sp/>});<sp/></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keyword">auto</highlight><highlight class="normal"><sp/>cond3<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([&amp;](){<sp/></highlight><highlight class="keywordflow">return</highlight><highlight class="normal"><sp/>i&gt;3<sp/>?<sp/>1<sp/>:<sp/>0;<sp/>});<sp/></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keyword">auto</highlight><highlight class="normal"><sp/>equl1<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([&amp;](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/>&lt;&lt;<sp/></highlight><highlight class="stringliteral">&quot;i=1\n&quot;</highlight><highlight class="normal">;<sp/>});<sp/></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keyword">auto</highlight><highlight class="normal"><sp/>equl2<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([&amp;](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/>&lt;&lt;<sp/></highlight><highlight class="stringliteral">&quot;i=2\n&quot;</highlight><highlight class="normal">;<sp/>});<sp/></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keyword">auto</highlight><highlight class="normal"><sp/>equl3<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([&amp;](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/>&lt;&lt;<sp/></highlight><highlight class="stringliteral">&quot;i=3\n&quot;</highlight><highlight class="normal">;<sp/>});<sp/></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keyword">auto</highlight><highlight class="normal"><sp/>grtr3<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([&amp;](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/>&lt;&lt;<sp/></highlight><highlight class="stringliteral">&quot;i&gt;3\n&quot;</highlight><highlight class="normal">;<sp/>});<sp/></highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">initi.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(cond1);</highlight></codeline>
<codeline><highlight class="normal">cond1.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(equl1,<sp/>cond2);<sp/><sp/></highlight><highlight class="comment">//<sp/>goes<sp/>to<sp/>cond2<sp/>if<sp/>i&gt;1</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">cond2.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(equl2,<sp/>cond3);<sp/><sp/></highlight><highlight class="comment">//<sp/>goes<sp/>to<sp/>cond3<sp/>if<sp/>i&gt;2</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">cond3.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(equl3,<sp/>grtr3);<sp/><sp/></highlight><highlight class="comment">//<sp/>goes<sp/>to<sp/>grtr3<sp/>if<sp/>i&gt;3</highlight></codeline>
</programlisting></para>
<para><dotfile name="/home/thuang295/Code/taskflow/doxygen/images/conditional-tasking-nested-if-else.dot"></dotfile>
</para>
</sect2>
<sect2 id="ConditionalTasking_1ImplementSwitchControlFlow">
<title>Implement Switch Control Flow</title>
<para>You can use conditional tasking to implement <emphasis>switch</emphasis> control flow. The following example creates a switch control flow diagram that executes one of the three cases at random using four condition tasks.</para>
<para><programlisting filename=".cpp"><codeline><highlight class="normal"><ref refid="classtf_1_1Taskflow" kindref="compound">tf::Taskflow</ref><sp/>taskflow;</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keyword">auto</highlight><highlight class="normal"><sp/>[source,<sp/>swcond,<sp/>case1,<sp/>case2,<sp/>case3,<sp/>target]<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>(</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>[](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/>&lt;&lt;<sp/></highlight><highlight class="stringliteral">&quot;source\n&quot;</highlight><highlight class="normal">;<sp/>},</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>[](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/>&lt;&lt;<sp/></highlight><highlight class="stringliteral">&quot;switch\n&quot;</highlight><highlight class="normal">;<sp/></highlight><highlight class="keywordflow">return</highlight><highlight class="normal"><sp/><ref refid="cpp/numeric/random/rand" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">rand</ref>()%3;<sp/>},</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>[](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/>&lt;&lt;<sp/></highlight><highlight class="stringliteral">&quot;case<sp/>1\n&quot;</highlight><highlight class="normal">;<sp/></highlight><highlight class="keywordflow">return</highlight><highlight class="normal"><sp/>0;<sp/>},</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>[](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/>&lt;&lt;<sp/></highlight><highlight class="stringliteral">&quot;case<sp/>2\n&quot;</highlight><highlight class="normal">;<sp/></highlight><highlight class="keywordflow">return</highlight><highlight class="normal"><sp/>0;<sp/>},</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>[](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/>&lt;&lt;<sp/></highlight><highlight class="stringliteral">&quot;case<sp/>3\n&quot;</highlight><highlight class="normal">;<sp/></highlight><highlight class="keywordflow">return</highlight><highlight class="normal"><sp/>0;<sp/>},</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>[](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/>&lt;&lt;<sp/></highlight><highlight class="stringliteral">&quot;target\n&quot;</highlight><highlight class="normal">;<sp/>}</highlight></codeline>
<codeline><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">source.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(swcond);</highlight></codeline>
<codeline><highlight class="normal">swcond.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(case1,<sp/>case2,<sp/>case3);</highlight></codeline>
<codeline><highlight class="normal">target.<ref refid="classtf_1_1Task_1a331b1b726555072e7c7d10941257f664" kindref="member">succeed</ref>(case1,<sp/>case2,<sp/>case3);</highlight></codeline>
</programlisting></para>
<para><dotfile name="/home/thuang295/Code/taskflow/doxygen/images/conditional-tasking-switch.dot"></dotfile>
</para>
<para>Assuming <computeroutput>swcond</computeroutput> returns 1, the program outputs:</para>
<para><programlisting filename=".shell-session"><codeline><highlight class="normal">source</highlight></codeline>
<codeline><highlight class="normal">switch</highlight></codeline>
<codeline><highlight class="normal">case<sp/>2</highlight></codeline>
<codeline><highlight class="normal">target</highlight></codeline>
</programlisting></para>
<para>Keep in mind, both switch and case tasks must be described as condition tasks. The following implementation is a common mistake in which case tasks are not described as condition tasks.</para>
<para><programlisting filename=".cpp"><codeline><highlight class="comment">//<sp/>wrong<sp/>implementation<sp/>of<sp/>switch<sp/>control<sp/>flow<sp/>using<sp/>only<sp/>one<sp/>condition<sp/>task</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"><ref refid="classtf_1_1Taskflow" kindref="compound">tf::Taskflow</ref><sp/>taskflow;</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keyword">auto</highlight><highlight class="normal"><sp/>[source,<sp/>swcond,<sp/>case1,<sp/>case2,<sp/>case3,<sp/>target]<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>(</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>[](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/>&lt;&lt;<sp/></highlight><highlight class="stringliteral">&quot;source\n&quot;</highlight><highlight class="normal">;<sp/>},</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>[](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/>&lt;&lt;<sp/></highlight><highlight class="stringliteral">&quot;switch\n&quot;</highlight><highlight class="normal">;<sp/></highlight><highlight class="keywordflow">return</highlight><highlight class="normal"><sp/><ref refid="cpp/numeric/random/rand" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">rand</ref>()%3;<sp/>},</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>[](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/>&lt;&lt;<sp/></highlight><highlight class="stringliteral">&quot;case<sp/>1\n&quot;</highlight><highlight class="normal">;<sp/>},</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>[](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/>&lt;&lt;<sp/></highlight><highlight class="stringliteral">&quot;case<sp/>2\n&quot;</highlight><highlight class="normal">;<sp/>},</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>[](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/>&lt;&lt;<sp/></highlight><highlight class="stringliteral">&quot;case<sp/>3\n&quot;</highlight><highlight class="normal">;<sp/>},</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>[](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/>&lt;&lt;<sp/></highlight><highlight class="stringliteral">&quot;target\n&quot;</highlight><highlight class="normal">;<sp/>}<sp/><sp/></highlight><highlight class="comment">//<sp/>target<sp/>has<sp/>three<sp/>strong<sp/>dependencies</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">source.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(swcond);</highlight></codeline>
<codeline><highlight class="normal">swcond.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(case1,<sp/>case2,<sp/>case3);</highlight></codeline>
<codeline><highlight class="normal">target.<ref refid="classtf_1_1Task_1a331b1b726555072e7c7d10941257f664" kindref="member">succeed</ref>(case1,<sp/>case2,<sp/>case3);</highlight></codeline>
</programlisting></para>
<para><dotfile name="/home/thuang295/Code/taskflow/doxygen/images/conditional-tasking-switch-wrong.dot"></dotfile>
</para>
<para>In this faulty implementation, task <computeroutput>target</computeroutput> has three strong dependencies but only one of them will be met. This is because <computeroutput>swcond</computeroutput> is a condition task, and only one case task will be executed depending on the return of <computeroutput>swcond</computeroutput>.</para>
</sect2>
<sect2 id="ConditionalTasking_1ImplementDoWhileLoopControlFlow">
<title>Implement Do-While-Loop Control Flow</title>
<para>You can use conditional tasking to implement <emphasis>do-while-loop</emphasis> control flow. The following example creates a do-while-loop control flow diagram that repeatedly increments variable <computeroutput>i</computeroutput> five times using one condition task.</para>
<para><programlisting filename=".cpp"><codeline><highlight class="normal"><ref refid="classtf_1_1Taskflow" kindref="compound">tf::Taskflow</ref><sp/>taskflow;</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keywordtype">int</highlight><highlight class="normal"><sp/>i;</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keyword">auto</highlight><highlight class="normal"><sp/>[init,<sp/>body,<sp/>cond,<sp/>done]<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>(</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>[&amp;](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/>&lt;&lt;<sp/></highlight><highlight class="stringliteral">&quot;i=0\n&quot;</highlight><highlight class="normal">;<sp/>i=0;<sp/>},</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>[&amp;](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/>&lt;&lt;<sp/></highlight><highlight class="stringliteral">&quot;i++<sp/>=&gt;<sp/>i=&quot;</highlight><highlight class="normal">;<sp/>i++;<sp/>},</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>[&amp;](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/>&lt;&lt;<sp/>i<sp/>&lt;&lt;<sp/></highlight><highlight class="charliteral">&apos;\n&apos;</highlight><highlight class="normal">;<sp/></highlight><highlight class="keywordflow">return</highlight><highlight class="normal"><sp/>i&lt;5<sp/>?<sp/>0<sp/>:<sp/>1;<sp/>},</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>[&amp;](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/>&lt;&lt;<sp/></highlight><highlight class="stringliteral">&quot;done\n&quot;</highlight><highlight class="normal">;<sp/>}</highlight></codeline>
<codeline><highlight class="normal">);<sp/><sp/></highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">init.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(body);</highlight></codeline>
<codeline><highlight class="normal">body.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(cond);</highlight></codeline>
<codeline><highlight class="normal">cond.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(body,<sp/>done);</highlight></codeline>
</programlisting></para>
<para><dotfile name="/home/thuang295/Code/taskflow/doxygen/images/conditional-tasking-do-while.dot"></dotfile>
</para>
<para>The program outputs:</para>
<para><programlisting filename=".shell-session"><codeline><highlight class="normal">i=0</highlight></codeline>
<codeline><highlight class="normal">i++<sp/>=&gt;<sp/>i=1</highlight></codeline>
<codeline><highlight class="normal">i++<sp/>=&gt;<sp/>i=2</highlight></codeline>
<codeline><highlight class="normal">i++<sp/>=&gt;<sp/>i=3</highlight></codeline>
<codeline><highlight class="normal">i++<sp/>=&gt;<sp/>i=4</highlight></codeline>
<codeline><highlight class="normal">i++<sp/>=&gt;<sp/>i=5</highlight></codeline>
<codeline><highlight class="normal">done</highlight></codeline>
</programlisting></para>
</sect2>
<sect2 id="ConditionalTasking_1ImplementWhileLoopControlFlow">
<title>Implement While-Loop Control Flow</title>
<para>You can use conditional tasking to implement <emphasis>while-loop</emphasis> control flow. The following example creates a while-loop control flow diagram that repeatedly increments variable <computeroutput>i</computeroutput> five times using two condition task.</para>
<para><programlisting filename=".cpp"><codeline><highlight class="normal"><ref refid="classtf_1_1Taskflow" kindref="compound">tf::Taskflow</ref><sp/>taskflow;</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keywordtype">int</highlight><highlight class="normal"><sp/>i;</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keyword">auto</highlight><highlight class="normal"><sp/>[init,<sp/>cond,<sp/>body,<sp/>back,<sp/>done]<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>(</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>[&amp;](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/>&lt;&lt;<sp/></highlight><highlight class="stringliteral">&quot;i=0\n&quot;</highlight><highlight class="normal">;<sp/>i=0;<sp/>},</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>[&amp;](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/>&lt;&lt;<sp/></highlight><highlight class="stringliteral">&quot;while<sp/>i&lt;5\n&quot;</highlight><highlight class="normal">;<sp/></highlight><highlight class="keywordflow">return</highlight><highlight class="normal"><sp/>i<sp/>&lt;<sp/>5<sp/>?<sp/>0<sp/>:<sp/>1;<sp/>},</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>[&amp;](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/>&lt;&lt;<sp/></highlight><highlight class="stringliteral">&quot;i++=&quot;</highlight><highlight class="normal"><sp/>&lt;&lt;<sp/>i++<sp/>&lt;&lt;<sp/></highlight><highlight class="charliteral">&apos;\n&apos;</highlight><highlight class="normal">;<sp/>},</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>[&amp;](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/>&lt;&lt;<sp/></highlight><highlight class="stringliteral">&quot;back\n&quot;</highlight><highlight class="normal">;<sp/></highlight><highlight class="keywordflow">return</highlight><highlight class="normal"><sp/>0;<sp/>},</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>[&amp;](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/>&lt;&lt;<sp/></highlight><highlight class="stringliteral">&quot;done\n&quot;</highlight><highlight class="normal">;<sp/>}</highlight></codeline>
<codeline><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">init.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(cond);</highlight></codeline>
<codeline><highlight class="normal">cond.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(body,<sp/>done);</highlight></codeline>
<codeline><highlight class="normal">body.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(back);</highlight></codeline>
<codeline><highlight class="normal">back.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(cond);</highlight></codeline>
</programlisting></para>
<para><dotfile name="/home/thuang295/Code/taskflow/doxygen/images/conditional-tasking-while.dot"></dotfile>
</para>
<para>The program outputs:</para>
<para><programlisting filename=".shell-session"><codeline><highlight class="normal">i=0</highlight></codeline>
<codeline><highlight class="normal">while<sp/>i&lt;5</highlight></codeline>
<codeline><highlight class="normal">i++=0</highlight></codeline>
<codeline><highlight class="normal">back</highlight></codeline>
<codeline><highlight class="normal">while<sp/>i&lt;5</highlight></codeline>
<codeline><highlight class="normal">i++=1</highlight></codeline>
<codeline><highlight class="normal">back</highlight></codeline>
<codeline><highlight class="normal">while<sp/>i&lt;5</highlight></codeline>
<codeline><highlight class="normal">i++=2</highlight></codeline>
<codeline><highlight class="normal">back</highlight></codeline>
<codeline><highlight class="normal">while<sp/>i&lt;5</highlight></codeline>
<codeline><highlight class="normal">i++=3</highlight></codeline>
<codeline><highlight class="normal">back</highlight></codeline>
<codeline><highlight class="normal">while<sp/>i&lt;5</highlight></codeline>
<codeline><highlight class="normal">i++=4</highlight></codeline>
<codeline><highlight class="normal">back</highlight></codeline>
<codeline><highlight class="normal">while<sp/>i&lt;5</highlight></codeline>
<codeline><highlight class="normal">done</highlight></codeline>
</programlisting></para>
<para>Notice that, when you implement a while-loop block, you cannot direct a dependency from the body task to the loop condition task. Doing so will introduce a strong dependency between the body task and the loop condition task, and the loop condition task will never be executed. The following code shows a common faulty implementation of while-loop control flow.</para>
<para><programlisting filename=".cpp"><codeline><highlight class="comment">//<sp/>wrong<sp/>implementation<sp/>of<sp/>while-loop<sp/>using<sp/>only<sp/>one<sp/>condition<sp/>task</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"><ref refid="classtf_1_1Taskflow" kindref="compound">tf::Taskflow</ref><sp/>taskflow;</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keywordtype">int</highlight><highlight class="normal"><sp/>i;</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keyword">auto</highlight><highlight class="normal"><sp/>[init,<sp/>cond,<sp/>body,<sp/>done]<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>(</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>[&amp;](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/>&lt;&lt;<sp/></highlight><highlight class="stringliteral">&quot;i=0\n&quot;</highlight><highlight class="normal">;<sp/>i=0;<sp/>},</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>[&amp;](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/>&lt;&lt;<sp/></highlight><highlight class="stringliteral">&quot;while<sp/>i&lt;5\n&quot;</highlight><highlight class="normal">;<sp/></highlight><highlight class="keywordflow">return</highlight><highlight class="normal"><sp/>i<sp/>&lt;<sp/>5<sp/>?<sp/>0<sp/>:<sp/>1;<sp/>},</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>[&amp;](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/>&lt;&lt;<sp/></highlight><highlight class="stringliteral">&quot;i++=&quot;</highlight><highlight class="normal"><sp/>&lt;&lt;<sp/>i++<sp/>&lt;&lt;<sp/></highlight><highlight class="charliteral">&apos;\n&apos;</highlight><highlight class="normal">;<sp/>},</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>[&amp;](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/>&lt;&lt;<sp/></highlight><highlight class="stringliteral">&quot;done\n&quot;</highlight><highlight class="normal">;<sp/>}</highlight></codeline>
<codeline><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">init.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(cond);</highlight></codeline>
<codeline><highlight class="normal">cond.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(body,<sp/>done);</highlight></codeline>
<codeline><highlight class="normal">body.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(cond);</highlight></codeline>
</programlisting></para>
<para><dotfile name="/home/thuang295/Code/taskflow/doxygen/images/conditional-tasking-while-wrong.dot"></dotfile>
</para>
<para>In the taskflow diagram above, the scheduler starts with <computeroutput>init</computeroutput> and then decrements the strong dependency of the loop condition task, <computeroutput>while i&lt;5</computeroutput>. After this, there remains one strong dependency, i.e., introduced by the loop body task, <computeroutput>i++</computeroutput>. However, task <computeroutput>i++</computeroutput> will not be executed until the loop condition task returns <computeroutput>0</computeroutput>, causing a deadlock.</para>
</sect2>
</sect1>
<sect1 id="ConditionalTasking_1CreateAMultiConditionTask">
<title>Create a Multi-condition Task</title>
<para>A <emphasis>multi-condition task</emphasis> is a generalized version of conditional tasking. In some cases, applications need to jump to multiple branches from a parent task. This can be done by creating a <emphasis>multi-condition task</emphasis> which allows a task to select one or more successor tasks to execute. Similar to a condition task, a multi-condition task returns a vector of integer indices that indicate the successors to execute when the multi-condition task completes. The index is defined with respect to the order of successors preceded by a multi-condition task. For example, the following code creates a multi-condition task, <computeroutput>A</computeroutput>, that informs the scheduler to run on its two successors, <computeroutput>B</computeroutput> and <computeroutput>D</computeroutput>.</para>
<para><programlisting filename=".cpp"><codeline><highlight class="normal"><ref refid="classtf_1_1Executor" kindref="compound">tf::Executor</ref><sp/>executor;</highlight></codeline>
<codeline><highlight class="normal"><ref refid="classtf_1_1Taskflow" kindref="compound">tf::Taskflow</ref><sp/>taskflow;</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keyword">auto</highlight><highlight class="normal"><sp/>A<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([&amp;]()<sp/>-&gt;<sp/><ref refid="classtf_1_1SmallVector" kindref="compound">tf::SmallVector&lt;int&gt;</ref><sp/>{<sp/></highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/>&lt;&lt;<sp/></highlight><highlight class="stringliteral">&quot;A\n&quot;</highlight><highlight class="normal">;<sp/></highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/></highlight><highlight class="keywordflow">return</highlight><highlight class="normal"><sp/>{0,<sp/>2};</highlight></codeline>
<codeline><highlight class="normal">}).name(</highlight><highlight class="stringliteral">&quot;A&quot;</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keyword">auto</highlight><highlight class="normal"><sp/>B<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([&amp;](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/>&lt;&lt;<sp/></highlight><highlight class="stringliteral">&quot;B\n&quot;</highlight><highlight class="normal">;<sp/>}).name(</highlight><highlight class="stringliteral">&quot;B&quot;</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keyword">auto</highlight><highlight class="normal"><sp/>C<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([&amp;](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/>&lt;&lt;<sp/></highlight><highlight class="stringliteral">&quot;C\n&quot;</highlight><highlight class="normal">;<sp/>}).name(</highlight><highlight class="stringliteral">&quot;C&quot;</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keyword">auto</highlight><highlight class="normal"><sp/>D<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([&amp;](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/>&lt;&lt;<sp/></highlight><highlight class="stringliteral">&quot;D\n&quot;</highlight><highlight class="normal">;<sp/>}).name(</highlight><highlight class="stringliteral">&quot;D&quot;</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">A.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(B,<sp/>C,<sp/>D);</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">executor.<ref refid="classtf_1_1Executor_1a8d08f0cb79e7b3780087975d13368a96" kindref="member">run</ref>(taskflow).wait();</highlight></codeline>
</programlisting></para>
<para><dotfile name="/home/thuang295/Code/taskflow/doxygen/images/multi-condition-task-1.dot"></dotfile>
</para>
<para><simplesect kind="attention"><para>The return type of a multi-condition task is <ref refid="classtf_1_1SmallVector" kindref="compound">tf::SmallVector</ref>, which provides C++ vector-style functionalities but comes with small buffer optimization.</para>
</simplesect>
One important application of conditional tasking is implementing <emphasis>iterative control flow</emphasis>. You can use multi-condition tasks to create multiple loops that run concurrently. The following code creates a sequential chain of four loops in which each loop increments a counter variable ten times. When the program completes, the value of the counter variable is <computeroutput>40</computeroutput>.</para>
<para><programlisting filename=".cpp"><codeline><highlight class="normal"><ref refid="classtf_1_1Executor" kindref="compound">tf::Executor</ref><sp/>executor;</highlight></codeline>
<codeline><highlight class="normal"><ref refid="classtf_1_1Taskflow" kindref="compound">tf::Taskflow</ref><sp/>taskflow;</highlight></codeline>
<codeline><highlight class="normal"><ref refid="cpp/atomic/atomic" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::atomic&lt;int&gt;</ref><sp/>counter{0};</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keyword">auto</highlight><highlight class="normal"><sp/>loop<sp/>=<sp/>[&amp;,<sp/>c<sp/>=<sp/>int(0)]()<sp/></highlight><highlight class="keyword">mutable</highlight><highlight class="normal"><sp/>-&gt;<sp/><ref refid="classtf_1_1SmallVector" kindref="compound">tf::SmallVector&lt;int&gt;</ref><sp/>{</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>counter.fetch_add(1,<sp/>std::memory_order_relaxed);</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/></highlight><highlight class="keywordflow">return</highlight><highlight class="normal"><sp/>{++c<sp/>&lt;<sp/>10<sp/>?<sp/>0<sp/>:<sp/>1};</highlight></codeline>
<codeline><highlight class="normal">};</highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keyword">auto</highlight><highlight class="normal"><sp/>init<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([](){}).name(</highlight><highlight class="stringliteral">&quot;init&quot;</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keyword">auto</highlight><highlight class="normal"><sp/>A<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>(loop).<ref refid="classtf_1_1Task_1a08ada0425b490997b6ff7f310107e5e3" kindref="member">name</ref>(</highlight><highlight class="stringliteral">&quot;A&quot;</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keyword">auto</highlight><highlight class="normal"><sp/>B<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>(loop).<ref refid="classtf_1_1Task_1a08ada0425b490997b6ff7f310107e5e3" kindref="member">name</ref>(</highlight><highlight class="stringliteral">&quot;B&quot;</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keyword">auto</highlight><highlight class="normal"><sp/>C<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>(loop).<ref refid="classtf_1_1Task_1a08ada0425b490997b6ff7f310107e5e3" kindref="member">name</ref>(</highlight><highlight class="stringliteral">&quot;C&quot;</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keyword">auto</highlight><highlight class="normal"><sp/>D<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>(loop).<ref refid="classtf_1_1Task_1a08ada0425b490997b6ff7f310107e5e3" kindref="member">name</ref>(</highlight><highlight class="stringliteral">&quot;D&quot;</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">init.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(A);</highlight></codeline>
<codeline><highlight class="normal">A.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(A,<sp/>B);</highlight></codeline>
<codeline><highlight class="normal">B.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(B,<sp/>C);</highlight></codeline>
<codeline><highlight class="normal">C.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(C,<sp/>D);</highlight></codeline>
<codeline><highlight class="normal">D.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(D);</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">executor.<ref refid="classtf_1_1Executor_1a8d08f0cb79e7b3780087975d13368a96" kindref="member">run</ref>(taskflow).wait();<sp/><sp/></highlight><highlight class="comment">//<sp/>counter<sp/>==<sp/>40</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">taskflow.<ref refid="classtf_1_1Taskflow_1ac433018262e44b12c4cc9f0c4748d758" kindref="member">dump</ref>(<ref refid="cpp/io/basic_ostream" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref>);</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"><ref refid="cpp/io/basic_ostream" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/>&lt;&lt;<sp/></highlight><highlight class="stringliteral">&quot;counter<sp/>==<sp/>&quot;</highlight><highlight class="normal"><sp/>&lt;&lt;<sp/>counter<sp/>&lt;&lt;<sp/></highlight><highlight class="charliteral">&apos;\n&apos;</highlight><highlight class="normal">;</highlight></codeline>
</programlisting></para>
<para><dotfile name="/home/thuang295/Code/taskflow/doxygen/images/multi-condition-task-2.dot"></dotfile>
</para>
<para><simplesect kind="attention"><para>It is your responsibility to ensure the return of a multi-condition task goes to a correct successor task. If a returned index falls outside the successor range of a multi-condition task, the scheduler will skip that index without doing anything. </para>
</simplesect>
</para>
</sect1>
    </detaileddescription>
    <location file="doxygen/cookbook/conditional_tasking.dox"/>
  </compounddef>
</doxygen>
