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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

My tutorial and take on C++20 coroutines

187 点作者 erwan大约 4 年前

18 条评论

nly大约 4 年前
Some notes:<p>- C++20 coroutines are <i>stackless</i>, meaning that multiple coroutines will share a single OS thread stack. This is non-obvious when you first look in to them them because coroutines look just like functions. The compiler does all the work of ensuring your local variables are captured and allocated as part of the coroutine yield context, but these yield contexts are <i>not stack frames</i>. Every coroutine you invoke from a coroutine has its own context that can outlive the caller.<p>- C++20 coroutine functions are just ordinary functions, and can be called from C code. In fact, coroutine contexts can be cast to and from void* to enable their use from C.<p>- There&#x27;s currently no generic utility in the C++20 standard library for using C++20 coroutines to defer work. std::future&lt;&gt; only works on threaded code, and you can&#x27;t really use std::function&lt;&gt;. See Lewis Bakers &#x27;task&#x27; class for a usable mechanism <a href="https:&#x2F;&#x2F;github.com&#x2F;lewissbaker&#x2F;cppcoro#taskt" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;lewissbaker&#x2F;cppcoro#taskt</a>
评论 #26222603 未加载
评论 #26222783 未加载
评论 #26230640 未加载
评论 #26222421 未加载
评论 #26229618 未加载
评论 #26225400 未加载
oivey大约 4 年前
Part of this is that I’m tired, but it blows my mind how difficult C++ coroutines are as someone who considers themselves decent at C++ (although maybe I’m not) and uses coroutines in other languages. The amount of code needed to do almost nothing is extraordinary, and putting it all together doesn’t seem like you would often get on your first try. I get that new keywords basically can’t be added, but man, that’s painful.
评论 #26222385 未加载
评论 #26222538 未加载
评论 #26224133 未加载
评论 #26228945 未加载
评论 #26222919 未加载
评论 #26247693 未加载
评论 #26222912 未加载
tobias3大约 4 年前
I&#x27;ve used C++ coroutines (with io_uring). They are really useful in this case and allows one to write code like one would with the simple blocking API. And from what I&#x27;ve read they are better than Rusts coroutines for this use case (and for the yield use case they aren&#x27;t good).<p>It adds an additional foot-gun w.r.t. to e.g. by-reference parameters to functions and their lifetime. The function parameter might not be alive anymore after a &quot;co_await&quot; and it doesn&#x27;t require any capture (like lambda) or has any hint about this being the case.<p>Then, the tooling isn&#x27;t there yet (other than the missing standard library). Gdb doesn&#x27;t show correct lines and can&#x27;t print local variables when in coroutines. If there is a deadlock one can&#x27;t see the suspended coroutine (and its call stack) holding the lock, etc.. Back to printf debugging...
评论 #26226900 未加载
zarkov99大约 4 年前
Seems like once again we are stuck with an overly complex and un-ergonomic design that is going to burden every single C++ programmer for a decade, until someone gets fed up and fixes it. Just like chrono, just like random, just like std::string, all of std::algorithm, etc. God damn it.
评论 #26230658 未加载
smitty1e大约 4 年前
&gt; C++20 coroutines are implemented as a nice little nugget buried underneath heaps of garbage that you have to wade through to access the nice part. Frankly, I was disappointed by the design, because other recent language changes were more tastefully done, but alas not coroutines. Further obfuscating coroutines is the fact that the C++ standard library doesn’t actually supply the heap of garbage you need to access coroutines, so you actually have to roll your own garbage and then wade through it.<p>Well, that sounds like more fun than a tax increase.
sempron64大约 4 年前
What I find interesting about coroutines is that they are relatively trivially achieved in hand-coded assembly using `jmp` and being careful about registers in a way that makes sense. `jmp`ing around is a pretty normal way to code in assembly. In a sophisticated environment they become a beast. It surprises me that we actually do lose something meaningful when abstracting to higher level languages.
评论 #26227959 未加载
评论 #26228531 未加载
volta83大约 4 年前
Its insane how complex C++20 coroutines are when compared with Rust coroutines.
评论 #26226963 未加载
评论 #26229092 未加载
jupp0r大约 4 年前
Keep in mind that these is a really basic building block where you can bring your own runtime and hook coroutines into it, not something that is at all usable out of the box. This is exacerbated by the fact that the C++ standard library is still lacking support for non-blocking futures&#x2F;promises and queued thread pools to run them.<p>To see how it can be used for actual asynchronous operations on a thread pool, take a look at asyncly, which I co-authored:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;LogMeIn&#x2F;asyncly&#x2F;blob&#x2F;master&#x2F;Test&#x2F;Unit&#x2F;future&#x2F;CoroutineTest.cpp" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;LogMeIn&#x2F;asyncly&#x2F;blob&#x2F;master&#x2F;Test&#x2F;Unit&#x2F;fut...</a>
signa11大约 4 年前
i used this : <a href="https:&#x2F;&#x2F;blog.panicsoftware.com&#x2F;coroutines-introduction&#x2F;" rel="nofollow">https:&#x2F;&#x2F;blog.panicsoftware.com&#x2F;coroutines-introduction&#x2F;</a> a while back. maybe someone else might find it useful too...
smallstepforman大约 4 年前
From the existing explanations, I’ve deduced that coroutines are a form of continuation based coopetative multitasking. Is it possible to “restart” the sequence, and how would this be done?
评论 #26224291 未加载
评论 #26222404 未加载
dilawar大约 4 年前
I see they are excellent for concurrent programming but is it possible to utilise all cores of processor effectively with coroutines?
评论 #26225644 未加载
评论 #26223352 未加载
评论 #26223332 未加载
perennus大约 4 年前
Typical HN comment I know, but how is this blog generated? It looks fantastic. Is this only Markdown + Pandoc?
wapxmas大约 4 年前
&quot;std::coroutine_handle&lt;&gt; *hp_;&quot;<p>does it, the former line, really count as a tutorial at the very beginning?
superkuh大约 4 年前
As a developer, when you decide to use bleeding edge C++?? extensions keep in mind that when you do so you&#x27;re making it much, much harder to run any of your code on a distro more than 4 years old. Is it worth it?
评论 #26226279 未加载
评论 #26228047 未加载
cozzyd大约 4 年前
Note that the author (in addition to being a great professor) is responsible for one of my favorite papers: <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=11098655" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=11098655</a>
panpanna大约 4 年前
Is learning modern C++ worth the effort in 2021?<p>I know classic c++, but don&#x27;t really use it anymore.
评论 #26222597 未加载
评论 #26222557 未加载
评论 #26223021 未加载
评论 #26221942 未加载
评论 #26222470 未加载
评论 #26222323 未加载
评论 #26223651 未加载
评论 #26222034 未加载
creedor大约 4 年前
I&#x27;m a bit confused by all of this. The idea of coroutines existed since the 60&#x27;s. Then we came up with OOP that tries to combine function + data. And now we abolished this, are back at only functions and are now solving the &#x27;state capture&#x27; problem for functions again with: Coroutines? How does the history of programming paradigms&#x2F;patterns make any sense? :)<p>Good article though.
评论 #26222077 未加载
评论 #26221986 未加载
评论 #26222228 未加载
评论 #26222011 未加载
评论 #26221989 未加载
zabzonk大约 4 年前
void main() does not exactly inspire confidence.<p><a href="https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;636829&#x2F;difference-between-void-main-and-int-main-in-c-c" rel="nofollow">https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;636829&#x2F;difference-betwee...</a>
评论 #26222621 未加载