mercredi 28 septembre 2016

Chaining asynchronous calls using boost::coroutines

I want to have chain of asynchronous calls using boost::coroutine.

I've started with the simplest solution.

My async methods are:

class C
{
    void function1(boost::coroutines::symmetric_coroutine<int>::call_type& coro, boost::coroutines::symmetric_coroutine<int>::yield_type& yield)
    {
        thread1 = std::thread([&](){ std::cout << "function1" << std::endl; yield(coro, 100); });

    }

    void function2(boost::coroutines::symmetric_coroutine<int>::call_type& coro, boost::coroutines::symmetric_coroutine<int>::yield_type& yield)
    {
        thread2 = std::thread([](){ std::cout << "function2" << std::endl;  });
    }

    void C::run()
    {
         boost::coroutines::symmetric_coroutine<int>::call_type coro_b(
            [&](boost::coroutines::symmetric_coroutine<int>::yield_type& yield)
            {
                 std::cout << "b entry" << std::endl;
                 function2(coro_b, yield);

                  yield();
                  std::cerr << "Never get this" << std::endl;

                   std::cout << yield.get() << std::endl;
            });

            boost::coroutines::symmetric_coroutine<int>::call_type coro_a(
                [&](boost::coroutines::symmetric_coroutine<int>::yield_type& yield)
            {
                std::cout << "a entry" << std::endl;

                 function1(coro_b, yield);
                 yield();

            });

        coro_a(2);
        coro_b(3);

        std::this_thread::sleep_for(std::chrono::minutes(1));

}


private:
    std::thread thread1;
    std::thread thread2;
};

int main()
{
    C c;
    c.run();

    return 0;
}

My output:

a entry

b entry

function2

function1

And I have an error:

main_tests: /usr/include/boost/coroutine/detail/symmetric_coroutine_impl.hpp:159: R* boost::coroutines::detail::symmetric_coroutine_impl<R>::yield_to_(Other*, typename Other::param_type*) [with Other = boost::coroutines::detail::symmetric_coroutine_impl<int>; R = int; typename Other::param_type = boost::coroutines::detail::parameters<int>]: Assertion is_running()' failed. The program has unexpectedly finished.

I just try to call coro_b when coro_a is finished (exactly when asynchronous function in coro_a is finished)....

Aucun commentaire:

Enregistrer un commentaire