eonil
eonil

Reputation: 86115

How are coroutines implemented?

I have a question about coroutine implementation. I saw coroutine first on Lua and stackless-python. I could understand the concept of it, and how to use yield keyword, but I cannot figure out how it is implemented.

Can I get some explanation about them?

Upvotes: 5

Views: 1449

Answers (2)

Jörg W Mittag
Jörg W Mittag

Reputation: 369584

See also: Implementing “Generator” support in a custom language. Generators are basically a limited form of (semi-)coroutines, most of what is discussed in that question applies here as well.

Also: How are exceptions implemented under the hood? While exceptions are obviously very different from coroutines, they both have something in common: both are advanced universal control flow constructs. (In fact, you can implement coroutines using exceptions and exceptions using coroutines.)

Upvotes: 2

user207421
user207421

Reputation: 311013

Coroutining is initiated by pushing the target address, then each coroutine switch exchanges the current PC with the top of the stack, which eventually has to get popped to terminate the coroutining.

Upvotes: 4

Related Questions