6 ms·
Semi off topic, but are there examples of coroutine usage in any relatively popular open source C or C++ code bases? The only thing that comes to mind is the MA
by aninteger 11y ago
Semi off topic, but are there examples of coroutine usage in any relatively popular open source C or C++ code bases? The only thing that comes to mind is the MAME and MESS code bases (I could be wrong here). We see coroutine articles on HN from time to time but I've always wondered who or what software out there are using these types of techniques.
- userbinator 11y agoQEMU is another one that I know of: http://blog.vmsplice.net/2014/01/coroutines-in-qemu-basics.html http://blog.vmsplice.net/2014/01/coroutines-in-qemu-basics.h... Coroutines seem like the type of concept that relatively few programmers understand and use, but when they are used well, it can simplify the code flow greatly.
- pm215 11y agoSpeaking as a QEMU developer, I really don't like the coroutine use. They're a portability mess, they tend to expose bugs in dusty corners of the compiler, and they can be painful to debug around. I would much rather take a view that C is simply not a language with coroutines in it, and not try to retrofit them without explicit support from the compiler and runtime...
- jhallenworld 11y agoThere is a branch of JOE which uses co-routines: http://sourceforge.net/p/joe-editor/mercurial/ci/default/tree/docs/hacking.md http://sourceforge.net/p/joe-editor/mercurial/ci/default/tre... The main release doesn't use them yet, but the Windows port does. My co-routine library also allows arbitrary arguments, but it just uses va_list for this. Also I use setjmp/longjump and stack allocations (if ucontext is not available), but do not depend on c99 dynamic arrays to do it. BTW, the array trick doesn't work on some architectures: IA64 (there are two stacks) or Cray (the stack is a linked-list).
- Pfiffer 11y agoThe lwan webserver uses them: http://tia.mat.br/blog/html/2012/09/29/asynchronous_i_o_in_c_with_coroutines.html http://tia.mat.br/blog/html/2012/09/29/asynchronous_i_o_in_c...
- david-given 11y agoIt's hardly well known, but I implemented an SMTP greylisting proxy called Spey which used a coroutine for each connection with a cooperative scheduler which switched between them as data arrived. (Cooperative scheduling has huge conceptual advantages, because now you don't have to think about synchronisation and concurrency issues.) It worked really well --- on my machine. Unfortunately on other machines there were weird bugs and instability, and really-hard-to-diagnose crashes; because it all worked fine on my machine, debugging was painful. Eventually I figured out that pthreads, which was being linked in by a library I depended on, when combined with a particular glibc and a particular Linux kernel, would store the TLS pointer at the top of the C stack --- it used alignment tricks to be able to figure out where the TLS pointer was from the current stack frame. (I assume this was to work around a kernel with no native TLS support.) Of course, my coroutine implementation was allocate its own stack with mmap(). This was causing pthreads to pick up either a garbage TLS pointer or, even worse, the wrong TLS pointer. That was when I gave up on manual coroutines in C. Lovely idea, works really well on paper, so much simpler than threading (if you can live without multicore support), doesn't actually work in practice. They're still worth checking out in languages like Lua, though. I'm still bitter that ES6 doesn't have proper coroutines, opting for the much less useful generator concept instead. Apparently they were too complicated to implement...