同步操作将从 Gitee 极速下载/cpp-taskflow 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
<?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.12.0" 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>One of the most powerful features that distinguishes Taskflow from other systems is its support for <emphasis>conditional tasking</emphasis>, also known as the <emphasis>control taskflow programming model</emphasis> (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.</para><sect1 id="ConditionalTasking_1CreateAConditionTask"><title>Create a Condition Task</title><para>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.</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="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"yes\n"</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="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"no\n"</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="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 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.</para></simplesect>A condition task can form a cycle to express <emphasis>iterative</emphasis> control flow. The example below demonstrates a simple yet commonly used feedback loop implemented using a condition task (lines 7–10) that returns a random binary value. If the return value from <computeroutput>cond</computeroutput> is <computeroutput>0</computeroutput>, the task loops back to itself; otherwise, it proceeds 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">"init"</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">"stop"</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="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"flipping<sp/>a<sp/>coin\n"</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="/Users/twhuang/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">"cond"</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/>'cond'<sp/>or<sp/>1<sp/>to<sp/>'stop'</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="conditional-tasking-1.dot"></dotfile></para><para>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:</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">"A"</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">"B"</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">"C"</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">"D"</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">"E"</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">"F"</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">"G"</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">"H"</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">"I"</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">"K"</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">"L"</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">"M"</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="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::rand</ref>()%2;<sp/>}).name(</highlight><highlight class="stringliteral">"cond_1"</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="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::rand</ref>()%2;<sp/>}).name(</highlight><highlight class="stringliteral">"cond_2"</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="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::rand</ref>()%2;<sp/>}).name(</highlight><highlight class="stringliteral">"cond_3"</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/>'B'<sp/>or<sp/>1<sp/>to<sp/>'E'</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/>'G'<sp/>or<sp/>1<sp/>to<sp/>'H'</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/>'cond_3'<sp/>or<sp/>1<sp/>to<sp/>'L'</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="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref>);</highlight></codeline></programlisting></para><para>The above code creates three condition tasks to implement three different control-flow tasks:<orderedlist><listitem><para>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>,</para></listitem><listitem><para>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>,</para></listitem><listitem><para>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></listitem></orderedlist></para><para><dotfile name="conditional-tasking-2.dot"></dotfile></para><para>In this particular example, we can clearly see the advantage of CTFG: the execution of <computeroutput>cond_1</computeroutput> can overlap with <computeroutput>cond_2</computeroutput> or <computeroutput>cond_3</computeroutput>, 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.</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 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:</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>dependencies </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 dependencies, the number of weak dependencies, and the number of dependencies 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="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/>task.<ref refid="classtf_1_1Task_1adefb65d68a64bd8a75364a8801cfec44" kindref="member">num_predecessors</ref>()<sp/><<<sp/></highlight><highlight class="charliteral">'\n'</highlight><highlight class="normal">;</highlight></codeline><codeline><highlight class="normal">8:<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/>task.<ref refid="classtf_1_1Task_1a0b7b789c9b8a21927a992f6ccc11de81" kindref="member">num_strong_dependencies</ref>()<sp/><<<sp/></highlight><highlight class="charliteral">'\n'</highlight><highlight class="normal">;<sp/></highlight></codeline><codeline><highlight class="normal">9:<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/>task.<ref refid="classtf_1_1Task_1ad5e874b7cc77df1e7dc875d436ff7b72" kindref="member">num_weak_dependencies</ref>()<sp/><<<sp/></highlight><highlight class="charliteral">'\n'</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 dependencies</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="task_level_scheduling.dot"></dotfile></para><para>Each task has an <emphasis>atomic</emphasis> 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.</para><sect2 id="ConditionalTasking_1TaskLevelSchedulingExample"><title>Example</title><para>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 <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="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="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 dependencies. 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>([&](){<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>([&](){<sp/></highlight><highlight class="keywordflow">return</highlight><highlight class="normal"><sp/>i>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>([&](){<sp/></highlight><highlight class="keywordflow">return</highlight><highlight class="normal"><sp/>i>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>([&](){<sp/></highlight><highlight class="keywordflow">return</highlight><highlight class="normal"><sp/>i>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>([&](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"i=1\n"</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>([&](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"i=2\n"</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>([&](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"i=3\n"</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>([&](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"i>3\n"</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>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>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>3</highlight></codeline></programlisting></para><para><dotfile name="conditional-tasking-nested-if-else.dot"></dotfile></para></sect2><sect2 id="ConditionalTasking_1ImplementSwitchControlFlow"><title>Implement Switch Control Flow</title><para>You can use condition tasks to implement <emphasis>switch-style</emphasis> 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.</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="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"source\n"</highlight><highlight class="normal">;<sp/>},</highlight></codeline><codeline><highlight class="normal"><sp/><sp/>[](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"switch\n"</highlight><highlight class="normal">;<sp/></highlight><highlight class="keywordflow">return</highlight><highlight class="normal"><sp/>rand()%3;<sp/>},</highlight></codeline><codeline><highlight class="normal"><sp/><sp/>[](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"case<sp/>1\n"</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="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"case<sp/>2\n"</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="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"case<sp/>3\n"</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="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"target\n"</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="conditional-tasking-switch.dot"></dotfile></para><para>Assuming <computeroutput>swcond</computeroutput> returns 1, the program outputs:</para><para><programlisting filename=".bash"><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="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"source\n"</highlight><highlight class="normal">;<sp/>},</highlight></codeline><codeline><highlight class="normal"><sp/><sp/>[](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"switch\n"</highlight><highlight class="normal">;<sp/></highlight><highlight class="keywordflow">return</highlight><highlight class="normal"><sp/>rand()%3;<sp/>},</highlight></codeline><codeline><highlight class="normal"><sp/><sp/>[](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"case<sp/>1\n"</highlight><highlight class="normal">;<sp/>},</highlight></codeline><codeline><highlight class="normal"><sp/><sp/>[](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"case<sp/>2\n"</highlight><highlight class="normal">;<sp/>},</highlight></codeline><codeline><highlight class="normal"><sp/><sp/>[](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"case<sp/>3\n"</highlight><highlight class="normal">;<sp/>},</highlight></codeline><codeline><highlight class="normal"><sp/><sp/>[](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"target\n"</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="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/>[&](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"i=0\n"</highlight><highlight class="normal">;<sp/>i=0;<sp/>},</highlight></codeline><codeline><highlight class="normal"><sp/><sp/>[&](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"i++<sp/>=><sp/>i="</highlight><highlight class="normal">;<sp/>i++;<sp/>},</highlight></codeline><codeline><highlight class="normal"><sp/><sp/>[&](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/>i<sp/><<<sp/></highlight><highlight class="charliteral">'\n'</highlight><highlight class="normal">;<sp/></highlight><highlight class="keywordflow">return</highlight><highlight class="normal"><sp/>i<5<sp/>?<sp/>0<sp/>:<sp/>1;<sp/>},</highlight></codeline><codeline><highlight class="normal"><sp/><sp/>[&](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"done\n"</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="conditional-tasking-do-while.dot"></dotfile></para><para>The program outputs:</para><para><programlisting filename=".bash"><codeline><highlight class="normal">i=0</highlight></codeline><codeline><highlight class="normal">i++<sp/>=><sp/>i=1</highlight></codeline><codeline><highlight class="normal">i++<sp/>=><sp/>i=2</highlight></codeline><codeline><highlight class="normal">i++<sp/>=><sp/>i=3</highlight></codeline><codeline><highlight class="normal">i++<sp/>=><sp/>i=4</highlight></codeline><codeline><highlight class="normal">i++<sp/>=><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/>[&](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"i=0\n"</highlight><highlight class="normal">;<sp/>i=0;<sp/>},</highlight></codeline><codeline><highlight class="normal"><sp/><sp/>[&](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"while<sp/>i<5\n"</highlight><highlight class="normal">;<sp/></highlight><highlight class="keywordflow">return</highlight><highlight class="normal"><sp/>i<sp/><<sp/>5<sp/>?<sp/>0<sp/>:<sp/>1;<sp/>},</highlight></codeline><codeline><highlight class="normal"><sp/><sp/>[&](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"i++="</highlight><highlight class="normal"><sp/><<<sp/>i++<sp/><<<sp/></highlight><highlight class="charliteral">'\n'</highlight><highlight class="normal">;<sp/>},</highlight></codeline><codeline><highlight class="normal"><sp/><sp/>[&](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"back\n"</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="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"done\n"</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="conditional-tasking-while.dot"></dotfile></para><para>The program outputs:</para><para><programlisting filename=".bash"><codeline><highlight class="normal">i=0</highlight></codeline><codeline><highlight class="normal">while<sp/>i<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<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<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<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<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<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/>[&](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"i=0\n"</highlight><highlight class="normal">;<sp/>i=0;<sp/>},</highlight></codeline><codeline><highlight class="normal"><sp/><sp/>[&](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"while<sp/>i<5\n"</highlight><highlight class="normal">;<sp/></highlight><highlight class="keywordflow">return</highlight><highlight class="normal"><sp/>i<sp/><<sp/>5<sp/>?<sp/>0<sp/>:<sp/>1;<sp/>},</highlight></codeline><codeline><highlight class="normal"><sp/><sp/>[&](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"i++="</highlight><highlight class="normal"><sp/><<<sp/>i++<sp/><<<sp/></highlight><highlight class="charliteral">'\n'</highlight><highlight class="normal">;<sp/>},</highlight></codeline><codeline><highlight class="normal"><sp/><sp/>[&](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"done\n"</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="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<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>([&]()<sp/>-><sp/><ref refid="classtf_1_1SmallVector" kindref="compound">tf::SmallVector<int></ref><sp/>{<sp/></highlight></codeline><codeline><highlight class="normal"><sp/><sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"A\n"</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">"A"</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>([&](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"B\n"</highlight><highlight class="normal">;<sp/>}).name(</highlight><highlight class="stringliteral">"B"</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>([&](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"C\n"</highlight><highlight class="normal">;<sp/>}).name(</highlight><highlight class="stringliteral">"C"</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>([&](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"D\n"</highlight><highlight class="normal">;<sp/>}).name(</highlight><highlight class="stringliteral">"D"</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_1a519777f5783981d534e9e53b99712069" kindref="member">run</ref>(taskflow).wait();</highlight></codeline></programlisting></para><para><dotfile name="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></para></sect1></detaileddescription><location file="doxygen/cookbook/conditional_tasking.dox"/></compounddef></doxygen>
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。