Subject: pthreads asynchronous cancelability is broken
To: None <tech-userlevel@netbsd.org>
From: None <sigsegv@rambler.ru>
List: tech-userlevel
Date: 09/21/2004 04:04:04
I am not 100% sure, but it looks like pthreads asynchronous 
cancelability is broken on NetBSD-2.x
For example:

/* each thread increments a particular 64-bit interger */
volatile unsigned long long tcount[NTHREADS];

 /*
** Funct : tightloop()
** Param1: void *arg, array subscript number is passed through this pointer
** Retval: void *
** Descr : Each thread executes this function, which runs a tight loop,
** incrementing a seperate 64-bit integer as fast as possible. On a 
system with
** multiple CPUs, many threads could be running in parallel. The 
function never
** terminates, but instead the main thread should cancel each thread 
which is
** executing this function, when the time is up.
*/
void *tightloop(void *arg) {

  if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
    fprintf(stderr, "Error: pthread_setcancelstate\n");
  if(pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL) != 0)
    fprintf(stderr, "Error: pthread_setcanceltype\n");

  while(1) {
    tcount[*(int *)arg]++;
    /*pthread_testcancel();*/ /* this should not be needed with 
PTHREAD_CANCEL_ASYNCHRONOUS */
  }
}

calling pthread_cancel() to cancel a thread executing this function 
returns OK, but the thread is never canceled, it keeps looping for ever.

Has anyone noticed this before?

By the way, I tested my code on Solaris, FreeBSD and Debian Linux, and 
it works perfectly on all three, only NetBSD-2.x and Suse Linux 9 seems 
to have the problem.