Friday, April 26, 2013

pthread_mutexattr_init example c c++


NAME

pthread_mutexattr_destroy, pthread_mutexattr_init - destroy and initialize the mutex attributes object

SYNOPSIS

[THR] [Option Start] #include <pthread.h>

int pthread_mutexattr_destroy(pthread_mutexattr_t *
attr);
int pthread_mutexattr_init(pthread_mutexattr_t *
attr); [Option End]

DESCRIPTION

The pthread_mutexattr_destroy() function shall destroy a mutex attributes object; the object becomes, in effect, uninitialized. An implementation may cause pthread_mutexattr_destroy() to set the object referenced by attr to an invalid value. A destroyed attr attributes object can be reinitialized using pthread_mutexattr_init(); the results of otherwise referencing the object after it has been destroyed are undefined.
The pthread_mutexattr_init() function shall initialize a mutex attributes object attr with the default value for all of the attributes defined by the implementation.
Results are undefined if pthread_mutexattr_init() is called specifying an already initialized attr attributes object.
After a mutex attributes object has been used to initialize one or more mutexes, any function affecting the attributes object (including destruction) shall not affect any previously initialized mutexes.(pthread_mutexattr_destroy, pthread_mutexattr_init)

RETURN VALUE

Upon successful completion, pthread_mutexattr_destroy() and pthread_mutexattr_init() shall return zero; otherwise, an error number shall be returned to indicate the error.

ERRORS

The pthread_mutexattr_destroy() function may fail if:
[EINVAL]
The value specified by attr is invalid.
The pthread_mutexattr_init() function shall fail if:
[ENOMEM]
Insufficient memory exists to initialize the mutex attributes object.
These functions shall not return an error code of [EINTR].
Example of (pthread_mutexattr_destroy, pthread_mutexattr_init)

See Code disclaimer information for information pertaining to code examples.

#include <pthread.h>
#include <stdio.h>
#include "check.h"

pthread_mutex_t    mutex;

int main(int argc, char **argv)
{
  int                   rc=0;
  pthread_mutexattr_t   mta;

  printf("Entering testcase\n");

  printf("Create a default mutex attribute\n");
  rc = pthread_mutexattr_init(&mta);
  checkResults("pthread_mutexattr_init\n", rc);

  printf("Create the mutex using a mutex attributes object\n");
  rc = pthread_mutex_init(&mutex, &mta);
  checkResults("pthread_mutex_init(mta)\n", rc);

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

  printf("Destroy mutex attribute\n");
  rc = pthread_mutexattr_destroy(&mta);
  checkResults("pthread_mutexattr_destroy()\n", rc);


  printf("Destroy mutex\n");
  rc = pthread_mutex_destroy(&mutex);
  checkResults("pthread_mutex_destroy()\n", rc);

  printf("Main completed\n");
  return 0;
}

Output:

Entering testcase
Create a default mutex attribute
Create the mutex using a mutex attributes object
- At this point, the mutex with its default attributes
- Can be used from any threads that want to use it
Destroy mutex attribute
Destroy mutex
Main completed