#include <pthread.h> int pthread_getcancelstate_np(int *cancelState);Service Program Name: QP0WPTHR
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 on 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:
None.
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.
The value specified for the argument is not correct.
See Code disclaimer information for information pertaining to code examples.
#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
Top | Pthread APIs | APIs by category |