Boost logo

Boost :

Subject: [boost] library to support async/await pattern
From: Valentin Milea (valentin.milea_at_[hidden])
Date: 2013-04-18 11:30:37

Is there interest for a coroutine-based library to make asynchronous APIs
easier to deal with?

Typically, for each task there is cruft: chaining callbacks, managing
intermediate state and error codes (since exceptions don't fit this model).
The code flow get inverted and becomes difficult to follow.

Recent versions of F# and C# solve this problem. They implement an await
operator that effectively suspends the executing method until a task
completes. The compiler takes care of transforming subsequent code into a
continuation. Everything runs on the main thread, with asynchronous methods
spending most time awaiting. N3328 proposes resumable functions of this
kind in C++.

For an immediate solution we could leverage the Boost.Context/Coroutine
library. The resulting code may look like this:

  try {
    task = do_a_async(...)

    // yield until task done
  } catch (const some_exception& e) {
     // exceptions arrive in awaiting context

  // normal code flow
  for (auto& task : tasks1) {

  taskAny = await_any(tasks2);


There needs to be a representation for Awaitable tasks (similar to
std::future but non-blocking). The other requirement is to have a Scheduler
(run loop) in order to weave between coroutines.

- normal code flow: plain conditionals, loops, exceptions, RAII
- algorithm state tracked on coroutine stack
- async tasks are composable
- any async API can be wrapped

- must wrap async APIs (e.g. Boost.Asio)
- needs std::exception_ptr to dispatch exceptions
- stackful coroutines are sometimes difficult to debug

I wrote an open-source library that does this:

It's far from Boost style but the concept looks sane. For a comparison
between async patterns please see:

Making a Boost version would involve serious redesign. So is this worth


Boost list run by bdawes at, gregod at, cpdaniel at, john at