java - 协程就是goto吗?
高洛峰
高洛峰 2017-04-17 15:26:00
0
4
1143

协程就是goto吗?两者有何异同?

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(4)
PHPzhong

goto jumps within the same function. Coroutine should be similar to setjump and longjump, which jumps between different functions.

大家讲道理

This is not accurate. It should be said that one of the main functions of coroutines is cross-function goto . Of course, goto can be returned when needed, which is more like a function call.

大家讲道理

Coroutines are a component of computer programming that standardizes the concept of subroutines. Coroutines are very suitable for implementing some useful program components such as cooperative multitasking, exception handling, event loops, and iteration Devices, infinite linked lists and pipes, etc.

The following compares general subroutines and coroutines:

The beginning of a subroutine is the only entry point. Once exited, the execution of the subroutine is completed. An instance of the subroutine will only return once.

A subroutine is always started at its beginning, which is usually a fixed position. A parallel program is always started at the next position where it last ended.

Coroutines can call other coroutines through yield. The relationship between coroutines that transfer execution rights through yield is not the relationship between the caller and the callee, but is symmetrical and equal to each other.

Subroutines are easy to implement on the stack because subroutines call other subroutines as subordinates. Instead, coroutines call other coroutines equally, best implemented using continuations (implemented by a garbage-collected heap) to track the flow of control.

Marlin describes the characteristics of coroutine as follows:

Local variables in the coroutine remain valid in subsequent calls.

The coroutine is suspended when control leaves, and execution continues where it was suspended when control is returned to it.

Classification of coroutines

1) Classification by control transfer mechanism: symmetric coroutine and asymmetric coroutine

Asymmetric coroutines (asymmetric), also known as semi-symmetric coroutines (semi-symmetric) or semi-coroutines (semi-coroutines). Asymmetric coroutines can be regarded as subordinate processes of the caller. The relationship between is similar to the relationship between calling and called routines.

Symmetric coroutines only provide a control transfer mechanism: handing over the execution process to the specified coroutine. Because symmetric coroutines can transfer execution processes to each other, the relationship between them is as if they are at the same level.

2) Is the coroutine a first-class type?

3) Is there a complete stack?

Coroutines with a complete stack allow the coroutine to suspend itself in the innermost function. After this coroutine is resumed, it will continue to execute from the place where it was suspended.

Coroutines without a complete stack, such as Python’s generator, can only be suspended in the main body of the generator.

Coroutines with a complete stack can be used to implement user-level multitasking, but coroutines without a complete stack cannot.

大家讲道理

The keyword of coroutine is yield. Personally, I think the meaning should be:
Interrupt execution and return data. Call again, return to breakpoint, and resume execution.
yield is equivalent to return and goto breakpoint.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!