Boost asio - stopping io_service

Mark Nelson picture Mark Nelson · Jan 26, 2011 · Viewed 23.7k times · Source

I'm using boost::asio to do some very basic UDP packet collection. The io_service object is instantiated in a worker thread, and io_service.run() is called from inside that thread. My problem is getting io_service.run() to return when I am done collecting packets.

I'm not clear on what methods of io_service can be called from other threads when it comes time to stop my worker thread. I have a reference to the io_service object, and from a different thread I make this call:

ios.dispatch( boost::bind( &udp_server::handle_kill, this ) );

In my udp_server class, the handler for that function cancels the pending work from a single boost::asio::ip::udp::socket and a single boost::asio::deadline_timer object. Both have pending async work to do. At that point I call ios.stop():

void udp_server::handle_kill()
{
    m_socket.cancel();
    m_timer.cancel();
    m_ios.stop();
}

With no work pending, I expect at this point that my call to ios.run() should return - but this does not happen.

So why does it not return? The most likely explanation to me is that I shouldn't be calling io_service::dispatch() from another thread. But the dispatch() method kind of seems like it was built to do just that - dispatch a function call in the thread that io_service::run() is working in. And it seems to do just that.

So this leaves me with a few related questions:

  1. Am I using io_service::dispatch() correctly?
  2. If all tasks are canceled, is there any reason that io_service::run() should not return?
  3. socket::upd::cancel() doesn't seem to be the right way to close a socket and abort all work. What is the right way?

asio is behaving pretty well for me, but I need to get a better understanding of this bit of architecture.

More data

socket::udp::cancel() is apparently an unsupported operation on an open socket under Win32 - so this operation fails by throwing an exception - which does in fact cause an exit from io_service::run(), but definitely not the desired exit.

socket::udp::close() doesn't seem to cancel the pending async_receive_from() task, so calling it instead of socket::udp::cancel() seems to leave the thread somewhere inside io_service::run().

Answer

Sam Miller picture Sam Miller · Jan 26, 2011

Invoking io_service::stop from another thread is safe, this is well described in the documentation

Thread Safety

Distinct objects: Safe.

Shared objects: Safe, with the exception that calling reset() while there are unfinished run(), run_one(), poll() or poll_one() calls results in undefined behaviour.

as the comments to your question indicate, you really need to boil this down to a reproducible example.