TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Show HN: Libconcurrent – Coroutines in C

91 点作者 mitghi超过 9 年前

9 条评论

david-given超过 9 年前
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 未加载
brudgers超过 9 年前
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 未加载
halayli超过 9 年前
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>
stepanhruda超过 9 年前
How does this compare to <a href="http:&#x2F;&#x2F;libmill.org" rel="nofollow">http:&#x2F;&#x2F;libmill.org</a>?
评论 #10896810 未加载
geofft超过 9 年前
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 未加载
evmar超过 9 年前
It would be helpful if the README explained how this is different than the many other coroutine libraries.
delinka超过 9 年前
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 未加载
ghrifter超过 9 年前
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 未加载
pantalaimon超过 9 年前
Are the Coroutines scheduled across multiple CPUs?
评论 #10896523 未加载