<ahref="#ImplementIfElseControlFlow">Implement If-Else Control Flow</a>
</li>
<liclass="level2">
<ahref="#ImplementSwitchControlFlow">Implement Switch Control Flow</a>
</li>
<liclass="level2">
<ahref="#ImplementDoWhileLoopControlFlow">Implement Do-While-Loop Control Flow</a>
</li>
<liclass="level2">
<ahref="#ImplementWhileLoopControlFlow">Implement While-Loop Control Flow</a>
</li>
</ul>
</li>
<liclass="level1">
<ahref="#CreateAMultiConditionTask">Create a Multi-condition Task</a>
</li>
</ul>
</div>
<divclass="textblock"><p>One of the most powerful features that distinguishes Taskflow from other systems is its support for <em>conditional tasking</em>, also known as the <em>control taskflow programming model</em> (CTFG). CTFG allows you to embed control flow directly within a taskflow graph, enabling tasks to make decisions dynamically during execution. This mechanism supports advanced in-graph control flow patterns, such as dynamic branching, loops, and conditionals—that are typically difficult or impossible to express in traditional task graph models.</p>
<p>A condition task returns an integer index indicating which successor task to execute next. The index corresponds to the position of the successor in the order it was added during task construction. The following example creates an if-else block using a condition task.</p>
<divclass="line"><spanclass="comment">// executes no if cond returns 1</span></div>
<divclass="ttc" id="aclasstf_1_1FlowBuilder_html_a4d52a7fe2814b264846a2085e931652c"><divclass="ttname"><ahref="classtf_1_1FlowBuilder.html#a4d52a7fe2814b264846a2085e931652c">tf::FlowBuilder::emplace</a></div><divclass="ttdeci">Task emplace(C &&callable)</div><divclass="ttdoc">creates a static task</div><divclass="ttdef"><b>Definition</b> flow_builder.hpp:1781</div></div>
<divclass="ttc" id="aclasstf_1_1Task_html_a331b1b726555072e7c7d10941257f664"><divclass="ttname"><ahref="classtf_1_1Task.html#a331b1b726555072e7c7d10941257f664">tf::Task::succeed</a></div><divclass="ttdeci">Task & succeed(Ts &&... tasks)</div><divclass="ttdoc">adds precedence links from other tasks to this</div><divclass="ttdef"><b>Definition</b> task.hpp:1313</div></div>
<divclass="ttc" id="aclasstf_1_1Task_html_a8c78c453295a553c1c016e4062da8588"><divclass="ttname"><ahref="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">tf::Task::precede</a></div><divclass="ttdeci">Task & precede(Ts &&... tasks)</div><divclass="ttdoc">adds precedence links from this to other tasks</div><divclass="ttdef"><b>Definition</b> task.hpp:1305</div></div>
<divclass="ttc" id="aclasstf_1_1Taskflow_html"><divclass="ttname"><ahref="classtf_1_1Taskflow.html">tf::Taskflow</a></div><divclass="ttdoc">class to create a taskflow object</div><divclass="ttdef"><b>Definition</b> taskflow.hpp:64</div></div>
</div><!-- fragment --><divclass="dotgraph">
<iframescrolling="no" frameborder="0" src="dot_conditional-tasking-if-else.svg" width="370" height="131"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
<p>The condition task <code>cond</code> is connected to two successor tasks, <code>yes</code> and <code>no</code>, via <code>precede(yes, no)</code>. When <code>cond</code> returns <code>0</code>, the execution moves on to <code>yes</code>. When <code>cond</code> returns <code>1</code>, the execution moves on to <code>no</code>.</p>
<dlclass="section note"><dt>Note</dt><dd>It is your responsibility to ensure that the return value of a condition task corresponds to a valid successor. If the returned index is out of range, the executor will not schedule any successor tasks.</dd></dl>
<p>A condition task can form a cycle to express <em>iterative</em> control flow. The example below demonstrates a simple yet commonly used feedback loop implemented using a condition task that returns a random binary value. If the return value from <code>cond</code> is <code>0</code>, the task loops back to itself; otherwise, it proceeds to <code>stop</code>.</p>
<divclass="line">cond.<aclass="code hl_function" href="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">precede</a>(cond, stop); <spanclass="comment">// returns 0 to 'cond' or 1 to 'stop'</span></div>
<divclass="ttc" id="aclasstf_1_1Task_html"><divclass="ttname"><ahref="classtf_1_1Task.html">tf::Task</a></div><divclass="ttdoc">class to create a task handle over a taskflow node</div><divclass="ttdef"><b>Definition</b> task.hpp:569</div></div>
</div><!-- fragment --><divclass="dotgraph">
<iframescrolling="no" frameborder="0" src="dot_conditional-tasking-1.svg" width="370" height="106"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
<p>Creating a taskflow with complex control flow often requires only a few lines of code to implement. Different control flow paths can execute in parallel, making it easy to express both logic and concurrency. The code below creates a taskflow with three condition tasks to demonstrate this capability:</p>
<divclass="line">cond_1.<aclass="code hl_function" href="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">precede</a>(B, E); <spanclass="comment">// return 0 to 'B' or 1 to 'E'</span></div>
<divclass="line">cond_2.<aclass="code hl_function" href="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">precede</a>(G, H); <spanclass="comment">// return 0 to 'G' or 1 to 'H'</span></div>
<divclass="line">cond_3.<aclass="code hl_function" href="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">precede</a>(cond_3, L); <spanclass="comment">// return 0 to 'cond_3' or 1 to 'L'</span></div>
<divclass="ttc" id="aclasstf_1_1Taskflow_html_ac433018262e44b12c4cc9f0c4748d758"><divclass="ttname"><ahref="classtf_1_1Taskflow.html#ac433018262e44b12c4cc9f0c4748d758">tf::Taskflow::dump</a></div><divclass="ttdeci">void dump(std::ostream &ostream) const</div><divclass="ttdoc">dumps the taskflow to a DOT format through a std::ostream target</div><divclass="ttdef"><b>Definition</b> taskflow.hpp:433</div></div>
</div><!-- fragment --><p>The above code creates three condition tasks to implement three different control-flow tasks:</p><oltype="1">
<li>A condition task <code>cond_1</code> that loops back to <code>B</code> on returning <code>0</code>, or proceeds to <code>E</code> on returning <code>1</code>,</li>
<li>A condition task <code>cond_2</code> that goes to <code>G</code> on returning <code>0</code>, or <code>H</code> on returning <code>1</code>,</li>
<li>A condition task <code>cond_3</code> that loops back to itself on returning <code>0</code>, or proceeds to <code>L</code> on returning <code>1</code></li>
</ol>
<divclass="dotgraph">
<iframescrolling="no" frameborder="0" src="dot_conditional-tasking-2.svg" width="1170" height="259"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
<p>In this particular example, we can clearly see the advantage of CTFG: the execution of <code>cond_1</code> can overlap with <code>cond_2</code> or <code>cond_3</code>, enabling greater concurrency in control-driven workloads. Unlike traditional task graph models that require static structure or external orchestration to handle control flow, CTFG allows tasks to make decisions dynamically and continue execution without global synchronization barriers. This design leads to better parallelism, reduced overhead, and more expressive task graphs, especially in workloads with branching or iterative control flows.</p>
<p>In order to understand how an executor schedules condition tasks, we define two dependency types, <em>strong dependency</em> and <em>weak dependency</em>. A strong dependency is a preceding link from one non-condition task to another task. A weak dependency is a preceding link from one condition task to another task. The number of dependencies of a task is the sum of its strong dependencies and weak dependencies. The table below lists the number of strong dependencies and weak dependencies of each task in the previous example:</p>
<divclass="ttc" id="aclasstf_1_1Task_html_a0b7b789c9b8a21927a992f6ccc11de81"><divclass="ttname"><ahref="classtf_1_1Task.html#a0b7b789c9b8a21927a992f6ccc11de81">tf::Task::num_strong_dependencies</a></div><divclass="ttdeci">size_t num_strong_dependencies() const</div><divclass="ttdoc">queries the number of strong dependencies of the task</div><divclass="ttdef"><b>Definition</b> task.hpp:1445</div></div>
<divclass="ttc" id="aclasstf_1_1Task_html_ad5e874b7cc77df1e7dc875d436ff7b72"><divclass="ttname"><ahref="classtf_1_1Task.html#ad5e874b7cc77df1e7dc875d436ff7b72">tf::Task::num_weak_dependencies</a></div><divclass="ttdeci">size_t num_weak_dependencies() const</div><divclass="ttdoc">queries the number of weak dependencies of the task</div><divclass="ttdef"><b>Definition</b> task.hpp:1450</div></div>
<divclass="ttc" id="aclasstf_1_1Task_html_adefb65d68a64bd8a75364a8801cfec44"><divclass="ttname"><ahref="classtf_1_1Task.html#adefb65d68a64bd8a75364a8801cfec44">tf::Task::num_predecessors</a></div><divclass="ttdeci">size_t num_predecessors() const</div><divclass="ttdoc">queries the number of predecessors of the task</div><divclass="ttdef"><b>Definition</b> task.hpp:1440</div></div>
</div><!-- fragment --><p>When you submit a task to an executor, the scheduler starts with tasks of <em>zero dependencies</em> (both zero strong and weak dependencies) and continues to execute successive tasks whenever their <em>strong dependencies</em> are met. However, the scheduler skips this rule when executing a condition task and jumps directly to its successors indexed by the return value.</p>
<divclass="dotgraph">
<iframescrolling="no" frameborder="0" src="dot_task_level_scheduling.svg" width="1059" height="470"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
<p>Each task has an <em>atomic</em> join counter to keep track of strong dependencies that are met at runtime. When a task completes, the join counter is restored to the task's strong dependency number in the graph, such that the subsequent execution can reuse the counter again.</p>
<p>Let's take a look at an example to understand how task-level scheduling works. Suppose we have the following taskflow of one condition task <code>cond</code> that forms a loop to itself on returning <code>0</code> and moves on to <code>stop</code> on returning <code>1</code>:</p>
<divclass="dotgraph">
<iframescrolling="no" frameborder="0" src="dot_conditional-tasking-1.svg" width="370" height="106"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
<p>The scheduler starts with <code>init</code> task because it has no dependencies (both strong and weak dependencies). Then, the scheduler moves on to the condition task <code>cond</code>. If <code>cond</code> returns <code>0</code>, the scheduler enqueues <code>cond</code> and runs it again. If <code>cond</code> returns <code>1</code>, the scheduler enqueues <code>stop</code> and then moves on.</p>
<p>Condition tasks are powerful but require careful graph construction. The following pitfalls are the most common sources of bugs when using conditional tasking, ranging from silent deadlocks to non-deterministic task races.</p>
<p>Every taskflow must have at least one task with zero dependencies for the scheduler to start with. When a condition task forms a cycle, it is easy to accidentally create a graph where every task has at least one incoming edge, leaving the scheduler with no entry point.</p>
<p>The figure below shows common pitfalls and their remedies.</p>
<divclass="dotgraph">
<iframescrolling="no" frameborder="0" src="dot_conditional-tasking-pitfalls.svg" width="1232" height="371"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
<p>In the <code>error1</code> scenario, there is no source task for the scheduler to start with. The simplest fix is to add a task <code>S</code> with no dependencies that precedes the rest of the graph, giving the scheduler an unambiguous entry point.</p>
<h2><aclass="anchor" id="PitfallTaskRace"></a>
Pitfall 2: Task Race</h2>
<p>A task race occurs when the same task can be scheduled more than once simultaneously through different paths. This typically happens when a task has both a strong dependency from a regular task and a weak dependency from a condition task.</p>
<p>In the <code>error2</code> scenario, task <code>D</code> can be scheduled twice: once by <code>E</code> through its strong dependency, and once by <code>C</code> through its weak dependency (when <code>C</code> returns <code>1</code>). If both paths activate <code>D</code> at the same time, <code>D</code> runs concurrently with itself, which is undefined behavior. The fix is to insert an auxiliary task <code>D-aux</code> between the two paths so that <code>D</code> always has exactly one active predecessor at a time.</p>
<p>In the risky scenario, task <code>X</code> may be raced by <code>M</code> and <code>P</code> if <code>M</code> returns <code>0</code> and <code>P</code> returns <code>1</code> simultaneously, triggering <code>X</code> from two different condition tasks at once. Whenever a task sits at the junction of multiple condition task outputs, carefully check whether those condition tasks can fire concurrently.</p>
<h2><aclass="anchor" id="PitfallDeadlock"></a>
Pitfall 3: Deadlock from Strong Back-edge</h2>
<p>A deadlock occurs when a condition task loops back to a task via a strong dependency rather than a weak one. Because the scheduler only moves a task to the ready queue when all its <em>strong</em> dependencies are satisfied, a loop that creates a strong back-edge will permanently block: the loop body waits for the condition task to complete, but the condition task can never re-run because the loop body has not been executed yet.</p>
<p>The wrong while-loop implementation at <aclass="el" href="#ImplementWhileLoopControlFlow">Implement While-Loop Control Flow</a> is a concrete example of this pitfall. When the body task <code>i++</code> directly precedes the loop condition task <code>cond</code> with <code>body.precede(cond)</code>, it creates a strong dependency. After <code>init</code> runs and decrements <code>cond's</code> strong dependency count by one, <code>cond</code> still waits for <code>i++</code> to complete before it can run. But <code>i++</code> only runs after <code>cond</code> returns <code>0</code>, so neither task can proceed and the graph deadlocks. The correct fix is to introduce a dedicated back-edge condition task <code>back</code> that returns <code>0</code> unconditionally to <code>cond</code>, creating a weak dependency instead.</p>
<dlclass="section note"><dt>Note</dt><dd>When in doubt, use <aclass="el" href="classtf_1_1Taskflow.html#ac433018262e44b12c4cc9f0c4748d758" title="dumps the taskflow to a DOT format through a std::ostream target">tf::Taskflow::dump</a> to visualize your graph and cross-reference the strong and weak dependency counts in the table at <aclass="el" href="#TaskSchedulingPolicy">Understand our Task-level Scheduling</a>. A task that has both strong and weak incoming edges from active paths is a strong signal that a race or deadlock may be present.</dd></dl>
<p>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 <code>i</code>.</p>
<divclass="line">cond1.<aclass="code hl_function" href="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">precede</a>(equl1, cond2); <spanclass="comment">// goes to cond2 if i>1</span></div>
<divclass="line">cond2.<aclass="code hl_function" href="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">precede</a>(equl2, cond3); <spanclass="comment">// goes to cond3 if i>2</span></div>
<divclass="line">cond3.<aclass="code hl_function" href="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">precede</a>(equl3, grtr3); <spanclass="comment">// goes to grtr3 if i>3</span></div>
</div><!-- fragment --><divclass="dotgraph">
<iframescrolling="no" frameborder="0" src="dot_conditional-tasking-nested-if-else.svg" width="1304" height="203"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
<p>You can use condition tasks to implement <em>switch-style</em> control flow. The following example demonstrates this by creating a switch structure that randomly selects and executes one of three cases using four condition tasks.</p>
<iframescrolling="no" frameborder="0" src="dot_conditional-tasking-switch.svg" width="612" height="203"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
<p>Assuming <code>swcond</code> returns 1, the program outputs:</p>
</div><!-- fragment --><p>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.</p>
<divclass="fragment"><divclass="line"><spanclass="comment">// wrong implementation of switch control flow using only one condition task</span></div>
<iframescrolling="no" frameborder="0" src="dot_conditional-tasking-switch-wrong.svg" width="567" height="203"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
<p>In this faulty implementation, task <code>target</code> has three strong dependencies but only one of them will be met. This is because <code>swcond</code> is a condition task, and only one case task will be executed depending on the return of <code>swcond</code>.</p>
<p>You can use conditional tasking to implement <em>do-while-loop</em> control flow. The following example creates a do-while-loop control flow diagram that repeatedly increments variable <code>i</code> five times using one condition task.</p>
<iframescrolling="no" frameborder="0" src="dot_conditional-tasking-do-while.svg" width="596" height="62"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
<p>The program outputs:</p>
<divclass="fragment"><divclass="line">i=0</div>
<divclass="line">i++ => i=1</div>
<divclass="line">i++ => i=2</div>
<divclass="line">i++ => i=3</div>
<divclass="line">i++ => i=4</div>
<divclass="line">i++ => i=5</div>
<divclass="line">done</div>
</div><!-- fragment --><h2><aclass="anchor" id="ImplementWhileLoopControlFlow"></a>
Implement While-Loop Control Flow</h2>
<p>You can use conditional tasking to implement <em>while-loop</em> control flow. The following example creates a while-loop control flow diagram that repeatedly increments variable <code>i</code> five times using two condition task.</p>
<iframescrolling="no" frameborder="0" src="dot_conditional-tasking-while.svg" width="599" height="186"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
<p>The program outputs:</p>
<divclass="fragment"><divclass="line">i=0</div>
<divclass="line">while i<5</div>
<divclass="line">i++=0</div>
<divclass="line">back</div>
<divclass="line">while i<5</div>
<divclass="line">i++=1</div>
<divclass="line">back</div>
<divclass="line">while i<5</div>
<divclass="line">i++=2</div>
<divclass="line">back</div>
<divclass="line">while i<5</div>
<divclass="line">i++=3</div>
<divclass="line">back</div>
<divclass="line">while i<5</div>
<divclass="line">i++=4</div>
<divclass="line">back</div>
<divclass="line">while i<5</div>
<divclass="line">done</div>
</div><!-- fragment --><p>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.</p>
<divclass="fragment"><divclass="line"><spanclass="comment">// wrong implementation of while-loop using only one condition task</span></div>
<iframescrolling="no" frameborder="0" src="dot_conditional-tasking-while-wrong.svg" width="443" height="143"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
<p>In the taskflow diagram above, the scheduler starts with <code>init</code> and then decrements the strong dependency of the loop condition task, <code>while i<5</code>. After this, there remains one strong dependency, i.e., introduced by the loop body task, <code>i++</code>. However, task <code>i++</code> will not be executed until the loop condition task returns <code>0</code>, causing a deadlock.</p>
<p>A <em>multi-condition task</em> 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 <em>multi-condition task</em> 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, <code>A</code>, that informs the scheduler to run on its two successors, <code>B</code> and <code>D</code>.</p>
<divclass="ttc" id="aclasstf_1_1Executor_html"><divclass="ttname"><ahref="classtf_1_1Executor.html">tf::Executor</a></div><divclass="ttdoc">class to create an executor</div><divclass="ttdef"><b>Definition</b> executor.hpp:62</div></div>
<divclass="ttc" id="aclasstf_1_1Executor_html_a519777f5783981d534e9e53b99712069"><divclass="ttname"><ahref="classtf_1_1Executor.html#a519777f5783981d534e9e53b99712069">tf::Executor::run</a></div><divclass="ttdeci">tf::Future< void > run(Taskflow &taskflow)</div><divclass="ttdoc">runs a taskflow once</div></div>
<divclass="ttc" id="aclasstf_1_1SmallVector_html"><divclass="ttname"><ahref="classtf_1_1SmallVector.html">tf::SmallVector</a></div><divclass="ttdoc">class to define a vector optimized for small array</div><divclass="ttdef"><b>Definition</b> small_vector.hpp:931</div></div>
</div><!-- fragment --><divclass="dotgraph">
<iframescrolling="no" frameborder="0" src="dot_multi-condition-task-1.svg" width="275" height="178"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
<dlclass="section note"><dt>Note</dt><dd>The return type of a multi-condition task is <aclass="el" href="classtf_1_1SmallVector.html" title="class to define a vector optimized for small array">tf::SmallVector</a>, which provides C++ vector-style functionalities but comes with small buffer optimization. </dd></dl>
</div></div><!-- contents -->
</div><!-- PageDoc -->
</div><!-- doc-content -->
<!-- HTML footer for doxygen 1.13.1-->
<!-- start footer part -->
<divid="nav-path" class="navpath"><!-- id is needed for treeview function! -->