Boost logo

Boost :

Subject: Re: [boost] [coroutine] new versions
From: Oliver Kowalke (oliver.kowalke_at_[hidden])
Date: 2012-10-11 07:39:46


> What I do is having operator bool () return true if the coroutine has not
> terminated and has data.

How does this indicate that the next call to coroutine<>::operator() would not return a value?

> Also, the coroutine-fn is called immediately when
> the coroutine is called, and not deferred to the first operator() call.

I don't get it - when will be the body of coroutine-fn entered?

coro_t c( fn);//?
or
c(); //?

In the current implementation the first call of coroutine<>::operator() enters the body of the coroutine-fn.

example with some additional arguments:

typedef coroutine< std::string(int) > coro1_t;
typedef coroutine< int(std::string) > coro2_t;

void fn( coro2_t & c){
   int x = c.get();
   c( textual_cast( x) );
   x = c.get();
   c( textual_cast( x) );
}

coro1_t c( fn);
c( 1); // fn() is entered here
std::string str = c.get();

int i = 0;
for(; c ; c( ++i)) {
      std::string s = c.get();
}

I think your forr-loop would not work .
After the second call to 'c( i++)' in the loop 'c.get()' returns a value ('two'). 'coroutine::operator bool()' will evaluate to true (because result is available).
If the next time c( ++i) is called the code returns from the last 'c( textual_cast( x) );' in coroutine-fn and terminates fn() - no data is returned.
Because the for-loop does not test 'coroutine::operator bool()' before
'c.get()' you get a fault.

Oliver


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