Implementing a rudimentary task type
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:
auto image = co_await load("image.jpg");
auto thumbnail = co_await resize(image, 100, 100);
co_await save(thumbnail, "thumbnail.jpg");
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 that would conform to the awaitable interface. However, std::future
does not support continuations, which means that 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...