Friday, April 26, 2013

pthread_condattr_destroy example c c++


NAME

pthread_condattr_init, pthread_condattr_destroy - initialise and destroy condition variable attributes object

 SYNOPSIS

#include <pthread.h>

int pthread_condattr_init(pthread_condattr_t *attr);
int pthread_condattr_destroy(pthread_condattr_t *attr);

 DESCRIPTION

The function pthread_condattr_init() initialises a condition variable attributes object attr with the default value for all of the attributes defined by the implementation.Attempting to initialise an already initialised condition variable attributes object results in undefined behaviour.(pthread_condattr_init, pthread_condattr_destroy)
After a condition variable attributes object has been used to initialise one or more condition variables, any function affecting the attributes object (including destruction) does not affect any previously initialised condition variables.
The pthread_condattr_destroy() function destroys a condition variable attributes object; the object becomes, in effect, uninitialised. An implementation may cause pthread_condattr_destroy() to set the object referenced by attr to an invalid value. A destroyed condition variable attributes object can be re-initialised using pthread_condattr_init(); the results of otherwise referencing the object after it has been destroyed are undefined.
Additional attributes, their default values, and the names of the associated functions to get and set those attribute values are implementation-dependent.

 RETURN VALUE of (pthread_condattr_init, pthread_condattr_destroy)

If successful, the pthread_condattr_init() and pthread_condattr_destroy() functions return zero. Otherwise, an error number is returned to indicate the error.

 ERRORS

The pthread_condattr_init() function will fail if:
[ENOMEM]
Insufficient memory exists to initialise the condition variable attributes object.
The pthread_condattr_destroy() function may fail if:
[EINVAL]
The value specified by attr is invalid.
These functions will not return an error code of [EINTR].

 EXAMPLES of (pthread_condattr_init, pthread_condattr_destroy)

See Code disclaimer information for information pertaining to code examples.
#include <pthread.h>
#include <stdio.h>
#include "check.h"

pthread_cond_t      cond;

int main(int argc, char **argv)
{
  int                   rc=0;
  pthread_condattr_t    attr;

  printf("Entering testcase\n");

  printf("Create a default condition attribute\n");
  rc = pthread_condattr_init(&attr);
  checkResults("pthread_condattr_init\n", rc);

  printf("Create the condition using the condition attributes object\n");
  rc = pthread_cond_init(&cond, &attr);
  checkResults("pthread_cond_init()\n", rc);

  printf("- At this point, the condition with its default attributes\n");
  printf("- Can be used from any threads that want to use it\n");

  printf("Destroy cond attribute\n");
  rc = pthread_condattr_destroy(&attr);
  checkResults("pthread_condattr_destroy()\n", rc);
 
  printf("Destroy condition\n");
  rc = pthread_cond_destroy(&cond);
  checkResults("pthread_cond_destroy()\n", rc);
 
  printf("Main completed\n");
  return 0;
}
Output:
Entering testcase
Create a default condition attribute
Create the condition using the condition attributes object
- At this point, the condition with its default attributes
- Can be used from any threads that want to use it
Destroy cond attribute
Destroy condition
Main completed