From mboxrd@z Thu Jan 1 00:00:00 1970 From: "Kevin D. Clark" To: Pthreads Developers List Subject: Re: C++ cleanup handler execution Date: Fri, 14 Jan 2000 08:36:00 -0000 Message-id: <14463.20741.97491.269465@cabletron.com> References: <14453.4956.759854.712859@cabletron.com> <14463.19695.396800.542343@cabletron.com> <14463.20160.891089.94040@cabletron.com> X-SW-Source: 2000/msg00011.html Kevin D. Clark writes: > Sorry, in my previous email my test file got garbled. > My fault. Sorry, now I've made *three* mistakes. I've been up for too long... *This* is the correct version of my test file. The previous versions that I sent were faulty. Sorry. --kevin (time to take a nap!) // Compiling this file as a C++ program versus being a C program doesn't // seem to affect the fact that the cleanup handler doesn't seem to be called. #ifdef __unix #include #endif #include #include "stdio.h" static void *canceledRoutine(void *), *anotherRoutine(void *); static volatile int counter = 0; static pthread_t canceledThread, anotherThread; int main(int argc, char *argv[]) { int i = 0; pthread_attr_t tattr; pthread_attr_init(&tattr); pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED); for (i=0; i<1000; i++) { fprintf(stderr, "iteration %d\n", i); pthread_create(&canceledThread, &tattr, canceledRoutine, (void *)i); pthread_create(&anotherThread, 0, anotherRoutine, (void *)i); pthread_join(anotherThread, 0); fprintf(stderr, "counter is %d\n", counter); } // kludge #ifdef __unix sleep(5); #else Sleep(5000); #endif return 0; } static void cancelCleanup(void *arg) { int i = (int)arg; counter++; fprintf(stderr, "%d -- Hey, I've been canceled!\n", i); } static void * canceledRoutine(void *arg) { int i = (int)arg; pthread_cleanup_push(cancelCleanup, (void *)i); while(1) { #ifdef __unix // sleep() is defined to be a cancelation point sleep(1); #else // I'm not totally sure what is defined to be a cancelation point here. // This probably needs to be documented. Sleep(1000); #endif } pthread_cleanup_pop(0); return 0; } static void * anotherRoutine(void *arg) { int i = (int)arg; pthread_cancel(canceledThread); return 0; }