4 ms·
You can build coroutines from them, too, which is really cool and useful.
by mlyle 7y ago
You can build coroutines from them, too, which is really cool and useful.
- vasama 7y agoI think you mean fibers, and no you can't.
- tlb 7y agoHere is an implementation of coroutines on top of setjmp/longjmp: https://fanf.livejournal.com/105413.html https://fanf.livejournal.com/105413.html
- vasama 7y agoLooks more like an implementation of undefined behaviour to me.
- monocasa 7y agoThe underlying implementations define the behavior here very well.
- fanf2 7y agoWell, I would not go that far. My coroutine hack relies on “unwarranted chumminess with the implementation” (http://c-faq.com/struct/structhack.html http://c-faq.com/struct/structhack.html) and there are lots of incidental things that can break it. It is best treated as an example of the principles of thread switching and stack allocation.
- craftinator 7y agoDon't be an ass you 'fiber' cretin.
- dang 7y ago"Please don't post shallow dismissals, especially of other people's work. A good critical comment teaches us something." https://news.ycombinator.com/newsguidelines.html https://news.ycombinator.com/newsguidelines.html
- superpermutat0r 7y agoHow does it work for arbitrary arguments? What about function pointers. What about calling a coroutine inside a coroutine? It also has a stack managed in runtime. I'm not sure that would work for any other application than the fun example here.
- deleted 7y ago[deleted]
- craftinator 7y agoDon't be an ass you 'fiber' cretin.
- thechao 7y agoI did! And the ergonomics are dreadful! https://github.com/jaroslov/coro https://github.com/jaroslov/coro
- thomasjames 7y agoI had only seen them in the context of userspace threading. I wonder what mainstream libs use them. Do you know of any?
- fucking_tragedy 7y agoGenerators are a subset of coroutines, and coroutines are useful in lexing and parsing.
- thomasjames 7y agoWhoops, I meant that use set/longjmp() to implement coroutines. My wording in retrospect was super unclear. Sorry!
- yongjik 7y agoI don't know why I still remember these, but if you need to build coroutines in C, ucontext.h might be a better tool: https://pubs.opengroup.org/onlinepubs/7908799/xsh/ucontext.h.html https://pubs.opengroup.org/onlinepubs/7908799/xsh/ucontext.h...
- anfilt 7y agoThose are deprecated.