forked from taskflow/taskflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriority.cpp
More file actions
55 lines (44 loc) · 1.54 KB
/
priority.cpp
File metadata and controls
55 lines (44 loc) · 1.54 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// This program demonstrates how to set priority to a task.
//
// Currently, Taskflow supports only three priority levels:
// + tf::TaskPriority::HIGH (numerical value = 0)
// + tf::TaskPriority::NORMAL (numerical value = 1)
// + tf::TaskPriority::LOW (numerical value = 2)
//
// Priority-based execution is non-preemptive. Once a task
// has started to execute, it will execute to completion,
// even if a higher priority task has been spawned or enqueued.
#include <taskflow/taskflow.hpp>
int main() {
// create an executor of only one worker to enable
// deterministic behavior
tf::Executor executor(1);
tf::Taskflow taskflow;
int counter {0};
// Here we create five tasks and print thier execution
// orders which should align with assigned priorities
auto [A, B, C, D, E] = taskflow.emplace(
[] () { },
[&] () {
std::cout << "Task B: " << counter++ << '\n'; // 0
},
[&] () {
std::cout << "Task C: " << counter++ << '\n'; // 2
},
[&] () {
std::cout << "Task D: " << counter++ << '\n'; // 1
},
[] () { }
);
A.precede(B, C, D);
E.succeed(B, C, D);
// By default, all tasks are of tf::TaskPriority::HIGH
B.priority(tf::TaskPriority::HIGH);
C.priority(tf::TaskPriority::LOW);
D.priority(tf::TaskPriority::NORMAL);
assert(B.priority() == tf::TaskPriority::HIGH);
assert(C.priority() == tf::TaskPriority::LOW);
assert(D.priority() == tf::TaskPriority::NORMAL);
// we should see B, D, and C in their priority order
executor.run(taskflow).wait();
}