You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.
Dismiss alert
<p>After you create a task dependency graph, you need to submit it to threads for execution. In this chapter, we will show you how to execute a task dependency graph.</p><sectionid="CreateAnExecutor"><h2><ahref="#CreateAnExecutor">Create an Executor</a></h2><p>To execute a taskflow, you need to create an <em>executor</em> of type <ahref="classtf_1_1Executor.html" class="m-doc">tf::<wbr/>Executor</a>. An executor is a <em>thread-safe</em> object that manages a set of worker threads and executes tasks through an efficient <em>work-stealing</em> algorithm. Issuing a call to run a taskflow creates a <em>topology</em>, a data structure to keep track of the execution status of a running graph. <ahref="classtf_1_1Executor.html" class="m-doc">tf::<wbr/>Executor</a> takes an unsigned integer to construct with <code>N</code> worker threads. The default value is <ahref="http://en.cppreference.com/w/cpp/thread/thread/hardware_concurrency.html" class="m-doc-external">std::<wbr/>thread::<wbr/>hardware_concurrency</a>.</p><preclass="m-code"><spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">Executor</span><spanclass="w"></span><spanclass="n">executor1</span><spanclass="p">;</span><spanclass="w"></span><spanclass="c1">// create an executor with the number of workers</span>
<spanclass="w"></span><spanclass="c1">// equal to std::thread::hardware_concurrency</span>
<spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">Executor</span><spanclass="w"></span><spanclass="nf">executor2</span><spanclass="p">(</span><spanclass="mi">4</span><spanclass="p">);</span><spanclass="w"></span><spanclass="c1">// create an executor of 4 worker threads</span></pre><p>An executor can be reused to execute multiple taskflows. In most workloads, you may need only one executor to run multiple taskflows where each taskflow represents a part of a parallel decomposition.</p></section><sectionid="ExecuteATaskflow"><h2><ahref="#ExecuteATaskflow">Execute a Taskflow</a></h2><p><ahref="classtf_1_1Executor.html" class="m-doc">tf::<wbr/>Executor</a> provides a set of <code>run_*</code> methods, <ahref="classtf_1_1Executor.html#a8d08f0cb79e7b3780087975d13368a96" class="m-doc">tf::<wbr/>Executor::<wbr/>run</a>, <ahref="classtf_1_1Executor.html#af15db5f7dde8e7ff1f86ef8fe825e9e2" class="m-doc">tf::<wbr/>Executor::<wbr/>run_n</a>, and <ahref="classtf_1_1Executor.html#ae4f9e214ea5ee873e8d90a70bc1c77e8" class="m-doc">tf::<wbr/>Executor::<wbr/>run_until</a> to run a taskflow for one time, multiple times, or until a given predicate evaluates to true. All methods accept an optional callback to invoke after the execution completes, and return a <ahref="classtf_1_1Future.html" class="m-doc">tf::<wbr/>Future</a> for users to access the execution status. The code below shows several ways to run a taskflow.</p><preclass="m-code"><spanclass="w"></span><spanclass="mi">1</span><spanclass="o">:</span><spanclass="w"></span><spanclass="c1">// Declare an executor and a taskflow</span>
<spanclass="w"></span><spanclass="mi">5</span><spanclass="o">:</span><spanclass="w"></span><spanclass="c1">// Add three tasks into the taskflow</span>
<spanclass="w"></span><spanclass="mi">6</span><spanclass="o">:</span><spanclass="w"></span><spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">Task</span><spanclass="w"></span><spanclass="n">A</span><spanclass="w"></span><spanclass="o">=</span><spanclass="w"></span><spanclass="n">taskflow</span><spanclass="p">.</span><spanclass="n">emplace</span><spanclass="p">([]</span><spanclass="w"></span><spanclass="p">()</span><spanclass="w"></span><spanclass="p">{</span><spanclass="w"></span><spanclass="n">std</span><spanclass="o">::</span><spanclass="n">cout</span><spanclass="w"></span><spanclass="o"><<</span><spanclass="w"></span><spanclass="s">"This is TaskA</span><spanclass="se">\n</span><spanclass="s">"</span><spanclass="p">;</span><spanclass="w"></span><spanclass="p">});</span>
<spanclass="w"></span><spanclass="mi">7</span><spanclass="o">:</span><spanclass="w"></span><spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">Task</span><spanclass="w"></span><spanclass="n">B</span><spanclass="w"></span><spanclass="o">=</span><spanclass="w"></span><spanclass="n">taskflow</span><spanclass="p">.</span><spanclass="n">emplace</span><spanclass="p">([]</span><spanclass="w"></span><spanclass="p">()</span><spanclass="w"></span><spanclass="p">{</span><spanclass="w"></span><spanclass="n">std</span><spanclass="o">::</span><spanclass="n">cout</span><spanclass="w"></span><spanclass="o"><<</span><spanclass="w"></span><spanclass="s">"This is TaskB</span><spanclass="se">\n</span><spanclass="s">"</span><spanclass="p">;</span><spanclass="w"></span><spanclass="p">});</span>
<spanclass="w"></span><spanclass="mi">8</span><spanclass="o">:</span><spanclass="w"></span><spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">Task</span><spanclass="w"></span><spanclass="n">C</span><spanclass="w"></span><spanclass="o">=</span><spanclass="w"></span><spanclass="n">taskflow</span><spanclass="p">.</span><spanclass="n">emplace</span><spanclass="p">([]</span><spanclass="w"></span><spanclass="p">()</span><spanclass="w"></span><spanclass="p">{</span><spanclass="w"></span><spanclass="n">std</span><spanclass="o">::</span><spanclass="n">cout</span><spanclass="w"></span><spanclass="o"><<</span><spanclass="w"></span><spanclass="s">"This is TaskC</span><spanclass="se">\n</span><spanclass="s">"</span><spanclass="p">;</span><spanclass="w"></span><spanclass="p">});</span>
<spanclass="mi">14</span><spanclass="o">:</span><spanclass="w"></span><spanclass="n">fu</span><spanclass="p">.</span><spanclass="n">wait</span><spanclass="p">();</span><spanclass="w"></span><spanclass="c1">// block until the execution completes</span>
<spanclass="mi">15</span><spanclass="o">:</span>
<spanclass="mi">16</span><spanclass="o">:</span><spanclass="w"></span><spanclass="n">executor</span><spanclass="p">.</span><spanclass="n">run</span><spanclass="p">(</span><spanclass="n">taskflow</span><spanclass="p">,</span><spanclass="w"></span><spanclass="p">[](){</span><spanclass="w"></span><spanclass="n">std</span><spanclass="o">::</span><spanclass="n">cout</span><spanclass="w"></span><spanclass="o"><<</span><spanclass="w"></span><spanclass="s">"end of 1 run"</span><spanclass="p">;</span><spanclass="w"></span><spanclass="p">}).</span><spanclass="n">wait</span><spanclass="p">();</span>
<spanclass="mi">18</span><spanclass="o">:</span><spanclass="w"></span><spanclass="n">executor</span><spanclass="p">.</span><spanclass="n">wait_for_all</span><spanclass="p">();</span><spanclass="w"></span><spanclass="c1">// block until all associated executions finish</span>
<spanclass="mi">19</span><spanclass="o">:</span><spanclass="w"></span><spanclass="n">executor</span><spanclass="p">.</span><spanclass="n">run_n</span><spanclass="p">(</span><spanclass="n">taskflow</span><spanclass="p">,</span><spanclass="w"></span><spanclass="mi">4</span><spanclass="p">,</span><spanclass="w"></span><spanclass="p">[](){</span><spanclass="w"></span><spanclass="n">std</span><spanclass="o">::</span><spanclass="n">cout</span><spanclass="w"></span><spanclass="o"><<</span><spanclass="w"></span><spanclass="s">"end of 4 runs"</span><spanclass="p">;</span><spanclass="w"></span><spanclass="p">}).</span><spanclass="n">wait</span><spanclass="p">();</span>
<spanclass="mi">20</span><spanclass="o">:</span><spanclass="w"></span><spanclass="n">executor</span><spanclass="p">.</span><spanclass="n">run_until</span><spanclass="p">(</span><spanclass="n">taskflow</span><spanclass="p">,</span><spanclass="w"></span><spanclass="p">[</span><spanclass="n">cnt</span><spanclass="o">=</span><spanclass="mi">0</span><spanclass="p">]</span><spanclass="w"></span><spanclass="p">()</span><spanclass="w"></span><spanclass="k">mutable</span><spanclass="w"></span><spanclass="p">{</span><spanclass="w"></span><spanclass="k">return</span><spanclass="w"></span><spanclass="o">++</span><spanclass="n">cnt</span><spanclass="w"></span><spanclass="o">==</span><spanclass="w"></span><spanclass="mi">10</span><spanclass="p">;</span><spanclass="w"></span><spanclass="p">});</span></pre><p>Debrief:</p><ul><li>Lines 6-8 create a taskflow of three tasks A, B, and C</li><li>Lines 13-14 run the taskflow once and wait for completion</li><li>Line 16 runs the taskflow once with a callback to invoke when the execution finishes</li><li>Lines 17-18 run the taskflow four times and use <ahref="classtf_1_1Executor.html#ab9aa252f70e9a40020a1e5a89d485b85" class="m-doc">tf::<wbr/>Executor::<wbr/>wait_for_all</a> to wait for completion</li><li>Line 19 runs the taskflow four times and invokes a callback at the end of the fourth execution</li><li>Line 20 keeps running the taskflow until the predicate returns true</li></ul><p>Issuing multiple runs on the same taskflow will automatically <em>synchronize</em> to a sequential chain of executions in the order of run calls.</p><preclass="m-code"><spanclass="n">executor</span><spanclass="p">.</span><spanclass="n">run</span><spanclass="p">(</span><spanclass="n">taskflow</span><spanclass="p">);</span><spanclass="w"></span><spanclass="c1">// execution 1</span>
<spanclass="n">executor</span><spanclass="p">.</span><spanclass="n">wait_for_all</span><spanclass="p">();</span><spanclass="w"></span><spanclass="c1">// execution 1 -> execution 2 -> execution 3</span></pre><asideclass="m-note m-warning"><h4>Attention</h4><p>A running taskflow must remain alive during its execution. It is your responsibility to ensure a taskflow not being destructed when it is running. For example, the code below can result undefined behavior.</p></aside><preclass="m-code"><spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">Executor</span><spanclass="w"></span><spanclass="n">executor</span><spanclass="p">;</span><spanclass="w"></span><spanclass="c1">// create an executor</span>
<spanclass="c1">// create a taskflow whose lifetime is restricted by the scope</span>
<spanclass="p">}</span><spanclass="w"></span><spanclass="c1">// leaving the scope will destroy taskflow while it is running, </span>
<spanclass="w"></span><spanclass="c1">// resulting in undefined behavior</span></pre><p>Similarly, you should avoid touching a taskflow while it is running.</p><preclass="m-code"><spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">Taskflow</span><spanclass="w"></span><spanclass="n">taskflow</span><spanclass="p">;</span>
<spanclass="c1">// Add tasks into the taskflow</span>
<spanclass="c1">// alter the taskflow while running leads to undefined behavior </span>
<spanclass="n">taskflow</span><spanclass="p">.</span><spanclass="n">emplace</span><spanclass="p">([](){</span><spanclass="w"></span><spanclass="n">std</span><spanclass="o">::</span><spanclass="n">cout</span><spanclass="w"></span><spanclass="o"><<</span><spanclass="w"></span><spanclass="s">"Add a new task</span><spanclass="se">\n</span><spanclass="s">"</span><spanclass="p">;</span><spanclass="w"></span><spanclass="p">});</span></pre><p>You must always keep a taskflow alive and must not modify it while it is running on an executor.</p></section><sectionid="ExecuteATaskflowWithTransferredOwnership"><h2><ahref="#ExecuteATaskflowWithTransferredOwnership">Execute a Taskflow with Transferred Ownership</a></h2><p>You can transfer the ownership of a taskflow to an executor and run it without wrangling with the lifetime issue of that taskflow. Each <code>run_*</code> method discussed in the previous section comes with an overload that takes a <em>moved</em> taskflow object.</p><preclass="m-code"><spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">Taskflow</span><spanclass="w"></span><spanclass="n">taskflow</span><spanclass="p">;</span>
<spanclass="c1">// now taskflow has no tasks</span>
<spanclass="n">assert</span><spanclass="p">(</span><spanclass="n">taskflow</span><spanclass="p">.</span><spanclass="n">num_tasks</span><spanclass="p">()</span><spanclass="w"></span><spanclass="o">==</span><spanclass="w"></span><spanclass="mi">0</span><spanclass="p">);</span></pre><p>However, you should avoid moving a <em>running</em> taskflow which can result in undefined behavior.</p><preclass="m-code"><spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">Taskflow</span><spanclass="w"></span><spanclass="n">taskflow</span><spanclass="p">;</span>
<spanclass="c1">// error! you cannot move a taskflow while it is running</span>
<spanclass="n">executor</span><spanclass="p">.</span><spanclass="n">run</span><spanclass="p">(</span><spanclass="n">std</span><spanclass="o">::</span><spanclass="n">move</span><spanclass="p">(</span><spanclass="n">taskflow</span><spanclass="p">));</span><spanclass="w"></span></pre><p>The correct way to submit a taskflow with moved ownership to an executor is to ensure all previous runs have completed. The executor will automatically release the resources of a moved taskflow right <em>after</em> its execution completes.</p><preclass="m-code"><spanclass="c1">// submit the taskflow and wait until it completes</span>
<spanclass="c1">// now it's safe to move the taskflow to the executor and run it</span>
<spanclass="n">executor</span><spanclass="p">.</span><spanclass="n">run</span><spanclass="p">(</span><spanclass="n">std</span><spanclass="o">::</span><spanclass="n">move</span><spanclass="p">(</span><spanclass="n">taskflow</span><spanclass="p">));</span><spanclass="w"></span></pre><p>Likewise, you cannot move a taskflow that is running on an executor. You must wait until all the previous fires of runs on that taskflow complete before calling move.</p><preclass="m-code"><spanclass="c1">// submit the taskflow and wait until it completes</span>
<spanclass="c1">// now it's safe to move the taskflow to another</span>
<spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">Taskflow</span><spanclass="w"></span><spanclass="nf">moved_taskflow</span><spanclass="p">(</span><spanclass="n">std</span><spanclass="o">::</span><spanclass="n">move</span><spanclass="p">(</span><spanclass="n">taskflow</span><spanclass="p">));</span><spanclass="w"></span></pre></section><sectionid="ExecuteATaskflowFromAnInternalWorker"><h2><ahref="#ExecuteATaskflowFromAnInternalWorker">Execute a Taskflow from an Internal Worker</a></h2><p>Each run variant of <ahref="classtf_1_1Executor.html" class="m-doc">tf::<wbr/>Executor</a> returns a <ahref="classtf_1_1Future.html" class="m-doc">tf::<wbr/>Future</a> object which allows you to wait for the result to complete. When calling <code>tf::Future::wait</code>, the caller blocks without doing anything until the associated state is written to be ready. This design, however, can introduce deadlock problem especially when you need to run multiple taskflows from the internal workers of an executor. For example, the code below creates a taskflow of 1000 tasks with each task running a taskflow of 500 tasks in a blocking fashion:</p><preclass="m-code"><spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">Executor</span><spanclass="w"></span><spanclass="nf">executor</span><spanclass="p">(</span><spanclass="mi">2</span><spanclass="p">);</span>
<spanclass="n">executor</span><spanclass="p">.</span><spanclass="n">run</span><spanclass="p">(</span><spanclass="n">taskflow</span><spanclass="p">).</span><spanclass="n">wait</span><spanclass="p">();</span></pre><p>To avoid this problem, the executor has a method, <ahref="classtf_1_1Executor.html#a8fcd9e0557922bb8194999f0cd433ea8" class="m-doc">tf::<wbr/>Executor::<wbr/>corun</a>, to execute a taskflow from a worker of that executor. The worker will not block but co-run the taskflow with other tasks in its work-stealing loop.</p><preclass="m-code"><spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">Executor</span><spanclass="w"></span><spanclass="nf">executor</span><spanclass="p">(</span><spanclass="mi">2</span><spanclass="p">);</span>
<spanclass="n">executor</span><spanclass="p">.</span><spanclass="n">run</span><spanclass="p">(</span><spanclass="n">taskflow</span><spanclass="p">).</span><spanclass="n">wait</span><spanclass="p">();</span></pre><p>Similar to <ahref="classtf_1_1Executor.html#a8fcd9e0557922bb8194999f0cd433ea8" class="m-doc">tf::<wbr/>Executor::<wbr/>corun</a>, the method <ahref="classtf_1_1Executor.html#a0fc6eb19f168dc4a9cd0a7c6187c1d2d" class="m-doc">tf::<wbr/>Executor::<wbr/>corun_until</a> is another variant that keeps the calling worker in the work-stealing loop until the given predicate becomes true. You can use this method to prevent blocking a worker from doing useful things, such as being blocked when submitting an outstanding task (e.g., a GPU operation).</p><preclass="m-code"><spanclass="n">taskflow</span><spanclass="p">.</span><spanclass="n">emplace</span><spanclass="p">([</span><spanclass="o">&</span><spanclass="p">](){</span>
<spanclass="p">});</span></pre><asideclass="m-note m-warning"><h4>Attention</h4><p>You must call <ahref="classtf_1_1Executor.html#a0fc6eb19f168dc4a9cd0a7c6187c1d2d" class="m-doc">tf::<wbr/>Executor::<wbr/>corun_until</a> and <ahref="classtf_1_1Executor.html#a8fcd9e0557922bb8194999f0cd433ea8" class="m-doc">tf::<wbr/>Executor::<wbr/>corun</a> from a worker of the calling executor or an exception will be thrown.</p></aside></section><sectionid="ThreadSafety"><h2><ahref="#ThreadSafety">Touch an Executor from Multiple Threads</a></h2><p>All <code>run_*</code> methods are <em>thread-safe</em>. You can have multiple threads call these methods from an executor to run different taskflows. However, the order which taskflow runs first is non-deterministic and is up to the runtime.</p><preclass="m-code"><spanclass="w"></span><spanclass="mi">1</span><spanclass="o">:</span><spanclass="w"></span><spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">Executor</span><spanclass="w"></span><spanclass="n">executor</span><spanclass="p">;</span>
<spanclass="w"></span><spanclass="mi">5</span><spanclass="o">:</span><spanclass="w"></span><spanclass="c1">// ... modify my taskflow at i</span>
<spanclass="w"></span><spanclass="mi">6</span><spanclass="o">:</span><spanclass="w"></span><spanclass="n">executor</span><spanclass="p">.</span><spanclass="n">run</span><spanclass="p">(</span><spanclass="n">taskflows</span><spanclass="p">[</span><spanclass="n">i</span><spanclass="p">]);</span><spanclass="w"></span><spanclass="c1">// run my taskflow at i</span>
<spanclass="mi">10</span><spanclass="o">:</span><spanclass="w"></span><spanclass="n">executor</span><spanclass="p">.</span><spanclass="n">wait_for_all</span><spanclass="p">();</span></pre></section><sectionid="QueryTheWorkerID"><h2><ahref="#QueryTheWorkerID">Query the Worker ID</a></h2><p>Each worker in an executor has an unique integer identifier in the range <code>[0, N)</code> that can be queried by the caller thread using <ahref="classtf_1_1Executor.html#a6487d589cb1f6b078b69fd3bb1082345" class="m-doc">tf::<wbr/>Executor::<wbr/>this_worker_id</a>. If the caller thread is not a worker in the executor, <code>-1</code> is returned. This method is convenient for users to maintain a one-to-one mapping between a worker and its application data structure.</p><preclass="m-code"><spanclass="n">std</span><spanclass="o">::</span><spanclass="n">vector</span><spanclass="o"><</span><spanclass="kt">int</span><spanclass="o">></span><spanclass="w"></span><spanclass="n">worker_vectors</span><spanclass="p">[</span><spanclass="mi">8</span><spanclass="p">];</span><spanclass="w"></span><spanclass="c1">// one vector per worker</span>
<spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">Executor</span><spanclass="w"></span><spanclass="nf">executor</span><spanclass="p">(</span><spanclass="mi">8</span><spanclass="p">);</span><spanclass="w"></span><spanclass="c1">// an executor of eight workers</span>
<spanclass="n">assert</span><spanclass="p">(</span><spanclass="n">executor</span><spanclass="p">.</span><spanclass="n">this_worker_id</span><spanclass="p">()</span><spanclass="w"></span><spanclass="o">==</span><spanclass="w"></span><spanclass="mi">-1</span><spanclass="p">);</span><spanclass="w"></span><spanclass="c1">// master thread is not a worker</span>
<spanclass="w"></span><spanclass="kt">int</span><spanclass="w"></span><spanclass="n">id</span><spanclass="w"></span><spanclass="o">=</span><spanclass="w"></span><spanclass="n">executor</span><spanclass="p">.</span><spanclass="n">this_worker_id</span><spanclass="p">();</span><spanclass="w"></span><spanclass="c1">// in the range [0, 8)</span>
<spanclass="p">});</span></pre></section><sectionid="ObserveThreadActivities"><h2><ahref="#ObserveThreadActivities">Observe Thread Activities</a></h2><p>You can observe thread activities in an executor when a worker thread participates in executing a task and leaves the execution using <ahref="classtf_1_1ObserverInterface.html" class="m-doc">tf::<wbr/>ObserverInterface</a> – an <em>interface</em> class that provides a set of methods for you to define what to do when a thread enters and leaves the execution context of a task.</p><preclass="m-code"><spanclass="k">class</span><spanclass="w"></span><spanclass="nc">ObserverInterface</span><spanclass="w"></span><spanclass="p">{</span>
<spanclass="p">};</span></pre><p>There are three methods you must define in your derived class, <ahref="classtf_1_1ObserverInterface.html#a41e6e62f12bf9d9dc4fa74632f6825d9" class="m-doc">tf::<wbr/>ObserverInterface::<wbr/>set_up</a>, <ahref="classtf_1_1ObserverInterface.html#a8225fcacb03089677a1efc4b16b734cc" class="m-doc">tf::<wbr/>ObserverInterface::<wbr/>on_entry</a>, and <ahref="classtf_1_1ObserverInterface.html#aa22f5378154653f08d9a58326bda4754" class="m-doc">tf::<wbr/>ObserverInterface::<wbr/>on_exit</a>. The method, <ahref="classtf_1_1ObserverInterface.html#a41e6e62f12bf9d9dc4fa74632f6825d9" class="m-doc">tf::<wbr/>ObserverInterface::<wbr/>set_up</a>, is a constructor-like method that will be called by the executor when the observer is constructed. It passes an argument of the number of workers to observer in the executor. You may use it to preallocate or initialize data storage, e.g., an independent vector for each worker. The methods, <ahref="classtf_1_1ObserverInterface.html#a8225fcacb03089677a1efc4b16b734cc" class="m-doc">tf::<wbr/>ObserverInterface::<wbr/>on_entry</a> and <ahref="classtf_1_1ObserverInterface.html#aa22f5378154653f08d9a58326bda4754" class="m-doc">tf::<wbr/>ObserverInterface::<wbr/>on_exit</a>, are called by a worker thread before and after the execution context of a task, respectively. Both methods provide immutable access to the underlying worker and the running task using <ahref="classtf_1_1WorkerView.html" class="m-doc">tf::<wbr/>WorkerView</a> and <ahref="classtf_1_1TaskView.html" class="m-doc">tf::<wbr/>TaskView</a>. You may use them to record timepoints and calculate the elapsed time of a task.</p><p>You can associate an executor with one or multiple observers (though one is common) using <ahref="classtf_1_1Executor.html#ab5a793a0f2ce41cd49ca45e9f0a6962c" class="m-doc">tf::<wbr/>Executor::<wbr/>make_observer</a>. We use <ahref="http://en.cppreference.com/w/cpp/memory/shared_ptr.html" class="m-doc-external">std::<wbr/>shared_ptr</a> to manage the ownership of an observer. The executor loops through each observer and invoke the corresponding methods accordingly.</p><preclass="m-code"><spanclass="cp">#include</span><spanclass="w"></span><spanclass="cpf"><taskflow/taskflow.hpp></span>
<spanclass="w"></span><spanclass="n">std</span><spanclass="o">::</span><spanclass="n">cout</span><spanclass="w"></span><spanclass="o"><<</span><spanclass="w"></span><spanclass="s">"setting up observer with "</span><spanclass="w"></span><spanclass="o"><<</span><spanclass="w"></span><spanclass="n">num_workers</span><spanclass="w"></span><spanclass="o"><<</span><spanclass="w"></span><spanclass="s">" workers</span><spanclass="se">\n</span><spanclass="s">"</span><spanclass="p">;</span>
<spanclass="w"></span><spanclass="n">oss</span><spanclass="w"></span><spanclass="o"><<</span><spanclass="w"></span><spanclass="s">"worker "</span><spanclass="w"></span><spanclass="o"><<</span><spanclass="w"></span><spanclass="n">w</span><spanclass="p">.</span><spanclass="n">id</span><spanclass="p">()</span><spanclass="w"></span><spanclass="o"><<</span><spanclass="w"></span><spanclass="s">" ready to run "</span><spanclass="w"></span><spanclass="o"><<</span><spanclass="w"></span><spanclass="n">tv</span><spanclass="p">.</span><spanclass="n">name</span><spanclass="p">()</span><spanclass="w"></span><spanclass="o"><<</span><spanclass="w"></span><spanclass="sc">'\n'</span><spanclass="p">;</span>
<spanclass="p">}</span></pre><p>The above code produces the following output:</p><preclass="m-code">constructing observer MyObserver
setting up observer with <spanclass="m">4</span> workers
worker <spanclass="m">2</span> ready to run A
<spanclass="m">1</span>
worker <spanclass="m">2</span> finished running A
worker <spanclass="m">2</span> ready to run B
<spanclass="m">2</span>
worker <spanclass="m">1</span> ready to run C
worker <spanclass="m">2</span> finished running B
<spanclass="m">3</span>
worker <spanclass="m">2</span> ready to run D
worker <spanclass="m">3</span> ready to run E
worker <spanclass="m">1</span> finished running C
<spanclass="m">4</span>
<spanclass="m">5</span>
worker <spanclass="m">1</span> ready to run F
worker <spanclass="m">2</span> finished running D
worker <spanclass="m">3</span> finished running E
<spanclass="m">6</span>
worker <spanclass="m">2</span> ready to run G
worker <spanclass="m">3</span> ready to run H
worker <spanclass="m">1</span> finished running F
<spanclass="m">7</span>
<spanclass="m">8</span>
worker <spanclass="m">2</span> finished running G
worker <spanclass="m">3</span> finished running H</pre><p>It is expected each line of <ahref="http://en.cppreference.com/w/cpp/io/basic_ostream.html" class="m-doc-external">std::<wbr/>cout</a> interleaves with each other as there are four workers participating in task scheduling. However, the <em>ready</em> message always appears before the corresponding task message (e.g., numbers) and then the <em>finished</em> message.</p></section><sectionid="ModifyWorkerProperty"><h2><ahref="#ModifyWorkerProperty">Modify Worker Property</a></h2><p>You can change the property of each worker thread from its executor, such as assigning thread-processor affinity before the worker enters the scheduler loop and post-processing additional information after the worker leaves the scheduler loop, by passing an instance derived from <ahref="classtf_1_1WorkerInterface.html" class="m-doc">tf::<wbr/>WorkerInterface</a> to the executor. The example demonstrates the usage of <ahref="classtf_1_1WorkerInterface.html" class="m-doc">tf::<wbr/>WorkerInterface</a> to affine a worker to a specific CPU core equal to its id on a linux platform:</p><preclass="m-code"><spanclass="c1">// affine the given thread to the given core index (linux-specific)</span>
<spanclass="w"></span><spanclass="n">printf</span><spanclass="p">(</span><spanclass="s">"worker %lu prepares to enter the work-stealing loop</span><spanclass="se">\n</span><spanclass="s">"</span><spanclass="p">,</span><spanclass="w"></span><spanclass="n">w</span><spanclass="p">.</span><spanclass="n">id</span><spanclass="p">());</span>
<spanclass="w"></span>
<spanclass="w"></span><spanclass="c1">// now affine the worker to a particular CPU core equal to its id</span>
<spanclass="w"></span><spanclass="n">printf</span><spanclass="p">(</span><spanclass="s">"successfully affines worker %lu to CPU core %lu</span><spanclass="se">\n</span><spanclass="s">"</span><spanclass="p">,</span><spanclass="w"></span><spanclass="n">w</span><spanclass="p">.</span><spanclass="n">id</span><spanclass="p">(),</span><spanclass="w"></span><spanclass="n">w</span><spanclass="p">.</span><spanclass="n">id</span><spanclass="p">());</span>
<spanclass="w"></span><spanclass="n">printf</span><spanclass="p">(</span><spanclass="s">"failed to affine worker %lu to CPU core %lu</span><spanclass="se">\n</span><spanclass="s">"</span><spanclass="p">,</span><spanclass="w"></span><spanclass="n">w</span><spanclass="p">.</span><spanclass="n">id</span><spanclass="p">(),</span><spanclass="w"></span><spanclass="n">w</span><spanclass="p">.</span><spanclass="n">id</span><spanclass="p">());</span>
<spanclass="w"></span><spanclass="p">}</span>
<spanclass="w"></span><spanclass="p">}</span>
<spanclass="w"></span><spanclass="c1">// to call after the worker leaves the scheduling loop</span>
<spanclass="w"></span><spanclass="n">printf</span><spanclass="p">(</span><spanclass="s">"worker %lu left the work-stealing loop</span><spanclass="se">\n</span><spanclass="s">"</span><spanclass="p">,</span><spanclass="w"></span><spanclass="n">w</span><spanclass="p">.</span><spanclass="n">id</span><spanclass="p">());</span>
<spanclass="p">}</span></pre><p>When running the program, we see the following one possible output:</p><preclass="m-console"><spanclass="go">worker 3 prepares to enter the work-stealing loop</span>
<spanclass="go">successfully affines worker 3 to CPU core 3</span>
<spanclass="go">worker 3 left the work-stealing loop</span>
<spanclass="go">worker 0 prepares to enter the work-stealing loop</span>
<spanclass="go">successfully affines worker 0 to CPU core 0</span>
<spanclass="go">worker 0 left the work-stealing loop</span>
<spanclass="go">worker 1 prepares to enter the work-stealing loop</span>
<spanclass="go">worker 2 prepares to enter the work-stealing loop</span>
<spanclass="go">successfully affines worker 1 to CPU core 1</span>
<spanclass="go">worker 1 left the work-stealing loop</span>
<spanclass="go">successfully affines worker 2 to CPU core 2</span>
<spanclass="go">worker 2 left the work-stealing loop</span></pre><p>When you create an executor, it spawns a set of worker threads to run tasks using a work-stealing scheduling algorithm. The execution logic of the scheduler and its interaction with each spawned worker via <ahref="classtf_1_1WorkerInterface.html" class="m-doc">tf::<wbr/>WorkerInterface</a> is given below:</p><preclass="m-code"><spanclass="k">for</span><spanclass="p">(</span><spanclass="kt">size_t</span><spanclass="w"></span><spanclass="n">n</span><spanclass="o">=</span><spanclass="mi">0</span><spanclass="p">;</span><spanclass="w"></span><spanclass="n">n</span><spanclass="o"><</span><spanclass="n">num_workers</span><spanclass="p">;</span><spanclass="w"></span><spanclass="n">n</span><spanclass="o">++</span><spanclass="p">)</span><spanclass="w"></span><spanclass="p">{</span>
<spanclass="p">}</span></pre><asideclass="m-note m-warning"><h4>Attention</h4><p><ahref="classtf_1_1WorkerInterface.html#a41c3b931a36bde8eff4aa8d375e8888a" class="m-doc">tf::<wbr/>WorkerInterface::<wbr/>scheduler_prologue</a> and tf::WorkerInterface::scheduler_eiplogue are invoked by each worker simultaneously.</p></aside></section>
</div>
</div>
</div>
</article></main>
<divclass="m-doc-search" id="search">
<ahref="#!" onclick="return hideSearch()"></a>
<divclass="m-container">
<divclass="m-row">
<divclass="m-col-m-8 m-push-m-2">
<divclass="m-doc-search-header m-text m-small">
<div><spanclass="m-label m-default">Tab</span> / <spanclass="m-label m-default">T</span> to search, <spanclass="m-label m-default">Esc</span> to close</div>