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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Show HN: Neco – Coroutine Library for C

145 点作者 tidwall大约 1 年前

11 条评论

dkjaudyeqooe大约 1 年前
These are stackful coroutines, as opposed to stackless.<p>Stackful coroutines create a new stack for each coroutine thread and save and restore the appropriate registers when switching. Stackless coroutines are basically normal C functions that have been hacked to save their local variables between calls and use gotos (or, notoriously, a switch statement) to resume from where they last yielded. Both are very useful in their own way.<p>This is a great project and I&#x27;ll be trying it out in my current work.
评论 #39986000 未加载
评论 #39988169 未加载
plagiat0r大约 1 年前
I love how simple and elegant is this to use: <a href="https:&#x2F;&#x2F;github.com&#x2F;tidwall&#x2F;neco&#x2F;blob&#x2F;main&#x2F;examples&#x2F;echo-server.c">https:&#x2F;&#x2F;github.com&#x2F;tidwall&#x2F;neco&#x2F;blob&#x2F;main&#x2F;examples&#x2F;echo-serv...</a><p>I was going to actually implement an echo server for load balancer health checks with minimal memory usage, but never considered doing it in C but I might just use it! Thank you so much.
keepamovin大约 1 年前
I like how your examples become progressively more comprehensive. What was your thinking going into this? How did you design this library? i think you wanted something this beautiful and perfect to exist, am i right? Or is it also an exercise to develop your own understanding? I wouldn&#x27;t know where to begin with C gens&#x2F;coroutines. Probably I would just fallback on kernel&#x2F;sys calls to suspend and hack from there: yay my function is stopped; yay my code has been called again. Are you using setjmp&#x2F;longjmp?
andrekandre大约 1 年前
<p><pre><code> &gt; neco_chan *messages = argv[0]; </code></pre> neco_chan → neko chan → kitty cat (in japanese)... coincidence? ^_^
评论 #39987001 未加载
dingdingdang大约 1 年前
Can someone help elucidate why &quot;It&#x27;s a non-goal for Neco to provide a scalable multithreaded runtime, where the coroutine scheduler is shared among multiple cpu cores [...]&quot; this library even makes sense then? When I use coroutines in Go it is invariably in order to make use of more CPU cores when extra performance needs to be extracted.
评论 #39985111 未加载
评论 #39986072 未加载
评论 #39984350 未加载
评论 #39984762 未加载
评论 #39985332 未加载
评论 #39986704 未加载
jonhohle大约 1 年前
I’ve used libaco with a lot of success in the past (mixing uv, curl, and zlib in a way that made the zlib loop easier to manage (since it looked like a normal read, decompress loop with all context stack local).<p>Any notes about why I would try Neco in the future?
评论 #39986156 未加载
anfilt大约 1 年前
It&#x27;s a little lower level than what you have, but you may find somethings interesting here: <a href="https:&#x2F;&#x2F;github.com&#x2F;Keith-Cancel&#x2F;Bunki">https:&#x2F;&#x2F;github.com&#x2F;Keith-Cancel&#x2F;Bunki</a>
nialv7大约 1 年前
These are cooperatively scheduled green threads...<p>I feel the word &quot;coroutine&quot; is slowly losing its original meaning, similar to what happened to &quot;lambda&quot;.
评论 #39986136 未加载
评论 #39986016 未加载
评论 #39985847 未加载
comex大约 1 年前
How does it work on WebAssembly, where there&#x27;s no native support for stack switching? Does it rely on Emscripten&#x27;s Asyncify transformation?
评论 #39986851 未加载
Karellen大约 1 年前
In &quot;Example 4&quot;, I don&#x27;t understand where the &quot;arg2&quot; in `free(arg2)` is coming from?
评论 #39988619 未加载
burntcaramel大约 1 年前
Can someone explain how this works please? How does it prevent this from saturating the CPU?<p><pre><code> while (1) { neco_sleep(NECO_SECOND*2); printf(&quot;tock\n&quot;); }</code></pre>
评论 #39985754 未加载