|
-
April 21st, 2009, 09:36 PM
#1
select in one thread, closesocket in another thread
I got a quick question: if a thread is waiting in a select(...) and another thread calls closesocket, will the select(...) returns a error?
-
April 21st, 2009, 10:41 PM
#2
Re: select in one thread, closesocket in another thread
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
ahoodin
To keep the plot moving, that's why.

-
April 22nd, 2009, 11:31 AM
#3
Re: select in one thread, closesocket in another thread
 Originally Posted by ahoodin
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.
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
-
April 22nd, 2009, 12:59 PM
#4
Re: select in one thread, closesocket in another thread
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).
-
April 22nd, 2009, 03:28 PM
#5
Re: select in one thread, closesocket in another 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.
ahoodin
To keep the plot moving, that's why.

-
April 22nd, 2009, 03:36 PM
#6
Re: select in one thread, closesocket in another thread
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.
-
May 1st, 2012, 02:17 PM
#7
Re: select in one thread, closesocket in another thread
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?
error C2146a : syntax error : nebulizer stained in the tower floppy apple rider. Go rubble in flee smite. Bleeble snip snip.
Documentation says: error C2146a - This means there is an error somewhere in the course of human endeavor. Fix in the usual way.
-
May 1st, 2012, 06:45 PM
#8
Re: select in one thread, closesocket in another thread
 Originally Posted by paradoxresolved
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
-
May 2nd, 2012, 03:01 PM
#9
Re: select in one thread, closesocket in another thread
Fair enough. It just seemed like an almost identical question. Thanks for your help!
error C2146a : syntax error : nebulizer stained in the tower floppy apple rider. Go rubble in flee smite. Bleeble snip snip.
Documentation says: error C2146a - This means there is an error somewhere in the course of human endeavor. Fix in the usual way.
-
May 2nd, 2012, 03:19 PM
#10
Re: select in one thread, closesocket in another thread
This Zombie thread has been harvested.
ahoodin
To keep the plot moving, that's why.

Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|