A key feature of C++20 is co-routines. I’ve spent lots of time using python coroutines, greenlets and SystemC threading. Co-routines should allow a general model of event driven asynchronous programming.

However, in C++ they are not easy at all…

These references are critical:

Compiled with these compilers, and generated coroutines code..only tested on WSL Ubuntu 20.04.

  • WSL Ubuntu: needs g++-11 (Ubuntu 11.1.0-1ubuntu1~20.04)
  • Windows msys2: g++.exe (Rev5, Built by MSYS2 project) 10.2.0
  • riscv-none-embed-g++ (xPack GNU RISC-V Embedded GCC, 64-bit) 10.1.0

The core ideas:

  • Language support is a framework - not something that is easily usable standalone.
  • Operators (co_await, co_return, co_yield) are defined that can do the architecture dependent code generation of context storage and switching.
    • These then hook into support classes to do the remaining work of managing higher level concepts of tasks/promises/futures and schedulers.
    • They are overidable.
  • There are lots of magic rules concepts to identify support classes. (Tasks, Awaitable, Promise etc)
  • The execution context is still a work in progress. The folly library implements some for co-routines, rchen shows you can link to other execution environments via a plain c callback. You can also roll-your-own.
  • A co-routine does not have an associated stack. It does have the need for heap storage of context. There are stack usage implications related to the lifespan of the coroutine context.

Other Ideas

  • Heap allocation can be avoided?
  • Asymmetric vs symmetric transfer. Symmetric allows context change a bit like return chaining - which is needed to save from potential stack overflow for iterative co-routine invocation.
  • Raw C pointers can be created from co-routine handles. These can be shared with legacy schedulers.

Initial attempts at using the feature: