TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Coroutines in C (2000)

181 pointsby woobyover 6 years ago

16 comments

edsiper2over 6 years ago
A really good library that we use is libco[0], it supports different architectures and mechanisms. We use it extensively on Fluent Bit[1] to manage async IO network operations (epoll + coroutines).<p>- [0] <a href="https:&#x2F;&#x2F;byuu.org&#x2F;library&#x2F;libco&#x2F;" rel="nofollow">https:&#x2F;&#x2F;byuu.org&#x2F;library&#x2F;libco&#x2F;</a><p>- [1] <a href="https:&#x2F;&#x2F;fluentbit.io" rel="nofollow">https:&#x2F;&#x2F;fluentbit.io</a>
评论 #19111257 未加载
brobdingnagiansover 6 years ago
A cool library that implements C coroutines, available for both idiomatic C (<a href="http:&#x2F;&#x2F;libdill.org&#x2F;" rel="nofollow">http:&#x2F;&#x2F;libdill.org&#x2F;</a>) and in the same style as Go (<a href="http:&#x2F;&#x2F;libmill.org&#x2F;" rel="nofollow">http:&#x2F;&#x2F;libmill.org&#x2F;</a>) with line by line translations
评论 #19107283 未加载
jeremycwover 6 years ago
I ran with this idea last year and wrote a proof of concept library that wraps libev using using this concept to allow you to write async C code inline. It&#x27;s about 250 lines of macro abuse and the resulting code looks pretty funky but it ultimately works. I have a simple echo server example here: <a href="https:&#x2F;&#x2F;github.com&#x2F;jeremycw&#x2F;zasync&#x2F;blob&#x2F;master&#x2F;echo_server.c" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;jeremycw&#x2F;zasync&#x2F;blob&#x2F;master&#x2F;echo_server.c</a>
plaguuuuuuover 6 years ago
As an OOP dev this kind of horrifies me, even though it works okay (though it&#x27;s surely not thread-safe?)<p>Nevertheless, even in C, I&#x27;d find it conceptually simpler to understand if the functions were written as pure functions, returning a tuple with (c, state) and passing the state back into the caller. Using non-idiomatic tricks and macros in this way is harder to read and understand.
评论 #19109996 未加载
c-smileover 6 years ago
And here is my C++ version of Simon&#x27;s idea, C++ generators in 10 lines header file:<p><a href="https:&#x2F;&#x2F;www.codeproject.com&#x2F;Tips&#x2F;29524&#x2F;Generators-in-C" rel="nofollow">https:&#x2F;&#x2F;www.codeproject.com&#x2F;Tips&#x2F;29524&#x2F;Generators-in-C</a>
entelechyover 6 years ago
We tried to implement coroutines based on the CoroutineTS here: <a href="https:&#x2F;&#x2F;github.com&#x2F;LoopPerfect&#x2F;conduit" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;LoopPerfect&#x2F;conduit</a><p>CoroutineTS uses coroutines that are implemented on the LLVM-IR level, as a result they get optimized away in many cases.<p>Unfortunately the CoroutineTS has also some design flaws around allocations and ownership
ThJover 6 years ago
I thought this was going to be an article about setjmp() and longjmp(), which can be used to implement this kind of mechanism.
evancox100over 6 years ago
Has anyone used these to write interrupt service routines? Seems like there&#x27;s an opportunity here. You can take your existing synchronous code and not have to rewrite it into something like continuation-passing style. This is a source of a lot of bugs.
评论 #19108566 未加载
mcwoodsover 6 years ago
Isn&#x27;t this just an application of a protothread[1]?<p>There are implementations that use the GCC goto and label pointer, which then avoid the need for a switch statement.<p>[1] <a href="http:&#x2F;&#x2F;dunkels.com&#x2F;adam&#x2F;pt&#x2F;" rel="nofollow">http:&#x2F;&#x2F;dunkels.com&#x2F;adam&#x2F;pt&#x2F;</a>
评论 #19111954 未加载
评论 #19112761 未加载
hectorhectorover 6 years ago
What&#x27;s the purpose of this code in the original decompressor? Assuming c is an uchar, aren&#x27;t EOF and 0xFF equal?<p><pre><code> if (c == 0xFF) { len = getchar(); c = getchar(); while (len--) emit(c); }</code></pre>
评论 #19107480 未加载
评论 #19107486 未加载
评论 #19107416 未加载
s-mackeover 6 years ago
I guess one of the applications of Coroutines could be WebAssembly. When you run WebAssembly on a website which needs 100% of the CPU for more than a few ms, you have to use the &quot;setTimeout(fn, 0)&quot; trick to prevent the &quot;this website stopped responding&quot; message. That means you have to store the current state at some point and continue in the function fn (after waiting 0 ms). Languages which support coroutines out of the box would help a lot here.
评论 #19108671 未加载
dangover 6 years ago
Several previous discussions: <a href="https:&#x2F;&#x2F;hn.algolia.com&#x2F;?query=Coroutines%20in%20C%20points%3E40&amp;sort=byDate&amp;dateRange=all&amp;type=story&amp;storyText=false&amp;prefix=false&amp;page=0" rel="nofollow">https:&#x2F;&#x2F;hn.algolia.com&#x2F;?query=Coroutines%20in%20C%20points%3...</a>
jfriesover 6 years ago
I really like that trick. It&#x27;s very powerful. But I wonder what the difference is between this and protothreads?
评论 #19109008 未加载
0db532a0over 6 years ago
You could use thread_local variables these days for re-entrancy instead of the ctx-&gt;i idea.
评论 #19118544 未加载
bluesnowmonkeyover 6 years ago
That page uses very simple HTML. Just paragraphs of text divided into sections with headings. Some code examples laid out side-by-side with a table. Tiny stylesheet. No JS. Loads quickly, easy to read. Warms the cockles of my heart.
评论 #19108076 未加载
评论 #19108046 未加载
评论 #19107903 未加载
评论 #19107923 未加载
yydcoolover 6 years ago
python has this:<p><pre><code> def function(): for i in range(10): yield i</code></pre>