pthread_setintrtype() — Set a thread cancelability type

Standards

Standards / Extensions C or C++ Dependencies
POSIX.4a both

POSIX(ON)

Format

#define _OPEN_THREADS
#include <pthread.h>

int pthread_setintrtype(int type);

General description

Controls when a cancel request is acted on. The cancelability types can be:
PTHREAD_INTR_ASYNCHRONOUS
The thread can be canceled at any time.
PTHREAD_INTR_CONTROLLED
The thread can be canceled, but only at specific points of execution. These are:
  • When waiting on a condition variable, which is pthread_cond_wait() or pthread_cond_timedwait()
  • When waiting for the end of another thread, which is pthread_join()
  • While waiting for an asynchronous signal, which is sigwait()
  • When setting the calling thread's cancelability state, which is pthread_setintr()
  • Testing specifically for a cancel request, which is pthread_testintr()
  • When suspended because of POSIX functions or one of the following C standard functions: close(), fcntl(), open(), pause(), read(), tcdrain(), tcsetattr(), sigsuspend(), sigwait(), sleep(), wait(), or write()

Usage notes

  1. If you are writing to the Single UNIX Specification, Version 3 standard, use pthread_setcanceltype() in place of pthread_setintrtype().

Returned value

If successful, pthread_setintrtype() returns the previous type.

If unsuccessful, pthread_setintrtype() returns -1 and sets errno to one of the following values:
Error Code
Description
EINVAL
type is an invalid value.

Example

CELEBP50
⁄* CELEBP50 *⁄                                   
#define _OPEN_THREADS                                                           
#include <stdio.h>                                                              
#include <string.h>                                                             
#include <pthread.h>                                                            
#include <errno.h>                                                              
#include <unistd.h>                                                             
                                                                                
int  thstatus;                                                                  
char state[60] = "enable⁄controlled - initial default";                         
                                                                                
void * thfunc(void *voidptr)                                                    
{                                                                               
 int  rc;                                                                       
 char *parmptr;                                                                 
                                                                                
 parmptr = voidptr;                                                             
 printf("parm = %s.\n", parmptr);                                               
 if ( pthread_setintrtype(PTHREAD_INTR_CONTROLLED ) == -1 ) {                   
   printf("set controlled failed. %s\n", strerror(errno));                      
   thstatus = 103;                                                              
   pthread_exit(&thstatus);                                                     
 }                                                                              
 strcpy(state, "disable⁄controlled");                                           
                                                                                
 if ( pthread_setintr(PTHREAD_INTR_ENABLE) == -1 ) {                            
   printf("set enable failed. %s\n", strerror(errno));                          
   thstatus = 104;                                                              
   pthread_exit(&thstatus);                                                     
 }                                                                              
 strcpy(state, "enable⁄controlled");                                            
                                                                                
 strcat(state, " - pthread_testintr");                                          
                                                                                
 while(1) {                                                                     
   pthread_testintr();                                                          
   sleep(1);                                                                    
 }                                                                              
                                                                                
 thstatus = 100;                                                                
 pthread_exit(&thstatus);                                                       
}                                                                               
                                                                                
main(int argc, char *argv[]) {                                                  
  int            rc;                                                            
  pthread_attr_t attrarea;                                                      
  pthread_t      thid;                                                          
  char           parm[] = "abcdefghijklmnopqrstuvwxyz";                         
  int            *statptr;                                                      
                                                                                
  if ( pthread_attr_init(&attrarea) == -1 ) {                                   
    printf("pthread_attr_init failed. %s\n", strerror(errno));                  
    exit(1);                                                                    
  }                                                                             
                                                                                
  if ( pthread_create(&thid, &attrarea, thfunc, (void *)&parm) == -1) {         
    printf("pthread_create failed. %s\n", strerror(errno));                     
    exit(2);                                                                    
  }                                                                             
                                                                                
  sleep(5);                                                                     
                                                                                
  if ( pthread_cancel(thid) == -1 ) {                                           
    printf("pthread_cancel failed. %s\n", strerror(errno));                     
    exit(3);                                                                    
  }                                                                             
                                                                                
   if ( pthread_join(thid, (void **)&statptr)== -1 ) {                          
    printf("pthread_join failed. %s\n", strerror(errno));                       
    exit(4);                                                                    
  }                                                                             
                                                                                
  if ( statptr == (int *)-1 )                                                   
    printf("thread was cancelled. state = %s.\n", state);                       
  else                                                                          
    printf("thread was not cancelled. thstatus = %d.\n", *statptr);             
                                                                                
  exit(0);                                                                      
                                                                                
}                                                                               
Output:
parm = abcdefghijklmnopqrstuvwxyz.
thread was canceled. state = enable/controlled - pthread_testintr.

Related information