r/cpp • u/trailing_zero_count async enthusiast | TooManyCooks author • Feb 03 '26
Announcing TooManyCooks: the C++20 coroutine framework with no compromises
TooManyCooks aims to be the fastest general-purpose C++20 coroutine framework, while offering unparalleled developer ergonomics and flexibility. It's suitable for a variety of applications, such as game engines, interactive desktop apps, backend services, data pipelines, and (consumer-grade) trading bots.
It competes directly with the following libraries:
- tasking libraries: libfork, oneTBB, Taskflow
- coroutine libraries: cppcoro, libcoro, concurrencpp
- asio wrappers: boost::cobalt (via tmc-asio)
TooManyCooks is Fast (Really)
I maintain a comprehensive suite of benchmarks for competing libraries. You can view them here: (benchmarks repo) (interactive results chart)
TooManyCooks beats every other library (except libfork) across a wide variety of hardware. I achieved this with cache-aware work-stealing, lock-free concurrency, and many hours of obsessive optimization.
TooManyCooks also doesn't make use of any ugly performance hacks like busy spinning (unless you ask it to), so it respects your laptop battery life.
What about libfork?
I want to briefly address libfork, since it is typically the fastest library when it comes to fork/join performance. However, it is arguably not "general-purpose":
- (link) it requires arcane syntax (as a necessity due to its implementation)
- it requires every coroutine to be a template, slowing compile time and creating bloat
- limited flexibility w.r.t. task lifetimes
- no I/O, and no other features
Most of its performance advantage comes from its custom allocator. The recursive nature of the benchmarks prevents HALO from happening, but in typical applications (if you use Clang) HALO will kick in and prevent these allocations entirely, negating this advantage.
TooManyCooks offers the best performance possible without making any usability sacrifices.
Killer Feature #1 - CPU Topology Detection
As every major CPU manufacturer is now exploring disaggregated / hybrid architectures, legacy work-stealing designs are showing their age. TooManyCooks is designed for this new era of hardware.
It uses the CPU topology information exposed by the libhwloc library to implement the following automatic behaviors:
- (docs) locality-aware work stealing for disaggregated caches (e.g. Zen chiplet architecture).
- (docs) Linux cgroups detection sets the number of threads according to the CPU quota when running in a container
- If the CPU quota is set instead by selecting specific cores (
--cpuset-cpus) or with Kubernetes Guaranteed QoS, the hwloc integration will detect the allowed cores (and their cache hierarchy!) and create locality-aware work stealing groups as if running on bare metal.
Additionally, the topology can be queried by the user (docs) (example) and APIs are provided that let you do powerful things:
- (docs)(example) Implement work steering for P- and E- cores on hybrid chips (e.g. Intel Hybrid / ARM big.LITTLE). Apple M / MacOS is also supported by setting the QoS class.
- (example) Turn Asio into a thread-per-core, share-nothing executor
- (example) Create an Asio thread and a worker thread pool for each chiplet in the system, that communicate exclusively within the same cache. This lets you scale both I/O and compute without cross-cache latency.
Killer Features, Round 2
TooManyCooks offers several other features that others do not:
- (docs) (example) support for the only working HALO implementation (Clang attributes)
- (docs) type traits to let you write generic code that handles values, awaitables, tasks, and functors
- (docs) support for multiple priority levels, as well as executor and priority affinity, are integrated throughout the library
- (example) seamless Asio integration
Mundane Feature Parity
TooManyCooks also aims to offer feature parity with the usual things that other libraries do:
- (docs) various executor types
- (docs) various ways to fork/join tasks
- (docs) async data structures (tmc::channel)
- (docs) async control structures (tmc::mutex, tmc::semaphore, etc)
Designed for Brownfield Development
TooManyCooks has a number of features that will allow you to slowly introduce coroutines/task-based concurrency into an existing codebase without needing a full rewrite:
- (docs) flexible awaitables like
tmc::fork_groupallow you to limit the virality of coroutines - only the outermost (awaiting) and innermost (parallel/async) function actually need to be coroutines. Everything in the middle of the stack can stay as a regular function. - global executor handles (
tmc::cpu_executor(),tmc::asio_executor()) and thetmc::set_default_executor()function let you initiate work from anywhere in your codebase - (docs) a manual executor lets you run work from inside of another event loop at a specific time
- (docs) (example) foreign awaitables are automatically wrapped to maintain executor and priority affinity
- (docs) (example) or you can specialize
tmc::detail::awaitable_traitsto fully integrate an external awaitable - (docs) (example) specialize
tmc::detail::executor_traitsto integrate an external executor - (example) you can even turn a C-style callback API into a TooManyCooks awaitable!
Designed for Beginners and Experts Alike
TooManyCooks wants to be a library that you'll choose first because it's easy to use, but you won't regret choosing later (because it's also very powerful).
To start, it offers the simplest possible syntax for awaitable operations, and requires almost no boilerplate. To achieve this, sane defaults have been chosen for the most common behavior. However, you can also customize almost everything using fluent APIs, which let you orchestrate complex task graphs across multiple executors with ease.
TooManyCooks attempts to emulate linear types (it expects that most awaitables are awaited exactly once) via a combination of [[nodiscard]] attributes, rvalue-qualified operations, and debug asserts. This gives you as much feedback as possible at compile time to help you avoid lifetime issues and create correct programs.
There is carefully maintained documentation as well as an extensive suite of examples and tests that offer code samples for you to draw from.
Q&A
Is this AI slop? Why haven't I heard of this before?
I've been building in public since 2023 and have invested thousands of man-hours into the project. AI was never used on the project prior to version 1.1. Since then I've used it mostly as a reviewer to help me identify issues. It's been a net positive to the quality of the implementation.
This announcement is well overdue. I could have just "shipped it" many months ago, but I'm a perfectionist and prefer to write code rather than advertise. This has definitely caused me to miss out on "first-mover advantage". However, at this point I'm convinced the project is world-class so I feel compelled to share.
The name is stupid.
That's not a question, but I'll take it anyway. The name refers to the phrase "too many cooks in the kitchen", which I feel is a good metaphor for all the ways things can go wrong in a multithreaded, asynchronous system. Blocking, mutex contention, cache thrashing, and false sharing can all kill your performance, in the same way as two cooks trying to use the same knife. TooManyCooks's structured concurrency primitives and lock-free internals let you ensure that your cooks get the food out the door on time, even under dynamically changing, complex workloads.
Will this support Sender/Receiver?
Yes, I plan to make it S/R compatible. It already supports core concepts such as scheduler affinity so I expect this will not be a heavy lift.
Are C++20 coroutines ready for prime time?
In my opinion, there were 4 major blockers to coroutine usability. TooManyCooks offers solutions for all of them:
- Compiler implementation correctness - This is largely solved.
- Library maturity - TooManyCooks aims to solve this.
- HALO - Clang's attributes are the only implementation that actually works. TooManyCooks fully supports this, and it applies consistently (docs) (example) when the prerequisites are met.
- Debugger integration - LLDB has recently merged support for SyntheticFrameProviders which allow reconstructing the async backtrace in the debugger. GDB also offers a Frame Filter API with similar capabilities. This is an area of active development, but I plan to release a working prototype soon.
59
u/CJWilliams10 Feb 03 '26
Hi u/trailing_zero_count, author of libfork here π, huge congrats on releasing this, I've been watching TMC for a while and it's looking very polished now. I look forward to having a proper look at your numa aware work stealing bits and scheduler, looks like some nice work there.
I'm working on V4 of libfork, hopefully the templates and arcane syntax will be improved π (and it might even get faster).
Congrats again on the release!
11
u/Awia00 Feb 03 '26
Will you release it on vcpkg at some point?
5
u/elkoe Feb 03 '26
For embedded (esp32) I played with coroutines a bit. A library with little overhead offering developer convenience would be interesting. I use asio already for network and basic event loop. I was trying coroutines for async handling of slow protocols, to avoid busy waiting I want to do multiple steps of a transaction, suspending the task in between while an external driver finishes work. Coroutines allow writing synchronous steps instead of a state machine.
What are reasons this would not be suitable for embedded and constrained environments?
3
u/Flimsy_Complaint490 Feb 04 '26
i remember finding this a few months ago in a random coroutine thread. i was using asio but i found its io uring implementation for networking lacking and i needed access to oob data to implement offloads anyway, so i thought i may as well as use this since the networking part of asio was no longer useful for me. Migrating off asio was pretty trivial and it just worked and the extra goodies like hwloc and priorities were pretty useful. only thing im missing is timers but thats what the asio integration is for.Β
do strongly recommend. And in general, im quite happy coroutines are finally becoming mainstream in cpp land.Β
13
u/scrumplesplunge Feb 03 '26
I didn't know about clang's
coro_await_elidableattribute, that's neat. Do you know if there is any existing effort to add similar attributes for gcc and msvc?2
u/thisismyfavoritename Feb 04 '26
your project looks very interesting. As someone with a codebase running on ASIO coroutines and relying on multiple of its async primitives (strands, channels, awaitable operators), do you have a migration guide?
Also if the project is mostly leveraging coroutines for I/O (through ASIO), will there be any benefit since it seems like the I/O work must be completed on ASIO's executor.
Would also like to see an async promise / one shot channel in your async primitives
11
u/germandiago Feb 04 '26
This is the most amazing work for coroutines I have ever seen in C++. Congratulations.
18
u/more_exercise Lazy Hobbyist Feb 03 '26
Also one of the more surreal Adult Swim spots
6
u/tartaruga232 MSVC user, r/cpp_modules Feb 04 '26
Congrats on choosing the BSL license! I also love the name.
1
u/current_thread Feb 05 '26
Just to make sure, is it Linux only, or does it support Windows as well? I'll definitely check it out for my game engine, it looks exactly like what I've been looking for.
A killer feature for me would be if this were integrated with P2300
1
u/GPSProlapse Feb 06 '26
Sorry, maybe it's just me being dummy, but how does one use it in a multi-dll project? I don't see any sort of export attributes in the repo
Besides that, don't call it a header-only library when it requires the impl macro hack
1
u/Adequat91 Feb 06 '26
You mentioned Taskflow ("it competes directly with"), but one of the main interests of Taskflow is its very intuitive ability to build task graphs. I don't see this capability with TMC. Can you comment?
1
u/Maleficent_Hair_7971 Feb 18 '26
How does it interop with asio existing completion tokens are they composable. For example asio has tokens like cancel_after making async tasks easily cancellable. Or other ones like append, prepend, etc. I saw the cancellation example for the timeout but wondered if it has support for working with asio tokens in general. Great library btw.
4
u/dexter2011412 Feb 04 '26
How do I get good enough to make something like this?
I genuinely do not understand how people are so smart and how I can catch up to them.
2
u/[deleted] Feb 04 '26
Omg thank you! I was going to implement tasks from scratch for my server but this is going to save me so much time. Really it was for learning experience, but Iβm busy af and need to roll this out.
2
u/def-pri-pub Feb 04 '26
Too Many Cooks!
(slightly NSFW, watch it on a lunch break)
2
u/IlPresidente995 May 25 '26
I did write a Key Value storage around Too Many Cooks in conjuction with `io_uring` and it's been great
https://fede-vaccaro.github.io/hedgedb.github.io/
https://github.com/fede-vaccaro/HedgeDB
-1
u/VinnieFalco wg21.org | corosio.org Feb 03 '26
Comparison: Capy vs. TooManyCooks
https://develop.capy.cpp.al/capy/why-not-tmc.html
-3
u/Lurker_enesimo Feb 05 '26
Keep framework bs away from c/c++ ecosystem, thanks.
0
u/[deleted] Feb 03 '26
[deleted] β view deleted comment