Subj : Re: pthreads: cancelling thread blocked on berkeley socket accept() To : comp.programming.threads From : David Hopwood Date : Mon Sep 19 2005 07:40 pm Sergei Organov wrote: > David Hopwood writes: >>Luke Dalessandro wrote: >> >>>I have a thread running on a listen()->accept()->spawn_job()->listen() >>>loop on a socket (unix/berkeley). I have the socket set to block on the >>>accept(). spawn_job() dispatches a thread to handle the incoming request. >>> >>>When the application is shut down, I want to cancel the thread and >>>close() the socket. Is it appropriate to call pthread_cancel() on a >>>blocked thread? If I call pthread_cancel(listener) and then >>>pthread_join(listener) from the main (parent) thread, am I guaranteed >>>that it is safe to close() the socket? >> >>You should set a shared 'shouldExit' flag and close() the socket from the >>main thread. The listening thread will get an error from the accept(), and >>can then exit when it sees that 'shouldExit' is set. > > I'm afraid async close() is asking for troubles. I'd suggest to call > shutdown() from main thread, then call close() when accept() returns > error. Yes, that's probably better. -- David Hopwood .