Subj : Re: Threading and Timeouts To : comp.programming.threads From : Giancarlo Niccolai Date : Wed Jul 20 2005 02:58 pm Marcin 'Qrczak' Kowalczyk wrote: > Giancarlo Niccolai writes: > >> Actually, I found that blocking i/o with select or poll works fine. >> Once select returns signaling that a socket can be read/written, >> a single read/write operation is GUARANTEED not to block; > > Except when another processes reads it in the meantime, Of course. Select is meant for single process operation. However, it guarantees that the NEXT operation on a file descriptor (regardless the underlying physical media) won't block, and ... if the NEXT operation is performed in another thread or process... well, that's YOUR problem ;-) [And IMHO a BIG problem of yours, as sharing a socket/fd among two processes or threads without coordination is, to say the least, awful design, and usually, totally meaningless] > and except > that Linux has a bug where certain socket activities trigger select > to return but there is nothing to read. May be, but read() won't block and must return immediately 0, or Linux is not posix compliant. Which may be, but I doubt a so relevant and important non-compliance can survive a single sub release. > > And of course with blocking sockets you have to select before each > read, and with non-blocking ones you only select when read fails with > EAGAIN. This is a way, but it is -at least- inefficient, as select() will perform, in much more efficient and CPU effective way, the same thing for you. No need to decide which sockets must be waited on select by checking their EAGAIN status; let select do that for you. > > select is primarily designed for non-blocking I/O. Where did you get this information? As you can see from this (as well as many other authoritative sources): http://seth.positivism.org/man.cgi/2/select_tut (Linux programmer manual) Select is applied to (blocking) pipes as i.e. stdin, without the need to change it to nonblocking. I can't see anything in the standards nor in relevant documentation suggesting that selected sockets/file descriptors should be non-blocking; OTOH, the purpose of select is to BLOCK your process until there's data to be processed, so... Bests Giancarlo Niccolai. .