-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
73 lines (51 loc) · 1.47 KB
/
main.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
#include <iostream>
#include <future>
#include <experimental/generator>
#include <cassert>
#include <functional>
#include <queue>
#include <type_traits>
#include "ThreadExecutor.h"
#include "Future.h"
void printThreadID() {
std::cout << "Current thread ID = " << std::this_thread::get_id() << std::endl;
}
Task<int> test2(AllocatorPtr, std::future<void> baton) {
std::cout << "test2()" << std::endl;
printThreadID();
co_await baton;
std::cout << "test2(): done awaiting baton" << std::endl;
printThreadID();
co_return 24;
}
Task<int> test(AllocatorPtr, std::future<void> baton, const int& x) {
std::cout << "test()" << std::endl;
printThreadID();
std::cout << "passed by const&, should be 42 = " << x << std::endl;
auto value = co_await call(test2, std::move(baton));
assert(value == 24);
std::cout << "test(): done awaiting test2" << std::endl;
printThreadID();
co_return 42 + value;
}
int main() {
ThreadExecutor executor;
std::promise<void> baton;
std::cout << "main(): spawning test()" << std::endl;
printThreadID();
auto future = [&]() {
int x = 42;
return spawnWithStack(executor, 1024, test, baton.get_future(), x);
}();
std::this_thread::sleep_for(std::chrono::milliseconds{ 100 });
std::cout << "main(): posting baton" << std::endl;
printThreadID();
baton.set_value();
future.wait();
assert(future.await_ready());
assert(*future == 66);
std::cout << "main(): done awaiting test()" << std::endl;
printThreadID();
executor.join();
return 0;
}