forked from taskflow/taskflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
composition.cpp
119 lines (95 loc) · 2.84 KB
/
composition.cpp
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
// 2019/03/12 - created by Chun-Xun Lin
// - added an example showing how to use framework decomposition
#include <taskflow/taskflow.hpp> // the only include you need
void composition_example_1() {
std::cout << "Composition example 1\n";
tf::Executor executor;
// f1 has three independent tasks
tf::Taskflow f1("F1");
auto [f1A, f1B, f1C] = f1.emplace(
[&](){ std::cout << "F1 TaskA\n"; },
[&](){ std::cout << "F1 TaskB\n"; },
[&](){ std::cout << "F1 TaskC\n"; }
);
f1A.name("f1A");
f1B.name("f1B");
f1C.name("f1C");
f1A.precede(f1C);
f1B.precede(f1C);
// f2A ---
// |----> f2C ----> f1_module_task ----> f2D
// f2B ---
tf::Taskflow f2("F2");
auto [f2A, f2B, f2C, f2D] = f2.emplace(
[&](){ std::cout << " F2 TaskA\n"; },
[&](){ std::cout << " F2 TaskB\n"; },
[&](){ std::cout << " F2 TaskC\n"; },
[&](){ std::cout << " F2 TaskD\n"; }
);
f2A.name("f2A");
f2B.name("f2B");
f2C.name("f2C");
f2D.name("f2D");
f2A.precede(f2C);
f2B.precede(f2C);
tf::Task f1_module_task = f2.composed_of(f1);
f1_module_task.name("module");
f2C.precede(f1_module_task);
f1_module_task.precede(f2D);
f2.dump(std::cout);
executor.run_n(f2, 3).get();
}
void composition_example_2() {
std::cout << "Composition example 2\n";
tf::Executor executor;
// f1 has two independent tasks
tf::Taskflow f1("F1");
auto [f1A, f1B] = f1.emplace(
[&](){ std::cout << "F1 TaskA\n"; },
[&](){ std::cout << "F1 TaskB\n"; }
);
f1A.name("f1A");
f1B.name("f1B");
// f2A ---
// |----> f2C
// f2B ---
//
// f1_module_task
tf::Taskflow f2("F2");
auto [f2A, f2B, f2C] = f2.emplace(
[&](){ std::cout << " F2 TaskA\n"; },
[&](){ std::cout << " F2 TaskB\n"; },
[&](){ std::cout << " F2 TaskC\n"; }
);
f2A.name("f2A");
f2B.name("f2B");
f2C.name("f2C");
f2A.precede(f2C);
f2B.precede(f2C);
f2.composed_of(f1);
// f3 has a module task (f2) and a regular task
tf::Taskflow f3("F3");
f3.composed_of(f2);
f3.emplace([](){ std::cout << " F3 TaskA\n"; }).name("f3A");
// f4: f3_module_task -> f2_module_task
tf::Taskflow f4;
f4.name("F4");
auto f3_module_task = f4.composed_of(f3);
auto f2_module_task = f4.composed_of(f2);
f3_module_task.precede(f2_module_task);
f4.dump(std::cout);
executor.run_until(f4, [iter = 1] () mutable { std::cout << '\n'; return iter-- == 0; }, [](){
std::cout << "First run_until finished\n";
}).get();
executor.run_until(f4, [iter = 2] () mutable { std::cout << '\n'; return iter-- == 0; }, [](){
std::cout << "Second run_until finished\n";
});
executor.run_until(f4, [iter = 3] () mutable { std::cout << '\n'; return iter-- == 0; }, [](){
std::cout << "Third run_until finished\n";
}).get();
}
int main(){
composition_example_1();
composition_example_2();
return 0;
}