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.

Show HN: Libconcurrent – Coroutines in C

91 pointsby mitghiover 9 years ago

9 comments

david-givenover 9 years ago
I wrote a networking daemon (SMTP greylisting proxy; now unmaintained, but &lt;plug&gt; <a href="http:&#x2F;&#x2F;spey.sf.net" rel="nofollow">http:&#x2F;&#x2F;spey.sf.net</a> &lt;&#x2F;plug&gt;) using coroutines once.<p>Non-preemptive multitasking made reasoning about the problem way easier, because I had no concurrency to worry about, while still allowing inversion of control flow which meant that the SMTP state machines were small and simple. It worked really well.<p>...until I started getting bug reports from users about weird irreproducable crashes. After a very, <i>very</i> long time I eventually figured out that if spey was built on a system where sqlite had been compiled with pthread support, then it automatically linked in a thread-safe malloc, which tried to fetch the current thread ID, which on some versions of pthreads on some Linux architectures on some glibc versions with some kernel versions, was stored at the top of the stack. It used alignment tricks to find this.<p>So, every time something called malloc(), the libc was doing the equivalent of:<p><pre><code> threadid = *(int*) (alignup(sp, 16MB) - sizeof(int)) </code></pre> Except my stacks weren&#x27;t allocated through pthreads, so threadid ended up being garbage. Hilarity ensued.<p>I eventually rebuild the coroutine library to be a wrapper around pthreads with a big lock to ensure that only one would run at a time. Which was kind of evil, but much more reliable. (Previously I was using my own coroutine implementation based on getcontext&#x2F;setcontext.)<p>So, uh. I can&#x27;t remember if I had a point any more; but the pain remains.
评论 #10897689 未加载
评论 #10897304 未加载
brudgersover 9 years ago
The links to other projects with similar goals at the bottom of the Readme is brilliant. It helps developers solve their problems irrespective of whether this particular solution works...and solving problems not stars and forks is the measure of utility for a library.
评论 #10895665 未加载
halayliover 9 years ago
You might also want to look at lthread. It has some interesting functions like lthread_compute_begin()&#x2F;end() to run expensive computations inside a coroutine. the compute methods move the coroutine into an actual pthread and resume until compute_end() is called. It also has c++11 bindings called lthread-cpp which allows you to launch coroutines with variable arguments &amp; types and comes with nice socket wrappers.<p><pre><code> void MyMethod(std::vector&lt;int&gt; my_vec) {} std::vector&lt;int&gt; v{1,2,3,4}; Lthread t1{&amp;MyMethod, v}; t1.detach() void my_coroutine() { lthread_compute_begin(); ret = fibonacci(55); lthread_compute_end(); }</code></pre>
stepanhrudaover 9 years ago
How does this compare to <a href="http:&#x2F;&#x2F;libmill.org" rel="nofollow">http:&#x2F;&#x2F;libmill.org</a>?
评论 #10896810 未加载
geofftover 9 years ago
Cool!<p>A co-TA and I ported the concurrency library in JOS, MIT&#x27;s teaching operating system, to regular UNIX:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;geofft&#x2F;vireo" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;geofft&#x2F;vireo</a><p>It uses the standard-ish library functions setcontext and getcontext to avoid an assembly dependency. (&quot;POSIX.1-2008 removes the specification of getcontext(), citing portability issues, and recommending that applications be rewritten to use POSIX threads instead.&quot;)
评论 #10895667 未加载
evmarover 9 years ago
It would be helpful if the README explained how this is different than the many other coroutine libraries.
delinkaover 9 years ago
How does this compare to Apple&#x27;s GCD [1]?<p>1 - <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Grand_Central_Dispatch" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Grand_Central_Dispatch</a>
评论 #10896959 未加载
评论 #10895838 未加载
ghrifterover 9 years ago
I got a silly question:<p>Is this statement basically a do while&#x2F;while(true) infinite loop?<p><pre><code> for (;;) { ... }</code></pre>
评论 #10895514 未加载
评论 #10895519 未加载
pantalaimonover 9 years ago
Are the Coroutines scheduled across multiple CPUs?
评论 #10896523 未加载