Subj : Re: Killing threads To : comp.programming.threads From : Giancarlo Niccolai Date : Wed Aug 17 2005 01:45 pm David Schwartz wrote: > > "David Hopwood" wrote in message > news:NcwMe.13006$0u2.11716@fe2.news.blueyonder.co.uk... > >> There are any number of blocking system functions that can get hung for >> any number of reasons (because the POSIX API has not been systematically >> designed to allow all functionality to be accessed by calls that are >> non-blocking or support timeouts). So it isn't in general possible to >> avoid the possibility of a thread becoming hung -- but it is possible to >> avoid holding locks when calling functions that might hang, so that >> cancellation can be made reliable. > > There are very, very few functions that are cancellation safe that > also > can't be made non-blocking. > In other words, you have to chose: make all the I/O non blocking and manage timeouts by polling, or use the pthread_cancel() and pthread_cleanup_push() functions (or a combination of the two). What you have to do is to provide a mechanism that allows the threads: 1) never actually to stop, and periodically check for themselves if any critical condition/timeout requires immediate termination and/or 2) Cooperate with the rest of the process by freeing all the shared resources immediately before blocking in a wait operation, or using the pthread_cleanup_push to do this in case cancellation during wait is requested. This for every possible critical condition you many face (and in general for every I/O). Bests, Giancarlo Niccolai. .