Subj : Re: pthread_cancel To : comp.programming.threads From : doug Date : Mon May 09 2005 09:08 am "Greg Law" wrote in message news:2l81pnF9h89tU1@uni-berlin.de... > Hi, > > I am calling pthread_cancel on a thread that has canceling enabled, of > deferred type. The thread I am canceling never makes any calls to > testcancel, or waits on any condition variables. So it surprises me that > the cancelation has any effect at all. That is, in the following program > the thread I've created is terminated (to my surprise). I guess I've not > understood correctly when threads may be canceled? > > Cheers, > > Greg > > > #include > #include > > void > Start (void *a) { > int tmp; > int r = pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, &tmp); > assert (!r); > r = pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, &tmp); > assert (!r); > > while (1) { > printf ("%d\n", tmp++); > } > } > > int > main (void) { > int r = 0; > pthread_t p; > > r = pthread_create (&p, NULL, (void*)Start, 0); > printf ("%d: %ld\n", r, p); > > r = pthread_cancel (p); > printf ("back from cancel (%d)\n", r); > while (1); > } If I remember correctly, I think POSIX states that certain system calls (read, write, etc.) are cancellation points. So the printf() in your Start() routine would be a cancellation point. What version of OS & threading lib are you using? I know that until recently, LinuxThreads hand't actually implemented this, though (e.g. see manpage for pthread_cancel on an older Fedora Core 3 system). It may have been fixed recently, or may be correct in NPTL. .