Boost logo

Boost Users :

Subject: [Boost-users] Boost::Asio - blocking read on socket does not disconnect when socket closed from other thread
From: Nick Aschberger (nick.aschberger_at_[hidden])
Date: 2010-01-24 22:30:48


Hi Folks,

Boost version is 1.36, OS is linux rhel-4.

I have an application with two threads. The secondary thread is a worker
thread
that manages a boost::asio tcp/ip socket.

The worker thread performs blocking reads as per:

try {
         boost::system::error_code error;
         boost::asio::read(mysocket,
                           boost::asio::buffer(inputbuffer, size_to_read),
                           boost::asio::transfer_all(),
                           error);
         if(error) { throw boost::system::system_error(error); }
     }
     catch(boost::system::system_error se){
         // log error here
         return false;
     }

In the main application thread, in response to the user choosing to
close the
connection, I do:
mysocket.cancel();
mysocket.close();

I would expect the worker thread to return from the blocking
boost::asio::read
call, with an error.
This occurs on windows, but does not happen on the linux build of my app -
it never returns from the above call.

Why does the second thread never return? What am I doing wrong?

I have considered:
- implementing a timeout - but I don't want to move to async sockets.
- It seems SO_RCVTIMEO, is wrapped by boost::asio::read, so no hope there.
- I could kill the thread but that means it isn't cleaned up properly, so
   that is no good.

What am I doing wrong?

cheers

Nick Aschberger


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