Boost logo

Boost Users :

Subject: [Boost-users] stop thread before socket destructor
From: Gianni Ambrosio (gianni.ambrosio_at_[hidden])
Date: 2011-04-27 11:17:44


Hi All,
I modified a boost example to implement a tcp server that's able to
accept clients on a thread. Unfortunately I get the following message:
"The I/O operation has been aborted because of either a thread exit or
an application request".

I realized that the problem is related to the fact the server destructor
is called before the handle_accept occurs.

Now, how could I stop the thread in the server destructor so that the
handle_accept is not called anymore?

Here is the test code I used:

typedef boost::shared_ptr<boost::asio::ip::tcp::socket> tcp_socket_ptr;

class stream_handler
{
public:
    stream_handler()
    : acceptor_(io_service_)
    {
       boost::asio::ip::tcp::endpoint endpoint =
boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 32123);
       acceptor_.open(endpoint.protocol());
       
acceptor_.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
       acceptor_.bind(endpoint);
       acceptor_.listen();

       new_socket = tcp_socket_ptr(new
boost::asio::ip::tcp::socket(io_service_));
       acceptor_.async_accept(*new_socket,
boost::bind(&stream_handler::handle_accept, this,
boost::asio::placeholders::error));
       thread_ =
boost::thread(boost::bind(&boost::asio::io_service::run, &io_service_));
    }

    ~stream_handler()
    {
       acceptor_.close();
    }

    void handle_accept(const boost::system::error_code& err)
    {
       if (err)
       {
          std::cout << err.message() << std::endl;
       }
       else
       {
          new_socket = tcp_socket_ptr(new
boost::asio::ip::tcp::socket(io_service_));
          acceptor_.async_accept(*new_socket,
boost::bind(&stream_handler::handle_accept, this,
boost::asio::placeholders::error));
       }
    }

private:
    io_service io_service_;
    tcp::acceptor acceptor_;
    boost::thread thread_;
    tcp_socket_ptr new_socket;
};

int _tmain(int argc, _TCHAR* argv[])
{
    stream_handler sh;
    return 0;
}

Regards
Gianni


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