-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.cpp
140 lines (118 loc) · 4.03 KB
/
test.cpp
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <chrono>
#include <iostream>
#include <string>
#include "weejobs.h"
static std::mutex print_lock;
WEEJOBS_INSTANCE;
using namespace std::chrono_literals;
void atomic_print(const std::string& str)
{
std::lock_guard<std::mutex> L(print_lock);
std::cout << str << std::endl;
}
int main(int argc, char** argv)
{
jobs::get_pool()->set_concurrency(4);
// fire and forget - no args, no return value
auto fire_and_forget = []()
{
atomic_print("Running fire and forget job");
};
jobs::dispatch(fire_and_forget);
// future result - return value, cancelable& required
auto get_future_result = [](jobs::cancelable& c)
{
atomic_print("Running future result job");
return 42;
};
auto future_result = jobs::dispatch(get_future_result);
// This should generate a WARNING in C++17 and above because of the
// [[nodiscard]] attribute.
jobs::dispatch([](jobs::cancelable&)
{
atomic_print("Job will not run because future was discarded.");
return 42;
});
// Join
auto result = future_result.join();
atomic_print("Future result = " + std::to_string(result));
// Chaining
auto chain_job1 = [](jobs::cancelable& c)
{
atomic_print("Running chain job 1");
return 42;
};
auto chain_job2 = [](const int& i, jobs::cancelable& c)
{
atomic_print("Running chain job 2");
return i * 2;
};
auto chain_job3 = [](const int& i)
{
int result = i * 2;
atomic_print("Running chain job 3 (fire and forget), result = " + std::to_string(result));
};
auto chain1 = jobs::dispatch(chain_job1);
auto chain2 = chain1.then_dispatch(chain_job2);
chain2.then_dispatch(chain_job3);
atomic_print("Chain result = " + std::to_string(chain2.join()));
// Cancelation
auto cancelable_task = [](jobs::cancelable& c)
{
if (c.canceled())
atomic_print("CANCELED Cancelable task");
else
atomic_print("Running cancelable task");
return 42;
};
auto cancelable_result = jobs::dispatch(cancelable_task);
if (cancelable_result.canceled())
atomic_print("Canceleable result = CANCELED");
else
atomic_print("Canceleable Result = " + std::to_string(cancelable_result.join()));
// User promise
auto user_promise_job = [](jobs::promise<int>& promise)
{
promise.resolve(66);
};
jobs::promise<int> my_promise;
auto user_promise_result = jobs::dispatch(user_promise_job, my_promise);
atomic_print("User promise result = " + std::to_string(user_promise_result.join()));
// Job pools
jobs::context context;
context.pool = jobs::get_pool("custom pool");
jobs::dispatch([]() {
atomic_print("Running in custom pool");
}, context);
// Priority
context.priority = []() { return 10.0f; };
jobs::dispatch([]() {
atomic_print("Running with priority 10");
}, context);
// Auto-cancelation off
context.can_cancel = false;
auto non_cancelable_task = [](jobs::cancelable& c)
{
if (c.canceled())
atomic_print("CANCELED Non-cancelable task");
else
atomic_print("Running non-cancelable task");
return 13;
};
auto non_cancelable_result = jobs::dispatch(non_cancelable_task, context);
// Grouping
context.group = jobs::jobgroup::create();
jobs::dispatch([]() {
atomic_print("Running group job 1");
}, context);
jobs::dispatch([]() {
atomic_print("Running group job 2");
}, context);
jobs::dispatch([]() {
atomic_print("Running group job 3");
}, context);
context.group->join(); // wait for all 3 to finish
atomic_print("All group jobs finished");
// At exit, queued jobs will be discarded and running jobs will be joined.
return 0;
}