Close socket and select()
So I need to close a particular conne开发者_如何转开发ction, but the problem is another thread is, at the same time, doing a select() which has the socket as one of the file descriptors it's watching.
Will the select() terminate gracefully, or will anything bad happen?
I am not 100% sure, but nothing bad should happen if you close the socket after the select()
started. select()
will return an error if you pass already closed socket to it at the beggining (see man page).
Also, if you use select()
to wait for data to read it may report this closed socket as ready for read (because read won't block on closed socket).
You may write a simple program and actually check what will happen on your system.
See the answer I got to nearly the same question:
Wake up thread blocked on accept() call
Short answer: have your select call wait on two file handles: the socket and the read-side of the pipe.
精彩评论