forked from taskflow/taskflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcancel.cpp
More file actions
39 lines (28 loc) · 1.01 KB
/
cancel.cpp
File metadata and controls
39 lines (28 loc) · 1.01 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
// The program demonstrates how to cancel a submitted taskflow
// graph and wait until the cancellation completes.
#include <taskflow/taskflow.hpp>
int main() {
tf::Executor executor;
tf::Taskflow taskflow("cancel");
// We create a taskflow graph of 1000 tasks each of 1 second.
// Ideally, the taskflow completes in 1000/P seconds, where P
// is the number of workers.
for(int i=0; i<1000; i++) {
taskflow.emplace([](){
std::this_thread::sleep_for(std::chrono::seconds(1));
});
}
// submit the taskflow
auto beg = std::chrono::steady_clock::now();
tf::Future fu = executor.run(taskflow);
// submit a cancel request to cancel all 1000 tasks.
fu.cancel();
// wait until the cancellation finishes
fu.get();
auto end = std::chrono::steady_clock::now();
// the duration should be much less than 1000 seconds
std::cout << "taskflow completes in "
<< std::chrono::duration_cast<std::chrono::milliseconds>(end-beg).count()
<< " milliseconds\n";
return 0;
}