Subj : Re: pthreads: cancelling thread blocked on berkeley socket accept() To : comp.programming.threads From : David Hopwood Date : Mon Sep 19 2005 05:07 pm 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. -- David Hopwood .