Implementing a Rudimentary Task Type (Part 1)
Explore the implementation of a rudimentary asynchronous task type using C++ coroutines. Understand how to manage return values, exceptions, and coroutine continuations without blocking, enabling efficient composition of asynchronous operations.
We'll cover the following...
Implementation
The task type we are about to implement is a type that can be returned from coroutines that represent asynchronous tasks. The task is something that a caller can wait for using co_await. The goal is to be able to write asynchronous application code that looks like this:
The standard library already provides a type that allows a function to return
an object that a caller can use for waiting on a result to be computed, namely std::future. We could potentially wrap std::future into something conforming to the awaitable interface. However, std::future does not support continuations, so whenever we try to get the value from a std::future, we block the current thread. In other words, there is no way to compose asynchronous operations without blocking when using std::future.
Another alternative would be to use std::experimental::future or a future type from the Boost library, which supports ...