Get the latest tech news
Hacking Coroutines into C
12.7.2025 A while ago, I was part of a team developing embedded software. The software was deeply rooted in state machines - dozens of them—spread across multiple functions.
However, in our coroutine system, we can't rely on this built-in stack mechanism because we're pausing execution at arbitrary points and resuming later—potentially from a different part of the program loop. This pattern is reminiscent of a classic C trick known as Duff's Device —a technique that uses a switch statement combined with loop unrolling to implement a form of coroutine or co-operative multitasking. This is an efficient way to avoid heap allocations, but it introduces a major caveat: if the coroutine is canceled or finishes before it’s signaled, and it does not clean up after itself, the list would contain a dangling pointer — a reference to a context that no longer exists.
Or read this on Hacker News