Hi, coroutine listers!
The example is very awesome. but after reading though the source, i have
a question:
Is any limitation makes a push_coroutine must be caller and
pull_coroutine must be the coro?
This way, we have to yield out at the first line of coroutine function by
calling caller(), just like the example showed:
typedef coroutines::pull_coroutine<void> coro_pull;
typedef coroutines::pull_coroutine<void> coro_push;
auto asynchronous(F f) -> future
{
typedef promise CoroPromise;
CoroPromise coro_promise;
auto coro_future = coro_promise.get_future();
CurrentCoro current_coro =
{
make_shared(std::bind([f](CoroPromise &coro_promise,
coro_push &caller)
{
caller();
coro_stack.top().caller = &caller;
coro_promise.set_value(f());
}, std::move(coro_promise), placeholders::_1);
};
coro_stack.push(std::move(current_coro));
(*coro_stack.top().coro)();
coro_stack.pop();
return coro_future;
}
There is an extra call to caller() in coroutine function to jump out to
prepare coro stack.
Why not swap coro_pull and coro_push like this:
auto asynchronous(F f) -> future
{
typedef promise CoroPromise;
CoroPromise coro_promise;
auto coro_future = coro_promise.get_future();
CurrentCoro current_coro =
{
make_shared(std::bind([f](CoroPromise &coro_promise,
coro_pull &caller)
{
coro_stack.top().caller = &caller;
coro_promise.set_value(f());
}, std::move(coro_promise), placeholders::_1);
};
coro_stack.push(std::move(current_coro));
(*coro_stack.top().coro)();
coro_stack.pop();
return coro_future;
}
This still works on my machine, and has no extra "call to caller()",
push_coroutine is not invoked until we explicitly run it (while
pull_coroutine is invoked in constructor).
So, my question is, s there any bad effects by using coroutines in this
way?
Thanks in advance!