forked from microsoft/vscode-remote-try-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoro_test.cpp
71 lines (45 loc) · 1.66 KB
/
coro_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
// cppcoroTask2.cpp
#include <chrono>
#include <iostream>
#include <string>
#include <thread>
#include <vector>
#include <map>
// #include <boost/algorithm/string.hpp>
#include <cppcoro/sync_wait.hpp>
#include <cppcoro/task.hpp>
using std::chrono::high_resolution_clock;
using std::chrono::time_point;
using std::chrono::duration;
using namespace std::chrono_literals;
auto getTimeSince(const time_point<::high_resolution_clock>& start) {
auto end = high_resolution_clock::now();
duration<double> elapsed = end - start;
return elapsed.count();
}
cppcoro::task<> third(const time_point<high_resolution_clock>& start) {
std::cout << "Third waited " << getTimeSince(start) << " seconds." << std::endl;
std::this_thread::sleep_for(1s);
co_return;
}
cppcoro::task<> second(const time_point<high_resolution_clock>& start) {
auto thi = third(start);
co_await thi;
std::vector<int> blah = { 1,2,3,4,5 };
std::map<int, std::string> blub = { {1, "hello"}, {2, "world"}};
std::cout << "Second waited " << getTimeSince(start) << " seconds." << std::endl;
std::this_thread::sleep_for(1s);
}
cppcoro::task<> first(const time_point<high_resolution_clock>& start) {
auto sec = second(start);
co_await sec;
std::cout << "First waited " << getTimeSince(start) << " seconds." << std::endl;
std::this_thread::sleep_for(1s);
}
int main() {
std::cout << std::endl;
auto start = ::high_resolution_clock::now();
cppcoro::sync_wait(first(start));
std::cout << "Main waited " << getTimeSince(start) << " seconds." << std::endl;
std::cout << std::endl;
}