Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[boost asio interaction] Restarting an io_service #239

Open
niekbouman opened this issue Mar 4, 2020 · 1 comment
Open

[boost asio interaction] Restarting an io_service #239

niekbouman opened this issue Mar 4, 2020 · 1 comment

Comments

@niekbouman
Copy link
Contributor

niekbouman commented Mar 4, 2020

Hi,

I am using the Boost.Fiber <-> Boost.Asio coupling via the boost::fibers::asio::round_robin scheduler, and I am trying to relaunch an asio io_service, that has previously been stopped.

The problem is, that the scheduler (or fiber) hangs after the second execution of the .run() method.
In the code example below, the "this line is never reached\n" string is never printed.

#include <iostream>
#include <boost/asio/io_context.hpp>
#include "round_robin.hpp"
#include <boost/fiber/all.hpp>

using io_context_ptr = std::shared_ptr<boost::asio::io_service>;

void start(io_context_ptr io_ctx)
{

  boost::asio::system_timer t(*io_ctx);
  t.expires_after(std::chrono::seconds(1));
  t.async_wait(boost::fibers::asio::yield);

  io_ctx->stop();

}

int main()
{
    io_context_ptr io_ctx = std::make_shared<boost::asio::io_service>();
    boost::fibers::use_scheduling_algorithm<boost::fibers::asio::round_robin>(io_ctx);
    auto f = boost::fibers::fiber(start, io_ctx);
    io_ctx->run();
    f.join();

    std::cout << "foo\n";

    io_ctx->restart();
    auto f2 = boost::fibers::fiber(start, io_ctx);

    std::cout << "bar\n";
    
    io_ctx->run();
    std::cout << "this line is never reached\n";
    f2.join();

}
@niekbouman niekbouman changed the title [boost asio interactionRestarting [boost asio interaction] Restarting an io_service Mar 4, 2020
@jpcordovae
Copy link

if I remember well you have to reset your service before restart it (there is a restart function btw). I suggest to carefully read the documentation, sometimes functions names are misinterpreted or require a context not so evident.

https://stackoverflow.com/questions/15568100/confused-when-boostasioio-service-run-method-blocks-unblocks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants