pthread_attr_getdetachstate() — Get the detach state attribute

Standards

Standards / Extensions C or C++ Dependencies

POSIX.4a
Single UNIX Specification, Version 3

both

POSIX(ON)

Format

#define _OPEN_THREADS
#include <pthread.h>

int pthread_attr_getdetachstate(pthread_attr_t *attr);
SUSV3:
#define _UNIX03_THREADS 
#include <pthread.h>
int pthread_attr_getdetachstate(const pthread_attr_t *attr,
                                int *detachstate);

General description

Returns the current value of the detachstate attribute for the thread attribute object, attr, that is created by pthread_attr_init(). The detachstate attribute values are:
0
Undetached. An undetached thread will keep its resources after termination.
1
Detached. A detached thread will have its resources automatically freed by the system at termination. Thus, you cannot get the thread's termination status (or wait for the thread to terminate) by using pthread_join().

You can use a thread attribute object to manage the characteristics of threads in your application. It defines the set of values to be used for the thread during its creation. By establishing a thread attribute object, you can create many threads with the same set of characteristics, without defining those characteristics for each thread. You can define more than one thread attribute object.

Returned value

If successful, pthread_attr_getdetachstate() returns the detachstate (0 or 1).

If unsuccessful, pthread_attr_getdetachstate() returns -1.

There are no documented errno values. Use perror() or strerror() to determine the cause of the error.

Special behavior for Single UNIX Specification, Version 3: If unsuccessful, pthread_attr_getdetachstate() returns an error number to indicate the error.

Example

CELEBP07

⁄* CELEBP07 *⁄                                   
#define _OPEN_THREADS                                                           
#include <stdio.h>                                                              
#include <pthread.h>                                                            
                                                                                
int main()                                                                      
{                                                                               
   pthread_attr_t attr;                                                         
   char           typ[12];                                                      
                                                                                
   if (pthread_attr_init(&attr) == -1) {                                        
      perror("error in pthread_attr_init");                                     
      exit(1);                                                                  
   }                                                                            
                                                                                
   switch(pthread_attr_getdetachstate(&attr)) {                                 
      default:                                                                  
         perror("error in pthread_attr_getdetachstate()");                      
         exit(2);                                                               
      case 0:                                                                   
         strcpy(typ, "undetached");                                             
         break;                                                                 
      case 1:                                                                   
         strcpy(typ, "detached");                                               
   }                                                                            
   printf("The detach state is %s.\n", typ);                                    
                                                                                
   if (pthread_attr_destroy(&attr) == -1) {                                     
      perror("error in pthread_attr_destroy");                                  
      exit(2);                                                                  
   }                                                                            
   exit(0);                                                                     
}                                                                               

CELEBP61

⁄* CELEBP61 *⁄                                   
⁄* Example using SUSv3 pthread_attr_getdetachstate() interface *⁄ 

#define _UNIX03_THREADS 1                                                          
#include <stdio.h>                                                              
#include <stdlib.h>
#include <string.h>
#include <pthread.h>                                                            
#include <errno.h>                                                            

                                                                                
int main(void)                                                                      
{                                                                               
  pthread_attr_t attr;                                                         
  int            rc, newstate, foundstate; 
  char           state[12];
                                                                                
  if (pthread_attr_init(&attr) == -1) {                                        
     perror("error in pthread_attr_init");                                     
     exit(1);                                                                  
  }                                                                            

  newstate = PTHREAD_CREATE_DETACHED; 
  pthread_attr_setdetachstate(&attr, newstate);
                                                                                
  rc = pthread_attr_getdetachstate(&attr,&foundstate);
  switch(foundstate) {
      case PTHREAD_CREATE_JOINABLE:
         strcpy(state,"joinable");
         break;
      case PTHREAD_CREATE_DETACHED:
         strcpy(state,"detached");
         break;
      default:
         printf("pthread_attr_getdetachstate returned: %d\n", rc); 
         printf("Error: %d, Errno_Jr: %08x\n", errno, __errno2());
         exit(2);
  }   

  printf("Threads created with this attribute object are %s.\n",state);

  if (pthread_attr_destroy(&attr) == -1) {                                     
     perror("error in pthread_attr_destroy");                                  
     exit(3);                                                                  
  }                                                                            

  exit(0);                                                                     
}                                                                               

Related information