Boost logo

Boost :

Subject: Re: [boost] library to support async/await pattern
From: Valentin Milea (valentin.milea_at_[hidden])
Date: 2013-04-19 08:36:49


I think Boost.Fiber tries to simulate thread semantics on top of
coroutines. So it has a very different goal.

CppAwait implements a task-based asynchronous pattern. Every asynchronous
operation is exposed as an abstract task. The focus is on being able to
compose tasks and to express complex async work in plain sequential code.

The closest C++ library I know of is Protothreads (
https://github.com/benhoyt/protothreads-cpp ). It's based on Duff's device
so very limited.

These .NET overviews cover the core concept well:
http://msdn.microsoft.com/en-us/library/vstudio/hh191443.aspx
http://msdn.microsoft.com/en-us/library/hh873175.aspx

And don't forget CppAwait examples for a quick intro.

On Thu, Apr 18, 2013 at 9:22 PM, Oliver Kowalke <oliver.kowalke_at_[hidden]>wrote:

> How does it compare to boost.fiber (github.com/olk/boost-fiber)?
>
>
> 2013/4/18 Valentin Milea <valentin.milea_at_[hidden]>
>
> > 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
> > task.await();
> > } catch (const some_exception& e) {
> > // exceptions arrive in awaiting context
> > }
> >
> > // normal code flow
> > for (auto& task : tasks1) {
> > task.await();
> > }
> >
> > taskAny = await_any(tasks2);
> > taskAny.await();
> >
> > ...
> >
> > 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.
> >
> > Benefits:
> > - normal code flow: plain conditionals, loops, exceptions, RAII
> > - algorithm state tracked on coroutine stack
> > - async tasks are composable
> > - any async API can be wrapped
> >
> > Cons:
> > - 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:
> > https://github.com/vmilea/CppAwait.
> >
> > It's far from Boost style but the concept looks sane. For a comparison
> > between async patterns please see:
> >
> https://github.com/vmilea/CppAwait/blob/master/Examples/ex_stockClient.cpp
> >
> > Making a Boost version would involve serious redesign. So is this worth
> > pursuing?
> >
> > Thanks!
> >
> > _______________________________________________
> > Unsubscribe & other changes:
> > http://lists.boost.org/mailman/listinfo.cgi/boost
> >
>
> _______________________________________________
> Unsubscribe & other changes:
> http://lists.boost.org/mailman/listinfo.cgi/boost
>


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk