Boost logo

Boost Users :

Subject: [Boost-users] [asio] Crash in io_service::run()
From: Germán Diago (germandiago_at_[hidden])
Date: 2010-08-15 14:58:04


Hello. I have a client application that accesses a server, both coded
with Boost.Asio.

The code in the client is like this:

Client::Client() :
                socket_(service_), work_(service_) {
}

void Client::connectWith(const std::string & hostname, int puerto,
                        const std::string & nombre, int equipo) {
        using namespace boost::asio::ip;

        tcp::resolver resolver(service_);
        tcp::resolver::query query(tcp::v4(), hostname, std::to_string(puerto));
        tcp::resolver::iterator endpoint_iterator = resolver.resolve(query),
endpoint_end;

        tcp::endpoint endpoint = *endpoint_iterator;
        socket_.async_connect(endpoint, boost::bind(&Cliente::handleConnect,
                        this, boost::asio::placeholders::error, ++endpoint_iterator));
        
        std::thread([&] {service_.run(); });
}

void Client::handleConnect(const boost::system::error_code & e,
                        boost::asio::ip::tcp::resolver::iterator endpoint_iterator)
{

        if (!e) {
        }
        else if (endpoint_iterator != boost::asio::ip::tcp::resolver::iterator())
        {
                socket_.close();
                boost::asio::ip::tcp::endpoint endpoint = *endpoint_iterator;
                socket_.async_connect(endpoint,
                boost::bind(&Cliente::handleConnect, this,
                boost::asio::placeholders::error, ++endpoint_iterator));
        }
        else {
                std::cerr << e.message() << std::endl;
        }
}

My application crashes when calling Client::connectWith when I run the
io_service::run()
function in another thread, which is what I exactly want to do.

If I run io_service::run() in the same thread, everything is ok.
Anyone knows why?

This is a GUI application, maybe it's a problem with gtkmm interaction
and threads?
Anyway, I would like how to handle to run io_service's run() in
another thread. 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