简体   繁体   中英

boost asio - io_service don't wait connection into threads

I want to create a server async with multi threads. When I create a thread_group and waiting for some connections in asynchronous way. My program don't wait and termine immediatly.

void Server::configServer() {
    _ip = boost::asio::ip::address_v4::from_string("127.0.0.1");
    boost::asio::ip::tcp::resolver resolver(_io_service);
    _endpoint = *resolver.resolve({tcp::v4(), _port});
    std::cout << "Server address: " << _ip.to_string() << ":" << _port << std::endl;

    _acceptor.close();
    _acceptor.open(_endpoint.protocol());
    _acceptor.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
    _acceptor.bind(_endpoint);
    _acceptor.listen();
    for (int i = 0; i < 8; ++i) {
        _threads.create_thread(boost::bind(&boost::asio::io_service::run, &_io_service));
    }
    _threads.join_all();
    std::cout << "Server is set up" << std::endl;
    run();
}

void Server::run() {
    Connection::pointer newConnection = Connection::create(_acceptor.get_io_service());
    std::cout << "Server is running" << std::endl;

    _acceptor.async_accept(newConnection->socket(),
        boost::bind(&Server::handleAccept, this, newConnection,
        boost::asio::placeholders::error));
}

void Server::handleAccept(Connection::pointer newConnection, const boost::system::error_code& error) {
    if (!error) {
        std::cout << "Reçu un client!" << std::endl;
        newConnection->start();
        run();
    }
}

Can you tell me what am I doing wrong ?

run works as long as there are any pending tasks/handlers to be processed.

In your case you started run , then first async_ method was called. So run ends immediately due to no handlers to be called.

You should init some asynchronous task, then invoke run or use object called work guard . You didn't specify which version of Boost you use, but there are two options:

  • in olders io_service / io_context::work ( ref )
  • current, executor_work_guard ( ref )

In your class you can add executor_work_guard as additional member variable:

class Server {
    boost::asio::io_context _io_service;
    boost::asio::executor_work_guard<boost::asio::io_context::executor_type> guard;

    Server() : ...., guard(boost::asio::make_work_guard(_io_service)) {

    }
};

with this approach, run doesn't return even if there are no handlers to be processed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM