I am using boost::asio
to transfer data to & fro from client to server. I have a reader thread on client side to read data received on the socket on client side. Please note that I am using boost::asio::read
on client side & boost::asio::write
on server side.
Not using async_read
or async_write
. Everything works great.
However when I close my application, 2 out 10 times the app does not cleanly tear down or close properly. It gets hung while closing down The issue is the following:
My closing function gets called when destructors get called during my app's close down. Following is the code of the Close function:
socket.cancel();
socket.close();
boost::system::error_code ec;
socket.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
The problem is that the boost::asio::read
call does not return when it does not get any data & keeps waiting on it. This should be fine as long as I can cancel it. I am trying to do a socket.cancel
on it to cancel all read operations while exiting.
However, it doesn't seems to work. I read in some forums that socket.cancel
only cancels async_read
operations. Is it so ? Then what is the way to cancel a boost::asio::read` operation when my app needs to exit ?
SIGINT
is sent to kill the whole app when the UI app gets killed. It is only a problem when the client app is a console app. Anyways, thanks for this explanation – Stinkwood