3 ms·
Let me rant about a bigger point: What are coroutines conceptually? Well, it's a "thing" that we use in order to express sequential code (I'm sure there is more
by majke 11y ago
Let me rant about a bigger point: What are coroutines conceptually? Well, it's a "thing" that we use in order to express sequential code (I'm sure there is more abstract definition).
What is a difference between Erlang Proc, Golang Goroutine, Operating System Thread, and this kind of Duff's device - not much if you think about it. It's all about sequential code with
a bit of context (variables) attached - the closure.
The only difference between a thread and asynchronous programming is how you deal with the context - is it explicit or implicit?
In this Duff's device, the context is passed explicitly. It's an (arguably nice) syntactic sugar over a trivial state machine.
The main point here is - how large is the context? If that's a few variables, sure you can pack them on a struct, or just async programming. If the context is somewhat non-trivial, having it implicit makes sense. If the context is really large you probably _must_ use OS / programming language help.
The problem - the larger the context, the costlier the context switch. It would be nice to have some kind of magic that would be able to chose the optimal coroutines hardware implementation based on the size of context.
- greggman 11y ago> The only difference between a thread and asynchronous programming is how you deal with the context Coroutines are not threads. Each serve vastly different purposes. Coroutines are basically an easier way of expressing certain kinds of state machines. They're great for games when you need a character or other game object to progress through different states. They free you from 3-10x boilerplate that would be required if you had to write and manipulate a state machine directly. They're not related to threading whatsoever.