forked from taskflow/taskflow
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchapter1.html
More file actions
167 lines (165 loc) · 27.4 KB
/
chapter1.html
File metadata and controls
167 lines (165 loc) · 27.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<!-- HTML header for doxygen 1.8.13-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Cpp-Taskflow</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link rel="icon" type="image/x-icon" href="favicon.ico" />
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
$(document).ready(initResizable);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
$(document).ready(function() { init_search(); });
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<td id="projectalign" style="padding-left: 0.5em;">
<div id="projectname"><a href="https://github.com/cpp-taskflow/cpp-taskflow">Cpp-Taskflow</a>
 <span id="projectnumber">2.0.0</span>
</div>
</td>
<td> <div id="MSearchBox" class="MSearchBoxInactive">
<span class="left">
<img id="MSearchSelect" src="search/mag_sel.png"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
alt=""/>
<input type="text" id="MSearchField" value="Search" accesskey="S"
onfocus="searchBox.OnSearchFieldFocus(true)"
onblur="searchBox.OnSearchFieldFocus(false)"
onkeyup="searchBox.OnSearchFieldChange(event)"/>
</span><span class="right">
<a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
</span>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
<div id="nav-sync" class="sync"></div>
</div>
</div>
<div id="splitbar" style="-moz-user-select:none;"
class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('chapter1.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
<div class="header">
<div class="headertitle">
<div class="title">C1: Understand the Task </div> </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p>In this chapter, we demonstrate the basic construct of a task dependency graph - <a class="el" href="classtf_1_1Task.html" title="Handle to modify and access a task. ">tf::Task</a>.</p>
<h1><a class="anchor" id="WhatIsATask"></a>
What is a Task?</h1>
<p>A task in Cpp-Taskflow is a callable object for which the operation <a href="https://en.cppreference.com/w/cpp/utility/functional/invoke">std::invoke</a> is applicable. It can be either a functor, a lambda expression, a bind expression, or a class objects with <code>operator()</code> overloaded.</p>
<p>Cpp-Taskflow provides three methods, <a class="el" href="classtf_1_1FlowBuilder.html#acab0b4ac82260f47fdb36a3244ee3aaf" title="creates an empty task ">tf::Taskflow::placeholder</a>, <a class="el" href="classtf_1_1FlowBuilder.html#a7285613836c840e22b8511d447734c87" title="creates a task from a given callable object without access to the result ">tf::Taskflow::silent_emplace</a>, and <a class="el" href="classtf_1_1FlowBuilder.html#a468ddceae9c77728fbaef0f70d9fd11c" title="creates a task from a given callable object ">tf::Taskflow::emplace</a> to create a task.</p>
<div class="fragment"><div class="line">1: <span class="keyword">auto</span> A = taskflow.placeholder();</div><div class="line">2: <span class="keyword">auto</span> B = taskflow.silent_emplace([] () {});</div><div class="line">3: <span class="keyword">auto</span> [C, FuC] = taskflow.emplace([] () { <span class="keywordflow">return</span> 1; });</div></div><!-- fragment --><p>Debrief: </p><ul>
<li>Line 1 creates an empty task </li>
<li>Line 2 creates a task from a given callable object and returns a task handle </li>
<li>Line 3 creates a task from a given callable object and returns, in addition to a task handle, a <a class="elRef" doxygen="/home/twhuang/PhD/Code/cpp-taskflow/docs/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/thread/future.html">std::future</a> object to access the result</li>
</ul>
<p>Each time you create a task, including an empty one, the taskflow object adds a node to the present graph and returns a task handle of type <a class="el" href="classtf_1_1Task.html" title="Handle to modify and access a task. ">Task</a>. A task handle is a lightweight object that wraps up a particular node in a graph and provides a set of methods for you to assign different attributes to the task such as adding dependencies, naming, and assigning a new work.</p>
<div class="fragment"><div class="line"> 1: <a class="code" href="classtf_1_1BasicTaskflow.html">tf::Taskflow</a> taskflow;</div><div class="line"> 2: <span class="keyword">auto</span> A = taskflow.<a class="code" href="classtf_1_1FlowBuilder.html#a7285613836c840e22b8511d447734c87">silent_emplace</a>([] () { <a class="codeRef" doxygen="/home/twhuang/PhD/Code/cpp-taskflow/docs/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> << <span class="stringliteral">"create a task A\n"</span>; });</div><div class="line"> 3: <span class="keyword">auto</span> B = taskflow.<a class="code" href="classtf_1_1FlowBuilder.html#a7285613836c840e22b8511d447734c87">silent_emplace</a>([] () { <a class="codeRef" doxygen="/home/twhuang/PhD/Code/cpp-taskflow/docs/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> << <span class="stringliteral">"create a task B\n"</span>; });</div><div class="line"> 4:</div><div class="line"> 5: A.name(<span class="stringliteral">"TaskA"</span>);</div><div class="line"> 6: A.work([] () { <a class="codeRef" doxygen="/home/twhuang/PhD/Code/cpp-taskflow/docs/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> << <span class="stringliteral">"reassign A to a new task\n"</span>; });</div><div class="line"> 7: A.precede(B);</div><div class="line"> 8:</div><div class="line"> 9: <a class="codeRef" doxygen="/home/twhuang/PhD/Code/cpp-taskflow/docs/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> << A.name() << <a class="codeRef" doxygen="/home/twhuang/PhD/Code/cpp-taskflow/docs/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a>; <span class="comment">// TaskA</span></div><div class="line">10: <a class="codeRef" doxygen="/home/twhuang/PhD/Code/cpp-taskflow/docs/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> << A.num_successors() << <a class="codeRef" doxygen="/home/twhuang/PhD/Code/cpp-taskflow/docs/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a>; <span class="comment">// 1</span></div><div class="line">11: <a class="codeRef" doxygen="/home/twhuang/PhD/Code/cpp-taskflow/docs/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> << A.num_dependents() << <a class="codeRef" doxygen="/home/twhuang/PhD/Code/cpp-taskflow/docs/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a>; <span class="comment">// 0</span></div><div class="line">12: </div><div class="line">13: <a class="codeRef" doxygen="/home/twhuang/PhD/Code/cpp-taskflow/docs/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> << B.num_successors() << <a class="codeRef" doxygen="/home/twhuang/PhD/Code/cpp-taskflow/docs/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a>; <span class="comment">// 0</span></div><div class="line">14: <a class="codeRef" doxygen="/home/twhuang/PhD/Code/cpp-taskflow/docs/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> << B.num_dependents() << <a class="codeRef" doxygen="/home/twhuang/PhD/Code/cpp-taskflow/docs/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a>; <span class="comment">// 1</span></div></div><!-- fragment --><p>Debrief: </p><ul>
<li>Line 1 creates a taskflow object </li>
<li>Line 2-3 creates two tasks A and B </li>
<li>Line 5-6 assigns a name and a work to task A, and add a precedence link to task B </li>
<li>Line 7 adds a dependency link from A to B </li>
<li>Line 9-14 dumps the task attributes</li>
</ul>
<p>Cpp-Taskflow uses the general-purpose polymorphic function wrapper <a class="elRef" doxygen="/home/twhuang/PhD/Code/cpp-taskflow/docs/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/utility/functional/function.html">std::function</a> to store and invoke any callable target in a task. You need to follow its contract to create a task. For instance, the callable object must be copy constructible.</p>
<h1><a class="anchor" id="AccessTheResultOfATask"></a>
Access the Result of a Task</h1>
<p>Unlike <a class="el" href="classtf_1_1FlowBuilder.html#a7285613836c840e22b8511d447734c87" title="creates a task from a given callable object without access to the result ">tf::Taskflow::silent_emplace</a>, the method <a class="el" href="classtf_1_1FlowBuilder.html#a468ddceae9c77728fbaef0f70d9fd11c" title="creates a task from a given callable object ">tf::Taskflow::emplace</a> returns a pair of a task handle and a <a class="elRef" doxygen="/home/twhuang/PhD/Code/cpp-taskflow/docs/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/thread/future.html">std::future</a> object to provide a mechanism to access the result when the associated task finishes. This is particularly useful when you would like to pass data between tasks.</p>
<div class="fragment"><div class="line"><span class="keyword">auto</span> [A, FuA] = taskflow.<a class="code" href="classtf_1_1FlowBuilder.html#a468ddceae9c77728fbaef0f70d9fd11c">emplace</a>([](){ <span class="keywordflow">return</span> 1; });</div><div class="line">taskflow.<a class="code" href="classtf_1_1BasicTaskflow.html#a37ef86998f23ee7315be032c40fe815e">wait_for_all</a>();</div><div class="line"><a class="codeRef" doxygen="/home/twhuang/PhD/Code/cpp-taskflow/docs/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> << FuA.get() << <a class="codeRef" doxygen="/home/twhuang/PhD/Code/cpp-taskflow/docs/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a>; <span class="comment">// 1</span></div></div><!-- fragment --><p>You should be aware that every time you add a task or a dependency, it creates only a node or an edge to the present graph. The execution does not start until you dispatch the graph. For example, the following code will block and never finish:</p>
<div class="fragment"><div class="line"><span class="keyword">auto</span> [A, FuA] = taskflow.<a class="code" href="classtf_1_1FlowBuilder.html#a468ddceae9c77728fbaef0f70d9fd11c">emplace</a>([](){ <span class="keywordflow">return</span> 1; });</div><div class="line"><a class="codeRef" doxygen="/home/twhuang/PhD/Code/cpp-taskflow/docs/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> << FuA.get() << <a class="codeRef" doxygen="/home/twhuang/PhD/Code/cpp-taskflow/docs/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a>; <span class="comment">// block</span></div><div class="line">taskflow.<a class="code" href="classtf_1_1BasicTaskflow.html#a37ef86998f23ee7315be032c40fe815e">wait_for_all</a>(); <span class="comment">// never enter this line</span></div></div><!-- fragment --><h1><a class="anchor" id="CreateMultipleTasksAtOneTime"></a>
Create Multiple Tasks at One Time</h1>
<p>Cpp-Taskflow uses C++ structured binding coupled with tuple to make the creation of tasks simple. Both <a class="el" href="classtf_1_1FlowBuilder.html#a7285613836c840e22b8511d447734c87" title="creates a task from a given callable object without access to the result ">tf::Taskflow::silent_emplace</a> and <a class="el" href="classtf_1_1FlowBuilder.html#a468ddceae9c77728fbaef0f70d9fd11c" title="creates a task from a given callable object ">tf::Taskflow::emplace</a> accept many callable objects to create multiple tasks at one time.</p>
<div class="fragment"><div class="line"><span class="keyword">auto</span> [A, B, C] = taskflow.<a class="code" href="classtf_1_1FlowBuilder.html#a7285613836c840e22b8511d447734c87">silent_emplace</a>( <span class="comment">// create three tasks in one call</span></div><div class="line"> [](){ <a class="codeRef" doxygen="/home/twhuang/PhD/Code/cpp-taskflow/docs/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> << <span class="stringliteral">"Task A\n"</span>; },</div><div class="line"> [](){ <a class="codeRef" doxygen="/home/twhuang/PhD/Code/cpp-taskflow/docs/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> << <span class="stringliteral">"Task B\n"</span>; },</div><div class="line"> [](){ <a class="codeRef" doxygen="/home/twhuang/PhD/Code/cpp-taskflow/docs/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> << <span class="stringliteral">"Task C\n"</span>; }</div><div class="line">);</div></div><!-- fragment --><h1><a class="anchor" id="LifetimeOfATask"></a>
Lifetime of A Task</h1>
<p>A task lives with its graph, and is not destroyed until its parent graph gets cleaned up. A task belongs to only a graph at a time. The lifetime of a task mostly refers to the user-given callable object, including captured values. As long as the graph is alive, all the associated tasks remain their existence. We recommend the users to read <a class="el" href="chapter2.html#LifeTimeOfAGraph">Lifetime of a Graph</a>.</p>
<h1><a class="anchor" id="C1Example1"></a>
Example 1: Create Multiple Dependency Graphs</h1>
<p>The example below demonstrates how to reuse task handles to create two task dependency graphs.</p>
<div class="fragment"><div class="line"> 1: #include <taskflow/taskflow.hpp></div><div class="line"> 2:</div><div class="line"> 3: <span class="keywordtype">int</span> main() {</div><div class="line"> 4: </div><div class="line"> 5: <a class="code" href="classtf_1_1BasicTaskflow.html">tf::Taskflow</a> taskflow;</div><div class="line"> 6:</div><div class="line"> 7: <span class="comment">// create a task dependency graph</span></div><div class="line"> 8: <a class="codeRef" doxygen="/home/twhuang/PhD/Code/cpp-taskflow/docs/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/container/array.html">std::array<tf::Task, 4></a> tasks {</div><div class="line"> 9: taskflow.<a class="code" href="classtf_1_1FlowBuilder.html#a7285613836c840e22b8511d447734c87">silent_emplace</a>([] () { <a class="codeRef" doxygen="/home/twhuang/PhD/Code/cpp-taskflow/docs/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> << <span class="stringliteral">"Task A\n"</span>; }),</div><div class="line">10: taskflow.<a class="code" href="classtf_1_1FlowBuilder.html#a7285613836c840e22b8511d447734c87">silent_emplace</a>([] () { std::cout << <span class="stringliteral">"Task B\n"</span>; }),</div><div class="line">11: taskflow.<a class="code" href="classtf_1_1FlowBuilder.html#a7285613836c840e22b8511d447734c87">silent_emplace</a>([] () { <a class="codeRef" doxygen="/home/twhuang/PhD/Code/cpp-taskflow/docs/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> << <span class="stringliteral">"Task C\n"</span>; }),</div><div class="line">12: taskflow.<a class="code" href="classtf_1_1FlowBuilder.html#a7285613836c840e22b8511d447734c87">silent_emplace</a>([] () { std::cout << <span class="stringliteral">"Task D\n"</span>; })</div><div class="line">13: };</div><div class="line">14:</div><div class="line">15: tasks[0].precede(tasks[1]); </div><div class="line">16: tasks[0].precede(tasks[2]);</div><div class="line">17: tasks[1].precede(tasks[3]);</div><div class="line">18: tasks[2].precede(tasks[3]);</div><div class="line">19:</div><div class="line">20: taskflow.<a class="code" href="classtf_1_1BasicTaskflow.html#a37ef86998f23ee7315be032c40fe815e">wait_for_all</a>();</div><div class="line">21:</div><div class="line">22: <span class="comment">// create another task dependency graph</span></div><div class="line">23: tasks = {</div><div class="line">24: taskflow.<a class="code" href="classtf_1_1FlowBuilder.html#a7285613836c840e22b8511d447734c87">silent_emplace</a>([] () { <a class="codeRef" doxygen="/home/twhuang/PhD/Code/cpp-taskflow/docs/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> << <span class="stringliteral">"New Task A\n"</span>; }),</div><div class="line">25: taskflow.<a class="code" href="classtf_1_1FlowBuilder.html#a7285613836c840e22b8511d447734c87">silent_emplace</a>([] () { std::cout << <span class="stringliteral">"New Task B\n"</span>; }),</div><div class="line">26: taskflow.<a class="code" href="classtf_1_1FlowBuilder.html#a7285613836c840e22b8511d447734c87">silent_emplace</a>([] () { <a class="codeRef" doxygen="/home/twhuang/PhD/Code/cpp-taskflow/docs/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> << <span class="stringliteral">"New Task C\n"</span>; }),</div><div class="line">27: taskflow.<a class="code" href="classtf_1_1FlowBuilder.html#a7285613836c840e22b8511d447734c87">silent_emplace</a>([] () { std::cout << <span class="stringliteral">"New Task D\n"</span>; })</div><div class="line">28: };</div><div class="line">29:</div><div class="line">30: tasks[3].precede(tasks[2]);</div><div class="line">31: tasks[2].precede(tasks[1]);</div><div class="line">32: tasks[1].precede(tasks[0]);</div><div class="line">33:</div><div class="line">34: taskflow.<a class="code" href="classtf_1_1BasicTaskflow.html#a37ef86998f23ee7315be032c40fe815e">wait_for_all</a>();</div><div class="line">35:</div><div class="line">36: <span class="keywordflow">return</span> 0;</div><div class="line">37: }</div></div><!-- fragment --><p>Debrief: </p><ul>
<li>Line 5 creates a taskflow object </li>
<li>Line 8 creates a task array to store four task handles </li>
<li>Line 9-12 creates four tasks </li>
<li>Line 15-18 adds four task dependency links </li>
<li>Line 20 dispatches the graph and blocks until it completes </li>
<li>Line 23-28 creates four new tasks and reassigns the task array to these four tasks </li>
<li>Line 30-32 adds a linear dependency to these four tasks </li>
<li>Line 34 dispatches the graph and blocks until it completes</li>
</ul>
<p>Notice that trying to modify a task in a dispatched graph results in undefined behavior. For examples, starting from Line 21, you should not modify any tasks but assign them to new targets (Line 23-28).</p>
<h1><a class="anchor" id="C1Example2"></a>
Example 2: Modify Task Attributes</h1>
<p>This example demonstrates how to modify a task's attributes using methods defined in the task handler.</p>
<div class="fragment"><div class="line"> 1: #include <taskflow/taskflow.hpp></div><div class="line"> 2:</div><div class="line"> 3: <span class="keywordtype">int</span> main() {</div><div class="line"> 4:</div><div class="line"> 5: <a class="code" href="classtf_1_1BasicTaskflow.html">tf::Taskflow</a> taskflow;</div><div class="line"> 6:</div><div class="line"> 7: <a class="codeRef" doxygen="/home/twhuang/PhD/Code/cpp-taskflow/docs/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector<tf::Task></a> tasks = { </div><div class="line"> 8: taskflow.<a class="code" href="classtf_1_1FlowBuilder.html#acab0b4ac82260f47fdb36a3244ee3aaf">placeholder</a>(), <span class="comment">// create a task with no work</span></div><div class="line"> 9: taskflow.<a class="code" href="classtf_1_1FlowBuilder.html#acab0b4ac82260f47fdb36a3244ee3aaf">placeholder</a>() <span class="comment">// create a task with no work</span></div><div class="line">10: };</div><div class="line">11:</div><div class="line">12: tasks[0].name(<span class="stringliteral">"This is Task 0"</span>);</div><div class="line">13: tasks[1].name(<span class="stringliteral">"This is Task 1"</span>);</div><div class="line">14: tasks[0].precede(tasks[1]);</div><div class="line">15:</div><div class="line">16: <span class="keywordflow">for</span>(<span class="keyword">auto</span> task : tasks) { <span class="comment">// print out each task's attributes</span></div><div class="line">17: <a class="codeRef" doxygen="/home/twhuang/PhD/Code/cpp-taskflow/docs/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> << task.name() << <span class="stringliteral">": "</span></div><div class="line">18: << <span class="stringliteral">"num_dependents="</span> << task.num_dependents() << <span class="stringliteral">", "</span></div><div class="line">19: << <span class="stringliteral">"num_successors="</span> << task.num_successors() << <span class="charliteral">'\n'</span>;</div><div class="line">20: }</div><div class="line">21:</div><div class="line">22: taskflow.<a class="code" href="classtf_1_1BasicTaskflow.html#adac448e1cc44307856b3116d7ed5490f">dump</a>(<a class="codeRef" doxygen="/home/twhuang/PhD/Code/cpp-taskflow/docs/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a>); <span class="comment">// dump the taskflow graph</span></div><div class="line">23:</div><div class="line">24: tasks[0].work([](){ <a class="codeRef" doxygen="/home/twhuang/PhD/Code/cpp-taskflow/docs/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> << <span class="stringliteral">"got a new work!\n"</span>; });</div><div class="line">25: tasks[1].work([](){ <a class="codeRef" doxygen="/home/twhuang/PhD/Code/cpp-taskflow/docs/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> << <span class="stringliteral">"got a new work!\n"</span>; });</div><div class="line">26:</div><div class="line">27: taskflow.<a class="code" href="classtf_1_1BasicTaskflow.html#a37ef86998f23ee7315be032c40fe815e">wait_for_all</a>();</div><div class="line">28:</div><div class="line">29: <span class="keywordflow">return</span> 0;</div><div class="line">30: }</div></div><!-- fragment --><p>The output of this program looks like the following:</p>
<div class="fragment"><div class="line">This is Task 0: num_dependents=0, num_successors=1</div><div class="line">This is Task 1: num_dependents=1, num_successors=0</div><div class="line">digraph Taskflow {</div><div class="line">"This is Task 1";</div><div class="line">"This is Task 0";</div><div class="line">"This is Task 0" -> "This is Task 1";</div><div class="line">}</div><div class="line">got a new work!</div><div class="line">got a new work!</div></div><!-- fragment --><p>Debrief: </p><ul>
<li>Line 5 creates a taskflow object </li>
<li>Line 7-10 creates two tasks with empty target and stores the corresponding task handles in a vector </li>
<li>Line 12-13 names the two tasks with human-readable strings </li>
<li>Line 14 adds a dependency link from the first task to the second task </li>
<li>Line 16-20 prints out the name of each task, the number of dependents, and the number of successors </li>
<li>Line 22 dumps the task dependency graph to a <a href="https://dreampuf.github.io/GraphvizOnline/">GraphViz Online</a> format (dot) </li>
<li>Line 24-25 assigns a new target to each task </li>
<li>Line 27 dispatches the graph and blocks until the execution finishes</li>
</ul>
<p>You can change the name and work of a task at anytime before dispatching the graph. The later assignment overwrites the previous values. Only the latest information will be used. </p>
</div></div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="footer">Generated by
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.13 </li>
</ul>
</div>
</body>
</html>