pthread_getcancelstate_np()--Get Cancel State


  Syntax:
 #include <pthread.h>
 int pthread_getcancelstate_np(int *cancelState);  
  Service Program Name: QP0WPTHR

  Default Public Authority: *USE

  Threadsafe: Yes

  Signal Safe: Yes

The pthread_getcancelstate_np() function gets the current cancel state of the thread. Cancel state is either PTHREAD_CANCEL_ENABLE or PTHREAD_CANCEL_DISABLE. For more information about cancelability, see Thread cancellation APIs.

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 that is a cancellation point, or calls pthread_testcancel(), thus creating a cancellation point. When cancelability is asynchronous, all cancels are acted upon immediately, interrupting the thread with its processing.

Notes:

  1. Your application should not use asynchronous thread cancellation through the PTHREAD_CANCEL_ASYNCHRONOUS option of pthread_setcanceltype(). See the common user errors section of this document for more information.
  2. This function is not portable.


Authorities and Locks

None.


Parameters

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

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

Note: By using the code examples, you agree to the terms of the Code license and disclaimer information.

#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 cancellation 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 cancellation "
         "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 cancellation cleanup handlers to run
In cancellation cleanup handler
current cancel state is 1
Main completed

API introduced: V4R3

[ Back to top | Pthread APIs | APIs by category ]