2401C++, core concepts of stackless coroutines

无栈协程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++20the 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++20coroutines 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 Athe coroutine wants to be Bin a coroutine 取数据, it can be implemented 直接by 协待 其他协程, 非常方便.

Guess you like

Origin blog.csdn.net/fqbqrr/article/details/135419428