I’m using a fairly simple boost::asio set-up, where I call io_service.run() from the main thread.
I have a tcp resolver, and use async resolve to look up an address.
When that look-up fails, I throw an exception inside the asynchronous callback.
I catch this exception outside the run() call, inside the main function. I then call stop() on my io_service instance (which is a global).
However, when main() returns, the program hangs. It turns out to be waiting for an exit_event_ that never comes from the resolver service.
I do not wish to hang on exit. Is there something I’m doing wrong? If so, what? I haven’t found much discussion about these things online.
I’m using boost 1.41.0 on Windows 7/64bit.
Try to use this trick (copied from io_service documentation) when you need to stop
io_service:The reason is simple (also from the documentation): call to
io_service::stop()will cause the io_service run() call to return as soon as possible, abandoning unfinished operations and without permitting ready handlers to be dispatched.So, calling
io_service::stop()is not enough if you need to dispatch all handlers.