99// Do so is difference from "wait_for_all" which will clean up the
1010// finished graphs. After the graph finished, we dump the topology
1111// for inspection.
12+ //
13+ // Usage: ./subflow detach|join
14+ //
1215
1316#include < taskflow.hpp>
1417
15- int main () {
18+ const auto usage = " usage: ./subflow detach|join" ;
19+
20+ int main (int argc, char * argv[]) {
21+
22+ if (argc != 2 ) {
23+ std::cerr << usage << std::endl;
24+ std::exit (EXIT_FAILURE);
25+ }
1626
27+ std::string_view opt (argv[1 ]);
28+
29+ if (opt != " detach" && opt != " join" ) {
30+ std::cerr << usage << std::endl;
31+ std::exit (EXIT_FAILURE);
32+ }
33+
34+ auto detached = (opt == " detach" ) ? true : false ;
35+
36+ // Create a taskflow graph with three regular tasks and one subflow task.
1737 tf::Taskflow tf (std::thread::hardware_concurrency ());
1838
1939 auto [A, B, C, D] = tf.silent_emplace (
2040 // Task A
2141 [] () { std::cout << " TaskA\n " ; },
2242 // Task B
23- [cap=std::vector<int >{1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 }] (auto & subflow) {
43+ [cap=std::vector<int >{1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 }, detached ] (auto & subflow) {
2444
25- std::cout << " TaskB\n " ;
45+ std::cout << " TaskB is spawning B1, B2, and B3 ... \n " ;
2646
2747 auto B1 = subflow.silent_emplace ([&]() {
2848 printf (" Subtask B1: reduce sum = %d\n " ,
@@ -41,6 +61,9 @@ int main() {
4161
4262 B1.precede (B3);
4363 B2.precede (B3);
64+
65+ // detach or join the subflow (by default the subflow join at B)
66+ if (detached) subflow.detach ();
4467 },
4568 // Task C
4669 [] () { std::cout << " TaskC\n " ; },
@@ -60,8 +83,8 @@ int main() {
6083
6184 tf.dispatch ().get (); // block until finished
6285
63- // Now we can dump the topology
64- std::cout << tf.dump_topologies ();
86+ // examine the graph
87+ std::cout << ' \n ' << tf.dump_topologies ();
6588
6689 return 0 ;
6790}
0 commit comments