|
|
|
Show HN: Writing a C++20M:N Scheduler from Scratch (EBR, Work-Stealing)
(github.com)
|
|
18 points
by lixiasky
128 days ago
|
|
tiny_coro is a lightweight, educational M:N asynchronous runtime written from scratch using C++20 coroutines. It's designed to strip away the complexity of industrial libraries (like Seastar or Folly) to show the core mechanics clearly. Key Technical Features: M:N Scheduling: Maps M coroutines to N kernel threads (Work-Stealing via Chase-Lev deque). Memory Safety: Implements EBR (Epoch-Based Reclamation) to manage memory safely in lock-free structures without GC. Visualizations: I used Manim (the engine behind 3Blue1Brown) to create animations showing exactly how tasks are stolen and executed. Why I built it: To bridge the gap between "using coroutines" and "understanding the runtime." The code is kept minimal (~1k LOC core) so it can be read in a weekend. |
|
Since old Fortran does not have function pointers I used to wonder how does one write gradient descent routine for a user defined function in Fortran.
In other languages, one typically passes the user defined function and it's gradient to the routine as callbacks, so that the routine can call them at will.
It was a moment of great amusement and joy (grinning like a silly kid) when I learned how Fortran did it. It was through a pared down form of a coroutine. Inversion of control.
The gradient descent routine would effectively suspend and 'return' with a flag indicating that it needs the user defined function computed, or the gradient function computed.
These are computed outside and then the routine is called again with these recently computed values. At this point it resumes from where it had yielded. Pretty neat.
The routine's return value indicated whether it is done, or that it is suspended and expects to be called again to resume.
Now I don't remember if the suspended state was stored transparently by the run time, or explicitly in the arguments of the routine (pass by reference, I think), or whether the local, routine specific 'program counter' was managed explicitly or transparently by the compiler and run time.