gpt4 book ai didi

coroutine - 非对称和对称协程有什么区别?

转载 作者:行者123 更新时间:2023-12-04 02:49:32 25 4
gpt4 key购买 nike

我注意到 Boost.Coroutine2 for C++ 中删除了对对称协程的支持,所以我想了解 - 两者之间有什么区别?

最佳答案

Ana Lúcia de Moura 和 Roberto Ierusalimschy 在他们的论文“Revisiting Coroutines”中特别详细地描述了对称和非对称协程之间的区别:

A well-known classification of coroutines concerns the control-transfer operations that are provided and distinguishes the concepts of symmetric and asymmetric coroutines. Symmetric coroutine facilities provide a single control-transfer operation that allows coroutines to explicitly pass control between themselves. Asymmetric coroutine mechanisms (more commonly denoted as semi-symmetric or semi coroutines) provide two control-transfer operations: one for invoking a coroutine and one for suspending it, the latter returning control to the coroutine invoker. While symmetric coroutines operate at the same hierarchical level, an asymmetric coroutine can be regarded as subordinate to its caller, the relationship between them being somewhat similar to that between a called and a calling routine.

Coroutine mechanisms to support concurrent programming usually provide symmetric coroutines to represent independent units of execution, like in Modula-2. On the other hand, coroutine mechanisms intended for implementing constructs that produce sequences of values typically provide asymmetric coroutines. Examples of this type of construct are iterators and generators.



(引文省略)

在非对称协程模型中,“不对称”是指协程之间存在类似堆栈的调用者-被调用者关系。一个协程既可以调用另一个协程,也可以通过将控制权交给它的调用者来挂起自己,通常同时也向调用者提供一个值。

在对称协程模型中,一个协程可以不受限制地将控制权交给任何其他协程。

这两个模型实际上具有相同的表达能力;即非对称协程可以使用对称协程来实现,反之亦然。 (参见 Symmetric coroutines,由 Giovanni P. Deretta 编写,了解两种协程之间的转换。)因此,Moura 和 Ierusalimschy 写道:“提供这两种结构只会使 [协程] 机制的语义复杂化,而没有增加它的表现力。”

Coroutine2 的开发人员决定不在库中提供对称协程,因为他们认为对称协程功能由 boost::context::execution_context 更好地实现。 (Boost.Context 的一部分): http://lists.boost.org/Archives/boost/2015/06/223701.php

关于coroutine - 非对称和对称协程有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41891989/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com