FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cpp-taskflow/examples/async.cpp at master · feiyunwill/cpp-taskflow · GitHub
feiyunwill
/
cpp-taskflow
Public
forked from
taskflow/taskflow
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cpp-taskflow
/
examples
/
async.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
58 lines (42 loc) · 1.5 KB
Breadcrumbs
cpp-taskflow
/
examples
/
async.cpp
Copy path
File metadata and controls
58 lines (42 loc) · 1.5 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
//
The program demonstrates how to create asynchronous task
//
from an executor and a subflow.
#
include
<
taskflow/taskflow.hpp
>
int
main
() {
tf::Executor executor;
//
create asynchronous tasks from the executor
//
(using executor as a thread pool)
std::future<
int
> fu1 = executor.
async
(
"
async1
"
, [](){
std::cout <<
"
async task returns 1
\n
"
;
return
1
;
});
executor.
silent_async
(
"
async2
"
, [](){
//
silent async task doesn't return any future object
std::cout <<
"
silent async does not return
\n
"
;
});
//
create async tasks with runtime
std::future<
void
> fu2 = executor.
async
([](tf::Runtime& rt){
printf
(
"
async task with a runtime: %p
\n
"
, &rt);
});
executor.
silent_async
([](tf::Runtime& rt){
printf
(
"
silent async task with a runtime: %p
\n
"
, &rt);
});
executor.
wait_for_all
();
//
wait for the two async tasks to finish
//
create asynchronous tasks from a subflow
//
all asynchronous tasks are guaranteed to finish when the subflow joins
tf::Taskflow taskflow;
std::atomic<
int
> counter {
0
};
taskflow.
emplace
([&](tf::Runtime& rt){
for
(
int
i=
0
; i<
100
; i++) {
rt.
silent_async
([&](){ counter.
fetch_add
(
1
, std::memory_order_relaxed); });
}
rt.
corun
();
//
when subflow joins, all spawned tasks from the subflow will finish
if
(counter ==
100
) {
std::cout <<
"
async tasks spawned from the runtime all finish
\n
"
;
}
else
{
throw
std::runtime_error
(
"
this should not happen
"
);
}
});
executor.
run
(taskflow).
wait
();
return
0
;
}
Back
|
FazBrowse Home
|
New Git URL