pthread_attr_init() — Initialize a thread attribute object

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_init(pthread_attr_t *attr);
SUSV3:
#define _UNIX03_THREADS
#include <pthread.h>

int pthread_attr_init(pthread_attr_t *attr);

General description

Initializes attr with the default thread attributes, whose defaults are:
stacksize
Inherited from the STACK runtime option
detachstate
Undetached
synch
Synchronous
weight
Heavy

Using a thread attribute object, you can 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. All threads are of equal priority.

If a thread attribute object is shared between threads, the application must provide the necessary synchronization because a thread attribute object is defined in the application's storage.
Note: An XPLINK application uses two stacks, an upward-growing stack and a downward-growing stack. "stacksize" always refers to the size of the upward-growing stack. The size of the downward-growing stack is inherited from the THREADSTACK runtime option.

Returned value

If successful, pthread_attr_init() returns 0.

If unsuccessful, pthread_attr_init() returns -1 and sets errno to one of the following values:
Error Code
Description
ENOMEM
Not enough memory is available to create the thread attribute object.

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

Example

CELEBP10
/* CELEBP10 */                                   
#define _OPEN_THREADS                                                           
#include <stdio.h>                                                              
#include <pthread.h>                                                            
                                                                                
void *thread1(void *arg)                                                        
{                                                                               
   printf("hello from the thread\n");                                           
   pthread_exit(NULL);                                                          
}                                                                               
                                                                                
int main()                                                                      
{                                                                               
   int            rc, stat;                                                     
   pthread_attr_t attr;                                                         
   pthread_t      thid;                                                         
                                                                                
   rc = pthread_attr_init(&attr);                                               
   if (rc == -1) {                                                              
      perror("error in pthread_attr_init");                                     
      exit(1);                                                                  
   }                                                                            
                                                                                
   rc = pthread_create(&thid, &attr, thread1, NULL);                            
   if (rc == -1) {                                                              
      perror("error in pthread_create");                                        
      exit(2);                                                                  
   }                                                                            
                                                                                
   rc = pthread_join(thid, (void *)&stat);                                      
   exit(0);                                                                     
}