pthread_condattr_init() — Initialize a condition 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_condattr_init(pthread_condattr_t *attr);
SUSV3:
#define _UNIX03_THREADS
#include <pthread.h>

int pthread_condattr_init(pthread_condattr_t *attr);

General description

Establishes the default values for the condition variables that will be created. A condition attribute (condattr) object contains various condition variable characteristics. You can set up a template of these characteristics and then create a set of condition variables with similar characteristics.

Condition variable attribute objects are similar to mutex attribute objects. You can use them to manage the characteristics of condition variables in your application. They define the set of values to be used for the condition variable during its creation. For a valid condition variable attribute, refer to "pthread_condattr_setkind_np() -- Set Kind Attribute from a Condition Variable Attribute Object and pthread_condattr_setpshared() --Set the Process-Shared Condition Variable Attribute

pthread_condattr_init() is used to define a condition variable attribute object. pthread_condattr_destroy() is used to remove the definition of the condition variable attribute object. These functions are provided for portability purposes.

You can define a condition variable without using these functions by supplying a NULL parameter during the pthread_cond_init() call. For more details, refer to pthread_cond_init() — Initialize a condition variable.

Returned value

If successful, pthread_condattr_init() returns 0.

If unsuccessful, pthread_condattr_init() returns -1 and sets errno to one of the following values:
Error Code
Description
ENOMEM
There is not enough memory to initialize the condition variable attributes object.

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

Example

CELEBP25
⁄* CELEBP25 *⁄                                   
#define _OPEN_THREADS                                                           
#include <pthread.h>                                                            
#include <stdio.h>                                                              
                                                                                
main() {                                                                        
  pthread_condattr_t cond;                                                      
                                                                                
  if (pthread_condattr_init(&cond) != 0) {                                      
    perror("pthread_condattr_init() error");                                    
    exit(1);                                                                    
  }                                                                             
                                                                                
  if (pthread_condattr_destroy(&cond) != 0) {                                   
    perror("pthread_condattr_destroy() error");                                 
    exit(2);                                                                    
  }                                                                             
}                                                                               

Related information