Coroutines
The integration of coroutines into C++20 ushers in a new paradigm for asynchronous programming that’s more readable and intuitive. By allowing functions to be suspended and resumed, coroutines offer an alternative to the callback-heavy style often seen in asynchronous C++ code. While transformative in its own right, this evolution also provides fresh, innovative ways to interact with the venerable STL. Examining the interaction of coroutines with STL algorithms and data structures reveals how they simplify asynchronous operations.
Understanding coroutines – a refresher
A coroutine is a generalization of a function. While a traditional function runs to completion and then returns, a coroutine can be paused at specific points, returning control to the caller and then later resuming from where it left off. Three primary keywords are vital to understanding coroutines in C++: co_await
, co_return
, and co_yield
:
co_await
: Suspends the current coroutine...