Boost logo

Boost Users :

Subject: [Boost-users] [coroutine] await_emu example
From: bbxiong (bbxiong.xiao_at_[hidden])
Date: 2014-06-20 03:39:34


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<decltype(f())>
{
   typedef promise<decltype(f())> CoroPromise;
   CoroPromise coro_promise;
   auto coro_future = coro_promise.get_future();

   CurrentCoro current_coro =
   {
       make_shared<coro_pull>(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<decltype(f())>
{
   typedef promise<decltype(f())> CoroPromise;
   CoroPromise coro_promise;
   auto coro_future = coro_promise.get_future();

   CurrentCoro current_coro =
   {
       make_shared<coro_push>(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!



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