-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoRoutine.cpp
More file actions
122 lines (114 loc) · 2.43 KB
/
CoRoutine.cpp
File metadata and controls
122 lines (114 loc) · 2.43 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
// SET(CMAKE_CXX_FLAGS "-fcoroutines -std=c++2a")
#include <iostream>
#include <thread>
#include <coroutine>
#include <chrono>
#include <functional>
using call_back = std::function<void(int)>;
void Add100ByCallback(int init, call_back f) // 异步调用
{
std::thread t([init, f]()
{
std::this_thread::sleep_for(std::chrono::seconds(3));
f(init + 100);
});
t.detach();
}
struct Add100AWaitable
{
Add100AWaitable(int init)
:init_(init)
{
}
/**
* 返回 awaitable是否已经ready
* @return
*/
bool await_ready() const
{
return false; // 返回true说明结果已经得到 不需要执行协程了
}
/**
* 挂起 awaitable 通过handle.resume() 恢复协程
* @param handle
*/
void await_suspend(std::coroutine_handle<> handle)
{
auto f = [handle, this](int value) mutable
{
result_ = value;
handle.resume();
};
Add100ByCallback(init_, f); // 调用原来的异步调用
}
/**
* 协程恢复后 会调用此函数 返回结果即为co_wait的返回值
* @return
*/
int await_resume()
{
return result_;
}
int init_; // Add100ByCallback的参数
int result_; // Add100ByCallback的结果
};
/**
* 最简单的Promise规范的类型
*/
struct Task
{
struct promise_type
{
auto get_return_object()
{
return Task{};
}
auto initial_suspend()
{
return std::suspend_never{};
}
auto final_suspend()
{
return std::suspend_never{};
}
void unhandled_exception()
{
std::terminate();
}
void return_void()
{
}
};
};
/**
* 协程的入口函数 必须是在某个函数中. 函数的返回值需要满足Promise规范
* @return
*/
Task Add100ByCoroutine(int init, call_back f)
{
/**
* 协程可以解放异步函数的组织
* 否则多个异步回调实现同步 需要嵌套调用
* int ret = 0;
* Add100ByCallback(5, [&](int value)
* {
* ret = value;
* Add100ByCallback(ret, [&](int value)
* {
* ret += value;
* });
* });
*/
int ret = co_await Add100AWaitable(init); // 连续调用co_await
ret = co_await Add100AWaitable(ret); // 将多个异步调用转换为串行化的同步调用
f(ret);
}
int main()
{
Add100ByCallback(5, [](int value)
{ std::cout << "get result: " << value << "\n"; });
Add100ByCoroutine(10, [](int value) // 启动协程
{ std::cout << "get result from coroutine1: " << value << "\n"; });
Add100ByCoroutine(20, [](int value) // 启动协程
{ std::cout << "get result from coroutine2: " << value << "\n"; });
}