Boost logo

Boost Users :

Subject: Re: [Boost-users] Using Coroutines in the Visitor Pattern (use-case: BGL event visitor)
From: Nat Goodspeed (nat_at_[hidden])
Date: 2016-01-18 19:25:37


On Mon, Jan 18, 2016 at 10:36 AM, alex <alexhighviz_at_[hidden]> wrote:

> To get Nat's method to work for me, I used the following:
>
> while (dijkstra_object) {
> dijkstra_object();
> }
>
> Where dijkstra_object is a boost::coroutines::asymmetric_coroutine<Vertex>::pull_type.
>
> It was somehow disappointing that I couldn't simply do:
>
> while (dijkstra_object() )
> { }

You could instead, as in Daniel's example program a couple posts back,
use a range 'for' over the dijkstra_object.

> I can see in the documentation that the constructor enters the coroutine-function, but it is not clear to me why. Would it not have been neater if this was avoided?

With a pull_type coroutine, the expectation is that every time it
suspends (until it exits), it has produced a value for its consumer.
If the coroutine constructor didn't enter the coroutine-function, the
first invocation would have to be a special case. Even if that were
desirable, how could you distinguish the case in which the coroutine
produces zero values?

That said, if you want finer-grained control over exactly when the
coroutine is entered, you might consider using execution_context
instead [0].

[0] http://www.boost.org/doc/libs/1_60_0/libs/context/doc/html/context/econtext.html


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net