无栈协程
The essence is individual 可多次执行
. 状态机
What is generally used 协程
is 标准流程
to generate a handle 状态机
that returns the state machine and execute it repeatedly (协柄)
through control .该句柄
(恢复)
执行
In the process, 该状态机
the function is: stop 产生数据
and write 某个地方
. Therefore, c++20
the coroutine provides 协产
and 协中
, at the same time, there should be an object that 每个协程
implements 产生值
or 中值
function .承诺类型
承诺
At the same time, every time after 数据
writing 承诺类型
, 协程
there will be an 暂停
opportunity to easily 外部
remove the data.
So it 协产 式
is not equivalent to 承诺.产生值(式)
, but equivalent to 协待 承诺.产生值(式)
, 协待
is used 处理暂停
for.
Similar, 协中 式
it is not equivalent to 承诺.中值(式)
, but equivalent to 承诺.中值(式)
after, then 协待 承诺.终挂起()
(there is a middle 析构过程
).
So, 协待
how to deal with it 暂停
? Generally 协待 式
speaking, 式
it is one 等待器
(it can also be converted through some prescribed functions 等待器
), which 等待器
implements ,直接协
the sum of two functions 挂起协
.
直接协
Define whether it is really necessary 暂停协程
, 挂起协
and define what 协程
to continue after the pause 执行
. It can be back to another coroutine, 调用者
or it can also execute the current coroutine.启动
继续
When going to a coroutine by 挂起协
returning 句柄
to 启动
the coroutine, the current state 调用者
remains unchanged. It will
only change if it is explicitly called 恢复
or 创建协程
executed directly 调用者
.
At this point, the framework is generally completed 协程
. However, there is 很重要
one more thing. The function
of the coroutine 最重要
is 不断
to write data to 承诺
the object. But, how to get the data 承诺
out of the object?
The simplest thing is to take it easy. For example, if you 数据
write it in, it is enough to get it 承诺.值
directly .承诺.值
However, c++20
coroutines also provide additional mechanisms that help simplify the process of 承诺
retrieving objects :数据
(恢复协)
The standard stipulates that 协待 等待器
at that time , functions 等待器
will also be implemented , that is, the entire function .恢复协
恢复协
返回值
协待 等待器
返回值
Generally, 定义
when a coroutine is in use, there will also be a conversion 定义
from 协程
to 等待器
, so if A
the coroutine wants to be B
in a coroutine 取数据
, it can be implemented 直接
by 协待 其他协程
, 非常方便
.