[Prev] [Contents] [Next]

pthread_getcancelstate_np()--Get Cancel State

Syntax

#include <pthread.h>
int pthread_getcancelstate_np(int *cancelState);
Threadsafe: Yes
Signal Safe: Yes

DThe pthread_getcancelstate_np() function gets the current cancel state of the thread. Cancel state is one of PTHREAD_CANCEL_ENABLE or PTHREAD_CANCEL_DISABLE

Cancelability consists of 3 separate states (disabled, deferred, asynchronous) that can be represented by 2 boolean values.
Cancelability Cancelability State Cancelability Type
disabled PTHREAD_CANCEL_DISABLE PTHREAD_CANCEL_DEFERRED
disabled PTHREAD_CANCEL_DISABLE PTHREAD_CANCEL_ASYNCHRONOUS
deferred PTHREAD_CANCEL_ENABLE PTHREAD_CANCEL_DEFERRED
asynchronous PTHREAD_CANCEL_ENABLE PTHREAD_CANCEL_ASYNCHRONOUS

The default cancelability state is deferred.

When cancelability is disabled, all cancels are held pending in the target thread until the thread changes the cancelability. When cancelability is deferred, all cancels are held pending in the target thread until the thread changes the cancelability, calls a function which is a cancelation point or calls pthread_testcancel(), thus creating a cancelation point. When cancelability is asynchronous, all cancels are acted upon immediately, interrupting the thread with its processing.

It is recommended that your application not use asynchronous thread cancelation via the PTHREAD_CANCEL_ASYNCHRONOUS option of pthread_setcanceltype(). See the common user errors section of this document for more information.

This function is not portable

Parameters

cancelstate
(Output) Address of the variable to receive the cancel state.

Authorities and Locks

None.

Return Value

0
pthread_getcancelstate_np() was successful.
value
pthread_getcancelstate_np() was not successful. value is set to indicate the error condition.

Error Conditions

If pthread_getcancelstate_np() was not successful, the error condition returned usually indicates one of the following errors. Under some conditions, the value returned could indicate an error other than those listed here.

[EINVAL]
The value specified for the argument is not correct.

Related Information

Example

#define _MULTI_THREADED
#include <pthread.h>
#include <stdio.h>
#include <except.h>
#include <setjmp.h>
#include "check.h"

void    showCancelState(void);
int     threadStatus=42;

void showCancelState(void)
{
  int   state, rc;
  
  rc = pthread_getcancelstate_np(&state);
  checkResults("pthread_getcancelstate_np()\n", rc);  
  printf("current cancel state is %d\n", state);
}

void cleanupHandler2(void *p)
{
  printf("In cancelation cleanup handler\n");
  showCancelState();
  return;
}

void *threadfunc(void *parm)
{
  int           rc, old;
  
  printf("Inside secondary thread\n");
  showCancelState();
  
  pthread_cleanup_push(cleanupHandler2, NULL);
  threadStatus = 0;
  printf("Calling pthread_exit() will allow cancelation "
         "cleanup handlers to run\n");
  pthread_exit(__VOID(threadStatus));
  pthread_cleanup_pop(0);
  return __VOID(-1);
}

int main(int argc, char **argv)
{
  pthread_t             thread;
  int                   rc=0;
  char                  c;
  void                 *status;

  printf("Enter Testcase - %s\n", argv[0]);

  printf("Create thread that will demonstrate pthread_getcancelstate_np()\n");
  rc = pthread_create(&thread, NULL, threadfunc, NULL);
  checkResults("pthread_create()\n", rc);

  rc = pthread_join(thread, &status);
  checkResults("pthread_join()\n", rc);

  if (__INT(status) != threadStatus) {
    printf("Got an unexpected return status from the thread!\n");
    exit(1);
  }
  printf("Main completed\n");
  return 0;
}

Output

Enter Testcase - QP0WTEST/TPGETCANS0
Create thread that will demonstrate pthread_getcancelstate_np()
Inside secondary thread
current cancel state is 0
Calling pthread_exit() will allow cancelation cleanup handlers to run
In cancelation cleanup handler
current cancel state is 1
Main completed



[Prev] [Contents] [Next]
Copyright © 1998, IBM Corporation. All rights reserved.
Comments? Contact
rchthrds@us.ibm.com