tags:

views:

124

answers:

2

So I need to close a particular connection, 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?

A: 

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.

pajton
A: 

See the answer I got to nearly the same question:

http://stackoverflow.com/questions/2486335/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.

selbie