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: pthread_cancel() problems
Date: Wed, 02 Feb 2000 10:08:00 -0000	[thread overview]
Message-ID: <14488.29431.528635.717936@cabletron.com> (raw)
In-Reply-To: <14463.20741.97491.269465@cabletron.com>

Hi,

Is anybody else successfully using pthread_cancel() with this library?

I can't seem to get it to work, and I haven't been able to track down
the cause of the problem yet.  I'm not using asynchronous cancelation
either.

At the end of this mail I will include another test program that
shows off this problem.  If the program ran correctly then you'd see a
stream of increasing numbers on your screen.  However, since the
cleanup routine (associated with pthread_cleanup_push()) is never
called, the counter remains at zero.

So I strongly suspect that there's a bug in the library here.  I just
wish I could find it.

--kevin

PS  The program works as expected on a Solaris 2.6 box.


/*  Compiling this program as a C or C++ program doesn't seem to have
 *  an effect on the fact that calling pthread_cancel() doesn't cause
 *  the canceledRoutine() to be invoked.
 *
 *  Environment:  VC++ 6.x, NT 4.x, service pack 5
 *                Pthreads-win32 snapshot 1999-11-02
 */

#include <stdio.h>
#include <errno.h>
#ifdef __unix
#include <unistd.h>
#endif

#include <pthread.h>

void *canceledRoutine(void *);
static volatile int counter = 0; /* volatile thrown in out of desperation */
pthread_t canceledThread;

pthread_mutex_t mu = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cv = PTHREAD_COND_INITIALIZER;
static int okToCancel = 0;

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<100; i++) {
    /*    fprintf(stderr, "iteration %d\n", i); */
    pthread_create(&canceledThread, &tattr, canceledRoutine, (void *)i);

    pthread_mutex_lock(&mu);
    while (! okToCancel) {
      int result;
      result = pthread_cond_wait(&cv, &mu);
      if (result == EINTR) {
        fprintf(stderr, "%s:%d: interupted!\n", __FILE__, __LINE__);
      }
    }
    pthread_mutex_unlock(&mu);

    /* at this point we know that the other thread has pushed a cleanup
       handler onto its stack */
    pthread_cancel(canceledThread);

    okToCancel = 0;
    
    fprintf(stderr, "counter is %d\n", counter);
  }
  
  return 0;
}

void cancelCleanup(void *arg)
{
  int i = (int)arg;
  counter++;
  fprintf(stderr, "%d -- Hey, I've been canceled!\n", i);
}


void *
canceledRoutine(void *arg)
{
  int i = (int)arg;
  int ignored;
  
  /*
   *  My understanding is that the default is for deferred
   *  cancelation, but I'll do this anyways here just to be absolutely
   *  sure.
   */
  if (pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &ignored) != 0) {
    fprintf(stderr, "problem with pthread_setcanceltype()\n";
  }
  
  pthread_cleanup_push(cancelCleanup, (void *)i);
  pthread_mutex_lock(&mu);
  okToCancel = 1;
  pthread_mutex_unlock(&mu);
  pthread_cond_signal(&cv);
  
  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.
     *  Perhaps this needs to be documented?
     */
    Sleep(1000);
#endif
  }
  
  pthread_cleanup_pop(0);
  
  return 0;
}


  reply	other threads:[~2000-02-02 10:08 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 ` C++ cleanup handler execution 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
2000-02-02 10:08             ` Kevin D. Clark [this message]
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=14488.29431.528635.717936@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).