-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathex015-async_queue.cpp
90 lines (68 loc) · 2.55 KB
/
ex015-async_queue.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
#include <iostream>
#include <syncstream>
#include "coke/future.h"
#include "coke/queue.h"
#include "coke/sleep.h"
#include "coke/wait.h"
/**
* This example uses asynchronous queue to distribute tasks to workers.
*/
std::string current();
coke::Task<> do_work(int id, coke::Queue<int> &que) {
constexpr auto work_cost = std::chrono::milliseconds(500);
std::osyncstream os(std::cout);
int ret, data;
std::emit_on_flush(os);
while (true) {
// Pop element from que one by one until que is empty and closed
ret = co_await que.pop(data);
if (ret == coke::TOP_CLOSED)
break;
os << current() << "Worker " << id << " pop " << data << std::endl;
// Use coke::sleep to simulate that we are working so hard with this
// element
co_await coke::sleep(work_cost);
}
os << current() << "Worker " << id << " exit" << std::endl;
}
coke::Task<> start_work(std::chrono::milliseconds interval) {
std::osyncstream os(std::cout);
std::vector<coke::Future<void>> workers;
coke::Queue<int> que(2);
std::emit_on_flush(os);
// Start two workers to pop elements from que
for (int i = 0; i < 2; i++)
workers.emplace_back(coke::create_future(do_work(i, que)));
// Push an element into que for each `interval` time
for (int i = 0; i < 8; i++) {
if (i != 0)
co_await coke::sleep(interval);
os << current() << "Push " << i << std::endl;
// If try push failed, the que if full, fallback to async push
if (!que.try_push(i)) {
os << current() << "Queue full, use async push" << std::endl;
co_await que.push(i);
}
os << current() << "Push success " << i << std::endl;
}
que.close();
os << current() << "Queue closed" << std::endl;
co_await coke::wait_futures(workers, workers.size());
os << current() << "All worker done" << std::endl;
}
int main() {
std::cout << "Example 1: Push faster than work" << std::endl;
auto interval1 = std::chrono::milliseconds(100);
coke::sync_wait(start_work(interval1));
std::cout << std::string(80, '-') << std::endl;
std::cout << "Example 2: Push slower than work" << std::endl;
auto interval2 = std::chrono::milliseconds(300);
coke::sync_wait(start_work(interval2));
return 0;
}
std::string current() {
static auto start = std::chrono::steady_clock::now();
auto now = std::chrono::steady_clock::now();
std::chrono::duration<double> d = now - start;
return "[" + std::to_string(d.count()) + "s] ";
}