The server runs an async accept on a thread with basically the following code: boost::asio::ip::tcp::endpoint endpoint = boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), iPort); acceptor.open(endpoint.protocol()); acceptor.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true)); acceptor.bind(endpoint); acceptor.listen(); TcpSessionPtr connection(new TcpSession(io_service)); acceptor.async_accept(connection->getSocket(), boost::bind(&BoostTcpSocket::handleAccept, this, connection, boost::asio::placeholders::error));
Do you mean that the above code runs not in io_service::run() thread? When does this thread exit, just after it executes the above code?
So, it sounds reasonable that if the application ends an async accept operation may be pending. Now, how could I cancel a pending operation? Is there another way to solve the problem?
You can close() your acceptor, but its destructor closes it anyway.