frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Looking at Unity made me understand the point of C++ coroutines

https://mropert.github.io/2026/03/20/unity_cpp_coroutines/
46•ingve•3d ago

Comments

abcde666777•1h ago
More broadly the dimension of time is always a problem in gamedev, where you're partially inching everything forward each frame and having to keep it all coherent across them.

It can easily and often does lead to messy rube goldberg machines.

There was a game AI talk a while back, I forget the name unfortunately, but as I recall the guy was pointing out this friction and suggesting additions we could make at the programming language level to better support that kind of time spanning logic.

repelsteeltje•1h ago
> There was a game AI talk a while back, I forget the name unfortunately, but as I recall the guy was pointing out this friction and suggesting additions we could make at the programming language level to better support that kind of time spanning logic.

Sounds interesting. If it's not too much of an effort, could you dig up a reference?

cherryteastain•1h ago
Not an expert in game development, but I'd say the issue with C++ coroutines (and 'colored' async functions in general) is that the whole call stack must be written to support that. From a practical perspective, that must in turn be backed by a multithreaded event loop to be useful, which is very difficult to write performantly and correctly. Hence, most people end up using coroutines with something like boost::asio, but you can do that only if your repo allows a 'kitchen sink' library like Boost in the first place.
spacechild1•1h ago
ASIO is also available outside of boost! https://github.com/chriskohlhoff/asio
lionkor•44m ago
For anyone wondering; this isn't a hack, that's the same library, just as good, just without boost dependencies.
pjc50•45m ago
Much of the original motivation for async was for single threaded event loops. Node and Python, for example. In C# it was partly motivated by the way Windows handles a "UI thread": if you're using the native Windows controls, you can only do so from one thread. There's quite a bit of machinery in there (ConfigureAwait) to control whether your async routine is run on the UI thread or on a different worker pool thread.

In a Unity context, the engine provides the main loop and the developer is writing behaviors for game entities.

pjc50•1h ago
Always jarring to see how Unity is stuck on an ancient version of C#. The use of IEnumerable as a "generator" mechanic is quite a good hack though.
repelsteeltje•1h ago
Not too different from C++'s iterator interface for generators, I guess.
debugnik•1h ago
Not that ancient, they just haven't bothered to update their coroutine mechanism to async/await. The Stride engine does it with their own scheduler, for example.

Edit: Nevermind, they eventually bothered.

nananana9•58m ago
Unity has async too [1]. It's just that in a rare display of sanity they chose to not deprecate the IEnumerator stuff.

[1] https://docs.unity3d.com/6000.3/Documentation/ScriptReferenc...

debugnik•16m ago
Oh I totally missed this, thanks! I was overly confident they wouldn't have bothered, given how long it was taking. The last time I used Unity was 2022.3, which was apparently the last version without Awaitable.
tyleo•56m ago
Unity is currently on C# 9 and that IEnumerable trick is no longer needed in new codebases. async is properly supported.
ahoka•16m ago
IIRC generators and co-routines are equivalent in a sense that you can implement one with the other.
Philip-J-Fry•16m ago
>The use of IEnumerable as a "generator" mechanic is quite a good hack though.

Is that a hack? Is that not just exactly what IEnumerable and IEnumerator were built to do?

Joker_vD•58m ago
Simon Tatham, author of Putty, has quite a detailed blog post [0] on using the C++20's coroutine system. And yep, it's a lot to do on your own, C++26 really ought to give us some pre-built templates/patterns/scaffolds.

[0] https://web.archive.org/web/20260105235513/https://www.chiar...

nananana9•48m ago
You can roll stackful coroutines in C++ (or C) with 50-ish lines of Assembly. It's a matter of saving a few registers and switching the stack pointer, minicoro [1] is a pretty good C library that does it. I like this model a lot more than C++20 coroutines:

1. C++20 coros are stackless, in the general case every async "function call" heap allocates.

2. If you do your own stackful coroutines, every function can suspend/resume, you don't have to deal with colored functions.

3. (opinion) C++20 coros are very tasteless and "C++-design-commitee pilled". They're very hard to understand, implement, require the STL, they're very heavy in debug builds and you'll end up with template hell to do something as simple as Promise.all

[1] https://github.com/edubart/minicoro

pjc50•41m ago
> You can roll stackful coroutines in C++ (or C) with 50-ish lines of Assembly

I'm not normally keen to "well actually" people with the C standard, but .. if you're writing in assembly, you're not writing in C. And the obvious consequence is that it stops being portable. Minicoro only supports three architectures. Granted, those are the three most popular ones, but other architectures exist.

(just double checked and it doesn't do Windows/ARM, for example. Not that I'm expecting Microsoft to ship full conformance for C++23 any time soon, but they have at least some of it)

Joker_vD•35m ago
Hmm. I'm fairly certain that most of that assembly code for saving/restoring registers can be replaced with setjmp/longjmp, and only control transfer itself would require actual assembly. But maybe not.

That's the problem with register machines, I guess. Interestingly enough, BCPL, its main implementation being a p-code interpreter of sorts, has pretty trivially supported coroutines in its "standard" library since the late seventies — as you say, all you need to save is the current stack pointer and the code pointer.

lelanthran•29m ago
> Hmm. I'm fairly certain that most of that assembly code for saving/restoring registers can be replaced with setjmp/longjmp, and only control transfer itself would require actual assembly.

Actually you don't even need setjmp/longjmp. I've used a library (embedded environment) called protothreads (plain C) that abused the preprocessor to implement stackful coroutines.

(Defined a macro that used the __LINE__ macro coupled with another macro that used a switch statement to ensure that calling the function again made it resume from where the last YIELD macro was encountered)

gpderetta•27m ago
setjmp + longjump + sigaltstack is indeed the old trick.
zabzonk•6m ago
You can do a lot of horrible things with setjmp and friends. I actually implemented some exception throw/catch macros using them (which did work) for a compiler that didn't support real C++ exceptions. Thank god we never used them in production code.
bradrn•30m ago
In Haskell this technique has been called ‘reinversion of control’: http://blog.sigfpe.com/2011/10/quick-and-dirty-reinversion-o...
twoodfin•23m ago
As the author lays out, the thing that made coroutines click for me was the isomorphism with state machine-driven control flow.

That’s similar to most of what makes C++ tick: There’s no deep magic, it’s “just” type-checked syntactic sugar for code patterns you could already implement in C.

(Occurs to me that the exceptions to this … like exceptions, overloads, and context-dependent lookup … are where C++ has struggled to manage its own complexity.)

Meta told to pay $375M for misleading users over child safety

https://www.bbc.com/news/articles/cql75dn07n2o
111•testrun•3h ago•40 comments

I tried to prove I'm not AI. My aunt wasn't convinced

https://www.bbc.com/future/article/20260324-i-tried-to-prove-im-not-an-ai-deepfake
65•dabinat•1h ago•48 comments

TurboQuant: Redefining AI efficiency with extreme compression

https://research.google/blog/turboquant-redefining-ai-efficiency-with-extreme-compression/
211•ray__•7h ago•49 comments

Looking at Unity made me understand the point of C++ coroutines

https://mropert.github.io/2026/03/20/unity_cpp_coroutines/
47•ingve•3d ago•24 comments

VitruvianOS – Desktop Linux Inspired by the BeOS

https://v-os.dev
178•felixding•8h ago•109 comments

Goodbye to Sora

https://twitter.com/soraofficialapp/status/2036532795984715896
779•mikeocool•16h ago•573 comments

Flighty Airports

https://flighty.com/airports
356•skogstokig•11h ago•118 comments

Show HN: I took back Video.js after 16 years and we rewrote it to be 88% smaller

https://videojs.org/blog/videojs-v10-beta-hello-world-again
450•Heff•18h ago•90 comments

In Edison’s Revenge, Data Centers Are Transitioning From AC to DC

https://spectrum.ieee.org/data-center-dc
169•jnord•11h ago•195 comments

Why I forked httpx

https://tildeweb.nl/~michiel/httpxyz.html
130•roywashere•4h ago•81 comments

Tell HN: Litellm 1.82.7 and 1.82.8 on PyPI are compromised

https://github.com/BerriAI/litellm/issues/24512
727•dot_treo•1d ago•437 comments

Apple Business

https://www.apple.com/newsroom/2026/03/introducing-apple-business-a-new-all-in-one-platform-for-b...
652•soheilpro•20h ago•369 comments

I wanted to build vertical SaaS for pest control, so I took a technician job

https://www.onhand.pro/p/i-wanted-to-build-vertical-saas-for-pest-control-i-took-a-technician-job...
322•tezclarke•14h ago•132 comments

VNDB founder Yorhel has died

https://vndb.org/t24787
57•indrora•2d ago•12 comments

You can run a DNS server (2025)

https://simonsafar.com/2025/running_dns/
90•surprisetalk•4d ago•54 comments

Arm AGI CPU

https://newsroom.arm.com/blog/introducing-arm-agi-cpu
357•RealityVoid•18h ago•270 comments

Fun with CSF firmware (RK3588 GPU firmware)

https://icecream95.gitlab.io/fun-with-csf-firmware.html
39•M95D•3d ago•0 comments

Show HN: DuckDB community extension for prefiltered HNSW using ACORN-1

https://github.com/cigrainger/duckdb-hnsw-acorn
60•cigrainger•8h ago•5 comments

The Last Testaments of Richard II and Henry IV

https://www.historytoday.com/archive/feature/last-testaments-richard-ii-and-henry-iv
23•Petiver•3d ago•4 comments

Algorithm Visualizer

https://algorithm-visualizer.org/
123•vinhnx•4d ago•6 comments

Show HN: Email.md – Markdown to responsive, email-safe HTML

https://www.emailmd.dev/
313•dancablam•19h ago•80 comments

Wine 11 rewrites how Linux runs Windows games at kernel with massive speed gains

https://www.xda-developers.com/wine-11-rewrites-linux-runs-windows-games-speed-gains/
1008•felineflock•17h ago•346 comments

Why did the chicken cross the road?

https://taylor.town/other-side
23•surprisetalk•23h ago•6 comments

A Compiler Writing Journey

https://github.com/DoctorWkt/acwj
88•ibobev•12h ago•9 comments

Show HN: Gemini can now natively embed video, so I built sub-second video search

https://github.com/ssrajadh/sentrysearch
348•sohamrj•21h ago•92 comments

An Aural Companion for Decades, CBS News Radio Crackles to a Close

https://www.nytimes.com/2026/03/21/business/media/cbs-news-radio-appraisal.html
65•tintinnabula•3d ago•14 comments

Hypothesis, Antithesis, synthesis

https://antithesis.com/blog/2026/hegel/
257•alpaylan•20h ago•87 comments

Hypura – A storage-tier-aware LLM inference scheduler for Apple Silicon

https://github.com/t8/hypura
208•tatef•20h ago•80 comments

Intel Device Modeling Language for virtual platforms

https://github.com/intel/device-modeling-language
37•transpute•4d ago•1 comments

Missile defense is NP-complete

https://smu160.github.io/posts/missile-defense-is-np-complete/
349•O3marchnative•23h ago•366 comments