...

/

Implementing a Rudimentary Task Type (Part 1)

Implementing a Rudimentary Task Type (Part 1)

Learn how to implement an asynchronous task type that can be returned from coroutines using C++ and supports continuations.

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:

Press + to interact
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 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 ...