来源:https://lewissbaker.github.io/2017/09/25/coroutine-theory
Coroutines
Coroutines generalise the operations of a function by separating out some of the steps performed in the Call and Return operations into three extra operations: Suspend, Resume and Destroy.
比其他文章多描述了一个 Destroy 步骤,更准确。
Note that, like the Return
operation of a function, a coroutine can only be suspended from within the coroutine itself at well-defined suspend-points.
请注意,就像函数的 Return
操作一样,协程只能在定义好的暂停点处从协程自身内部暂停。
Coroutine activation frames
With coroutines there are some parts of the activation frame that need to be preserved across coroutine suspension and there are some parts that only need to be kept around while the coroutine is executing. For example, the lifetime of a variable with a scope that does not span any coroutine suspend-points can potentially be stored on the stack.