3 ms·
What are stackless coroutines? What do they look like?
by exacube 11y ago
What are stackless coroutines? What do they look like?
- acidx 11y agoRead Adam Dunkels' paper on Protothreads.
- exacube 11y agoThanks for your comment. I don't know how to feel about Protothreads. It's just syntatic sugar for using a state-machine to provide continuations. You're also not allowed to carry state over between continuations (although I can see how you can extend it to carry-over some struct of data or something). This greatly diminishes the usefulness of Protothreads.. so it feels more like a fun proof-of-concept. Are there any popular/real-world use cases of Protothreads?
- acidx 11y agoYeah, that's how you end up doing this: store coroutine state in a struct, with an integer that lets you decide where to jump to when you resume the coroutine. With compiler support that can be pretty efficient (gcc has computed goto, for instance). Things get quite awkward in C but similar techniques are manageable in C++. I've never used Protothreads before, but Contiki (the tiny operating system written by the same person) uses them for its process implementation. Similar technique is also used by Putty as well: http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html