public inbox for pthreads-win32@sourceware.org
 help / color / mirror / Atom feed
From: "Kevin D. Clark" <kclark@cabletron.com>
To: Pthreads Developers List <pthreads-win32@sourceware.cygnus.com>
Subject: Re: C++ cleanup handler execution
Date: Fri, 14 Jan 2000 08:36:00 -0000	[thread overview]
Message-ID: <14463.20741.97491.269465@cabletron.com> (raw)
In-Reply-To: <14463.20160.891089.94040@cabletron.com>

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;
}


  parent reply	other threads:[~2000-01-14  8:36 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <000601bf578e$3c89a750$208e08d0@fflaptop>
2000-01-05 17:35 ` Ross Johnson
2000-01-06 14:11   ` Kevin D. Clark
2000-01-06 18:17     ` Ross Johnson
2000-01-14  8:19       ` Kevin D. Clark
2000-01-14  8:27         ` Kevin D. Clark
2000-01-14  8:30           ` Kevin D. Clark
2000-01-14  8:36           ` Kevin D. Clark [this message]
2000-02-02 10:08             ` pthread_cancel() problems Kevin D. Clark
2000-06-14 18:06         ` Question about condition variable Taci Ãœlker
     [not found]           ` <3949054F.3127@surfnshop.com>
2000-06-16  7:33             ` Taci Ãœlker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=14463.20741.97491.269465@cabletron.com \
    --to=kclark@cabletron.com \
    --cc=pthreads-win32@sourceware.cygnus.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).