FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
taskflow/examples/observer.cpp at master · 521hellogithub/taskflow · GitHub
521hellogithub
/
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
taskflow
/
examples
/
observer.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
60 lines (44 loc) · 1.87 KB
Breadcrumbs
taskflow
/
examples
/
observer.cpp
Copy path
File metadata and controls
60 lines (44 loc) · 1.87 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
56
57
58
59
//
Demonstrates the use of observer to monitor worker activities.
#
include
<
taskflow/taskflow.hpp
>
struct
MyObserver
:
public
tf
::ObserverInterface {
MyObserver
(
const
std::string& name) {
std::cout <<
"
constructing observer
"
<< name <<
'
\n
'
;
}
//
set_up is a constructor-like method that will be called exactly once
//
passing the number of workers
void
set_up
(
size_t
num_workers)
override
final
{
std::cout <<
"
setting up observer with
"
<< num_workers <<
"
workers
\n
"
;
}
//
on_entry will be called before a worker runs a task
void
on_entry
(tf::WorkerView wv, tf::TaskView tv)
override
final
{
std::ostringstream oss;
oss <<
"
worker
"
<< wv.
id
() <<
"
ready to run
"
<< tv.
name
() <<
'
\n
'
;
std::cout << oss.
str
();
}
//
on_exit will be called after a worker completes a task
void
on_exit
(tf::WorkerView wv, tf::TaskView tv)
override
final
{
std::ostringstream oss;
oss <<
"
worker
"
<< wv.
id
() <<
"
finished running
"
<< tv.
name
() <<
'
\n
'
;
std::cout << oss.
str
();
}
};
int
main
(){
tf::Executor executor;
//
Create a taskflow of eight tasks
tf::Taskflow taskflow;
taskflow.
emplace
([] () { std::cout <<
"
1
\n
"
; }).
name
(
"
A
"
);
taskflow.
emplace
([] () { std::cout <<
"
2
\n
"
; }).
name
(
"
B
"
);
taskflow.
emplace
([] () { std::cout <<
"
3
\n
"
; }).
name
(
"
C
"
);
taskflow.
emplace
([] () { std::cout <<
"
4
\n
"
; }).
name
(
"
D
"
);
taskflow.
emplace
([] () { std::cout <<
"
5
\n
"
; }).
name
(
"
E
"
);
taskflow.
emplace
([] () { std::cout <<
"
6
\n
"
; }).
name
(
"
F
"
);
taskflow.
emplace
([] () { std::cout <<
"
7
\n
"
; }).
name
(
"
G
"
);
taskflow.
emplace
([] () { std::cout <<
"
8
\n
"
; }).
name
(
"
H
"
);
//
create a default observer
std::shared_ptr<MyObserver> observer = executor.
make_observer
<MyObserver>(
"
MyObserver
"
);
//
run the taskflow
executor.
run
(taskflow).
get
();
//
remove the observer (optional)
executor.
remove_observer
(
std::move
(observer));
return
0
;
}
Back
|
FazBrowse Home
|
New Git URL