I got a quick question: if a thread is waiting in a select(...) and another thread calls closesocket, will the select(...) returns a error?
Printable View
I got a quick question: if a thread is waiting in a select(...) and another thread calls closesocket, will the select(...) returns a error?
Yes the call to select will return -1 if there is a problem with one of the sockets.
Since you are using blocking sockets, you had best not close the socket from a seperate thread.
In non-blocking scenarios it is much easier to handle this gracefully, by simply setting the timeout on the select to zero.
Note the flow chart for select states.
http://publib.boulder.ibm.com/infoce...6xnonblock.htm
What has the former to do with the latter?
It does not matter whether the sockets passed to select() are blocking or nonblocking. The result of closing a socket would be identical: select() would return with an indication that there is something to be received on the socket and a subsequent recv() would return 0 to indicate that the socket is closed. select() does not return -1 here!
The described scenario is perfectly legal IMHO.
Regards,
Richard
I hvae to revise my statement: when select() returns, a subsequent call to recv() would return -1 (error indication) because the socket has locally been closed (by the other thread).
Well that can be especially bad depending on the way this lad has coded his app. If that happens, the process may exit. Personally I believe that one should not close the socket until communications are done. It is bad to just call close socket at any point in communications.
But what's the point of your statement? Assuming you have one thread only responsible for reading data from the socket (or several sockets, or you would not need select()) and another thread for some higher logic, then why not close the socket when the latter thread decides you're done with communication? The first thread gets to know the socket is no longer valid and terminates.
Would the situation be similar with connect() and closesocket() . . . specifically: if one thread closes a socket that another thread is attempting a connect() on, does the second thread continue to block with connect() or does it promptly return a -1? If so, what WSAGetLastError() would it result with?
Yes, the principle is the same, and I believe that the WSAGetLAstError will return WSAENOTSOCK
Incidentally, it probably would have been better to start a new thread that references this one, rather than awakening a zombie thread more than three years old.
Mike
Fair enough. :) It just seemed like an almost identical question. Thanks for your help! :)
This Zombie thread has been harvested.