-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathcorun.cpp
More file actions
31 lines (23 loc) · 708 Bytes
/
Copy pathcorun.cpp
File metadata and controls
31 lines (23 loc) · 708 Bytes
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
// This example demonstrates how to use the corun method from a running worker
// of an executor to avoid deadlock.
#include <taskflow/taskflow.hpp>
int main(){
const size_t N = 100;
const size_t T = 1000;
// create an executor and a taskflow
tf::Executor executor(2);
tf::Taskflow taskflow;
std::array<tf::Taskflow, N> taskflows;
std::atomic<size_t> counter{0};
for(size_t n=0; n<N; n++) {
for(size_t i=0; i<T; i++) {
taskflows[n].emplace([&](){ counter++; });
}
taskflow.emplace([&executor, &tf=taskflows[n]](){
executor.corun(tf);
//executor.run(tf).wait(); <-- can result in deadlock
});
}
executor.run(taskflow).wait();
return 0;
}