-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
289 lines (198 loc) · 7.32 KB
/
Copy pathmain.cpp
File metadata and controls
289 lines (198 loc) · 7.32 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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include <weave/executors/manual.hpp>
#include <weave/executors/strand.hpp>
#include <weave/executors/submit.hpp>
#include <weave/executors/thread_pool.hpp>
// Futures API example
#include <weave/futures/make/submit.hpp>
#include <weave/futures/run/await.hpp>
// Fiber pool example
#include <weave/executors/fibers/manual.hpp>
#include <weave/fibers/core/self.hpp>
#include <weave/fibers/sched/yield.hpp>
#include <fmt/core.h>
#include <string>
using namespace std::chrono_literals;
using namespace weave; // NOLINT
//////////////////////////////////////////////////////////////////////
/*
Executor is an object which executes tasks.
Public API allows these tasks to be lambdas exclusively.
There are two ways of submitting tasks:
executors::Submit and futures::Submit.
While the latter is preferred, we will take a look at both of them
*/
//////////////////////////////////////////////////////////////////////
void ThreadPoolExample(){
fmt::println("ThreadPool Example");
// compute::ThreadPool -- simple pool of worker threads
// which have shared unbounded blocking queue
// Best fit for cpu bound tasks
executors::tp::compute::ThreadPool pool{/*threads=*/ 4};
// Pool needs to be started explicitly
pool.Start();
static const size_t kIncrements = 100500;
std::atomic<uint64_t> count{0};
// Submit lambdas which perform a non-atomic increment
// They will be performed concurrently by 4 threads
for(size_t i = 0; i < kIncrements; i++){
executors::Submit(pool, [&]{
count.store(count.load() + 1);
});
}
// Wait for pool to finish all the work
pool.WaitIdle();
fmt::println("Count is {}, expected is {}", count.load(), kIncrements);
// ~= std::thread::join() but for pool
pool.Stop();
}
//////////////////////////////////////////////////////////////////////
void FuturesSubmitExample(){
fmt::println("Submit example");
executors::tp::compute::ThreadPool pool{/*threads=*/ 4};
pool.Start();
// The most efficient way of using executors is via futures API
// You can submit a lambda using futures::Submit function
auto future = futures::Submit(pool, []{
fmt::println("Task is running...");
return 42;
});
// weave futures are lazy by default.
// This means, that the task is not submitted to the pool right here
pool.WaitIdle(); // <- Returns control instantly
// To submit a task and also retrieve the value from future
// One can use futures::Await
auto result = std::move(future) | futures::Await();
// At this point task is not only submitted but is also finished
fmt::println("Value is {}", *result);
pool.Stop();
}
//////////////////////////////////////////////////////////////////////
// This example is intentionally broken
void LifeTimeExample(){
fmt::println("Lifetime example");
executors::tp::compute::ThreadPool pool{/*threads=*/ 4};
pool.Start();
// futures::Submit as well as executors::Submit transfers lambda's
// ownership to the executor. However, if lambda has some non-owning references,
// executor will not obtain ownership over the referred object
{
std::string hello{"Hello!"};
executors::Submit(pool, [&hello]{
// Stall the executor
std::this_thread::sleep_for(1s);
// Heap-use-after-scope right here
fmt::println("String is \"{}\"", hello);
});
} // <- hello is destroyed
pool.WaitIdle();
pool.Stop();
}
//////////////////////////////////////////////////////////////////////
void StrandExample(){
fmt::println("Strand example");
executors::tp::compute::ThreadPool pool{/*threads=*/ 4};
pool.Start();
// Strand is an executor which guarantees that tasks submitted into it
// will be executed in a sequence with happens-before ordering
// Strand is not a standalone executor
// Instead Strand is a decorator over the normal executor
executors::Strand strand{pool};
static const size_t kConcurrentStrandUsers = 5;
static const size_t kIncrements = 100500;
size_t counter{0};
// We shall submit tasks into the strand from thread_pool workers
// This way tasks would be submitted concurrently
// And yet strand would execute them in a sequence
for(size_t i = 0; i < kConcurrentStrandUsers; i++){
executors::Submit(pool, [&]{
for(size_t j = 0; j < kIncrements; j++){
executors::Submit(strand, [&]{
// This increment is non-atomic
// However, it is performed inside the strand
// so every increment will be ordered
counter++;
});
}
});
}
pool.WaitIdle();
fmt::println("Increments {}, expected {}", counter, kConcurrentStrandUsers * kIncrements);
pool.Stop();
}
//////////////////////////////////////////////////////////////////////
void ManualExample(){
fmt::println("Manual example");
// ManualExecutor -- single thread executor which runs tasks
// on command
executors::ManualExecutor manual;
executors::Submit(manual, []{
fmt::println("Hello from manual!");
});
fmt::println("Currently manual has {} tasks", manual.TaskCount());
// We can launch tasks using RunNext, RunAtMost and Drain methods
// RunNext launches 1 task if there is one and returns true iff a task was ran
manual.RunNext(); // return true
// Let's add some more tasks to the manual
for(size_t i = 0; i < 15; i++){
executors::Submit(manual, [i]{
fmt::println("Running task #{}", i);
});
}
// RunAtMost(count) runs no more than count tasks as the name suggests
fmt::println("Ran tasks: {}", manual.RunAtMost(5)); // 5
// Drain runs tasks until the queue is empty
fmt::println("Ran tasks {}", manual.Drain()); // 10
// Drain accounts for tasks which submit tasks themselves
executors::Submit(manual, [&]{
executors::Submit(manual, []{
fmt::println("Hello!");
});
});
fmt::println("Ran tasks {}", manual.Drain()); // 2
}
//////////////////////////////////////////////////////////////////////
// Consider checking these examples after you are familiar with fiber's API
// examples/fibers
void FiberManualExample(){
// Fiber manual executor == manual which launches tasks in fibers context
executors::fibers::ManualExecutor manual;
executors::Submit(manual, []{
// We are in fibers context!
assert(fibers::IAmFiber());
for(size_t i = 0; i < 10; i++){
fibers::Yield();
}
});
fmt::println("Tasks ran: {}", manual.Drain()); // 11
// There is a difference in API
// fibers Manual has to be stopped manually
manual.Stop();
}
//////////////////////////////////////////////////////////////////////
void FiberThreadPoolExample() {
// ThreadPool also has a fiber version
// Currently fiber threadpool (actually scheduler)
// is the default version of threadpool form executors/thread_pool.hpp
executors::fibers::ThreadPool fiber_pool{/*threads=*/ 4};
fiber_pool.Start();
executors::Submit(fiber_pool, []{
assert(fibers::IAmFiber());
for(size_t i = 0; i < 25; i++){
// fiber version support workstealing thus iterations
// may be ran on different threads
fmt::println("Worker {} did step {}", executors::tp::fast::Worker::Current()->Index(), i);
fibers::Yield();
}
});
fiber_pool.WaitIdle();
fiber_pool.Stop();
}
int main() {
ThreadPoolExample();
FuturesSubmitExample();
//LifeTimeExample();
StrandExample();
ManualExample();
FiberManualExample();
FiberThreadPoolExample();
}