[GD-Linux] Cancelling blocking recv calls on Linux
Brought to you by:
vexxed72
From: Hal A. <ha...@cl...> - 2002-05-10 11:27:51
|
I wonder if anybody else has run into this and come up with a satisfactory solution. Part of my app uses TCP with blocking BSD sockets. Each "accept"ed connection has its own thread which simply recv's data and pumps it into the main thread. This all works fine and dandy on Linux and Windows. The problem comes when it comes to disconnecting the socket. The worker thread will generally be blocked at the recv call and so the main thread has to somehow overcome this - under WinSock1.1 a simple combination of shutdown( fd_, RD_BOTH ); // fd_ is the file descriptor of the socket. WSACancelBlockingCall(); // (this is Winsock 1.1 so this call is fine). Will cause the recv to immediately fail and the worker thread to terminate and so the call to closesocket( fd_ ) doesn't lock and everything is great. Under linux I use shutdown( fd_, SHUT_RDWR ) followed by close( fd_ ). This close then locks - the recv call never returns. I've tried: Setting the socket to non-blocking just before the close (i.e. it will already be involved in a recv call). My current solution is to use pthread_cancel to cancel the recv thread. This terminates the recv and the close then works fine. Anybody got anything better or is this infact the correct way to use blocking sockets with threads? |