-
Book Overview & Buying
-
Table Of Contents
Software Architecture with C++ - Second Edition
By :
Threads are heavier-weight, OS-level components for concurrent execution, each with its own memory stack. In contrast, coroutines are functions that can suspend and resume execution. They enable cooperative multitasking and allow writing asynchronous code in a very similar manner to how you would write synchronous code.
Compared to writing asynchronous code with std::async, coroutines allow writing cleaner code that’s easier to understand and maintain. There’s no need to write callbacks anymore, and no need to deal with the verbosity of std::async with promises and futures. Coroutines, being a more lightweight alternative to threads, are ideal for I/O-bound tasks when thousands of such tasks can run on just a few threads. By suspending without blocking the underlying thread, they achieve greater efficiency and performance in asynchronous operations.
std::async based code usually incurs more overhead due to thread switching and waiting. In contrast...