This is the mail archive of the pthreads-win32@sourceware.cygnus.com mailing list for the pthreas-win32 project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: C++ cleanup handler execution



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 <unistd.h>
#endif
#include <pthread.h>
#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;
}



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]