[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: multiple pthread_cancel() problem

From: Pavan K Al
Subject: Re: multiple pthread_cancel() problem
Date: Mon, 11 Oct 2004 19:21:39 +0530

Hi Wolfram,

Thx for the reply.

>>> Here is the testcase to recreate the problem:
>>You omitted the testcase!

I did not omit the testcase. I attached the testcase, not
sure whether you received it or not. Here is the same
/* Compilation line: gcc -o server multiple_pthread_cancel.c -lpthread */

#include <setjmp.h>
#include <pthread.h>
#include <stdio.h>

pthread_t Thread2;

void *Thread3_StartRoutine(void *string) {

/* This Thread3 simply send cancel signal to Thread2 */

  int rc;
  while(1) {
    printf("\t\tT3: Thread3_StartRoutine: Sleep 3 secs\n");
    printf("\t\tT3: Thread3_StartRoutine: pthread_cancel(%d) ", Thread2);
    rc = pthread_cancel(Thread2);
    printf("-> %d\n", rc);

void jump_back(void* arg) {
  jmp_buf *jmp = (jmp_buf*)arg;
  printf("\tT2: start jumping\n");
  _longjmp(*jmp, 1);

void *Thread2_StartRoutine(void *string) {

/* This Thread2 simply blocks at getchar() and waits for cancel from Thread3
and handles it. After handling that cancel this Thread2 again goes back and

waits at getchar() and waits for another cancel signal. */

  int i=1;
  jmp_buf jmp;
  struct _pthread_cleanup_buffer _buffer;
  while(1) {
    _pthread_cleanup_push(&_buffer, jump_back, &jmp);
    if(!_setjmp (jmp)) {
      printf("\tT2: Thread2_StartRoutine: Blocking on getchar()\n");
      _pthread_cleanup_pop(&_buffer, 0);
    } else {
      printf("\tT2: Thread2_StartRoutine: Got a jump\n");
      /* For AIX we need to add the following line to make it work:
      /* For SOLARIS we need to add the following line to make it work:
         pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &__cstate);
         pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &__ctype);
      /* for Linux
      ???? What is the equivalent code ????

int main(int argc, char *argv[]) {

/* This main function creates Thread2 and Thread3 and does join on them */

  pthread_t Thread3;
  unsigned st;

  printf("T1: main: creating Thread2 & Thread3s\n");
  pthread_create(&Thread2, NULL, (void*)Thread2_StartRoutine, NULL);
  pthread_create(&Thread3, NULL, (void*)Thread3_StartRoutine, NULL);
  pthread_join(Thread2, (void*) &st);
  printf("T1: main: Thread2 came back. Status = %d\n", st);
  pthread_join(Thread3,(void*) &st);
  printf("T1: main: Thread3 came back. Status = %d\n", st);
  return 0;

Thanks and Regards,

Wolfram Gloger <address@hidden>

10/10/2004 05:31 PM

Pavan K Al/India/address@hidden
Re: multiple pthread_cancel() problem


> I am facing a problem in a multithread program where I send multiple
> pthread_cancels
> to a same thread from another thread.

Sounds like no "problem", but like misunderstanding..  You _cannot_
"send" multiple cancels!

> In the first thread Iam catching
> this
> pthread_cancel signal and returning to normal operation as if cancel
> signal has not come.
> This application works fine on Aix and Solaris but on Linux(redhat 9
> Kernel version 2.4.20-8)
> it is not working.

Thread cancellation is _not_ like a signal.  It's semantics are rather
different.  With LinuxThreads, cancellation is actually realized with
a signal _internally_, but that doesn't change the semantics.

> I would be greatful for your help and inputs in this regard.
> I could recreate this problem with native pthread calls and setjmp/longjmp
> calls. The
> following mail gives complete  information about the problem.
> Here is the testcase to recreate the problem:

You omitted the testcase!

However, I suspect that when you do multiple pthread_cancel()s, the
thread is _gone_ on Linux by the time you perform the second
pthread_cancel() call.  This is most probably how it should be.

> The pthread_clear_exit_np() function clears the exit status of the thread.
> This function is not portable

... and it is not available on Linux.  (I'd like to add: _fortunately_
it is not available, as I absolutely cannot see a point in it)

> Is there any alternative(or any APIs available) here to make above
> testcase
> to run with expected o/p similar to AIX/SOLARIS. Can you pls let me know
> where
> Iam going wrong here.

Well, if you want signals, you know where to find them.  Try
pthread_kill().  But note that it is really hard to mix signals and
threads correctly and usefully.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]