3 ms·
These are stackful coroutines, as opposed to stackless. Stackful coroutines create a new stack for each coroutine thread and save and restore the appropriate r
by dkjaudyeqooe 3y ago
These are stackful coroutines, as opposed to stackless.
Stackful coroutines create a new stack for each coroutine thread and save and restore the appropriate registers when switching. Stackless coroutines are basically normal C functions that have been hacked to save their local variables between calls and use gotos (or, notoriously, a switch statement) to resume from where they last yielded. Both are very useful in their own way.
This is a great project and I'll be trying it out in my current work.
- liuliu 3y agoYeah, at some point, would just say if you want stackful coroutine, go to Go and stackless one, use C++. BTW, here is my macros for stackless coroutine in C: https://github.com/liuliu/co/blob/master/example.c https://github.com/liuliu/co/blob/master/example.c
- styren 3y agoWould you mind elaborating on the pros and cons of each?
- dkjaudyeqooe 3y agoStackless are very lightweight and can be done without stepping outside of C code, but it's a bit hacky, using either macros or generating the coroutine yourself. One big limitation is you have to yield from the originally called coroutine function and not from any function it called (because the coroutine has no stack of its own, but you could work around this, with more complexity). So besides the function and the memory required to save its local variables when it yields, no resources are required. These are great when you just want a basic generator or something. It's simple, platform independent, efficient and you can do it yourself (at least in C). Stackful coroutines are more flexible in that they don't have the calling limitations since they have their own stack. The big downside is that they are much more complex to implement and use (understand). The code has to create and destroy stacks from scratch and switch contexts by saving and restoring the correct registers (so your platform has to be supported explicit). This may cause compatibility problems with some code. Those activities also require more resources, but still not very much. For some people they will be easier to use since you don't have to roll anything yourself.