Friday, April 26, 2013

pthread_rwlockattr_destroy example c c++


NAME

pthread_rwlockattr_destroy, pthread_rwlockattr_init - destroy and initialize the read-write lock attributes object

SYNOPSIS

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

int pthread_rwlockattr_destroy(pthread_rwlockattr_t
 *attr);
int pthread_rwlockattr_init(pthread_rwlockattr_t
 *attr); [Option End]

DESCRIPTION

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

RETURN VALUE

If successful, the pthread_rwlockattr_destroy() and pthread_rwlockattr_init() functions shall return zero; otherwise, an error number shall be returned to indicate the error.

ERRORS

The pthread_rwlockattr_destroy() function may fail if:
[EINVAL]
The value specified by attr is invalid.
The pthread_rwlockattr_init() function shall fail if:
[ENOMEM]
Insufficient memory exists to initialize the read-write lock attributes object.
These functions shall not return an error code of [EINTR].

EXAMPLES of (pthread_rwlockattr_destroy, pthread_rwlockattr_init)

Note: By using the code examples, you agree to the terms of the Code license and disclaimer information.
#define _MULTI_THREADED
#include <pthread.h>
#include <stdio.h>
#include "check.h"

pthread_rwlock_t        rwlock1;
pthread_rwlock_t        rwlock2 = PTHREAD_RWLOCK_INITIALIZER;

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

  printf("Enter Testcase - %s\n", argv[0]);

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

  printf("Use the rwlock attributes to created rwlocks here\n");
  rc = pthread_rwlock_init(&rwlock1, &attr);
  checkResults("pthread_rwlock_init()\n", rc);

  printf("The rwlock1 is now ready for use.\n");
  printf("The rwlock2 that was statically initialized was ready when\n"
         "the main routine was entered\n");

  printf("Destroy rwlock attribute\n");
  rc = pthread_rwlockattr_destroy(&attr);
  checkResults("pthread_rwlockattr_destroy()\n", rc);

  printf("Use the rwlocks\n");
  rc = pthread_rwlock_rdlock(&rwlock1);
  checkResults("pthread_rwlock_rdlock()\n", rc);

  rc = pthread_rwlock_wrlock(&rwlock2);
  checkResults("pthread_rwlock_wrlock()\n", rc);

  rc = pthread_rwlock_unlock(&rwlock1);
  checkResults("pthread_rwlock_unlock(1)\n", rc);

  rc = pthread_rwlock_unlock(&rwlock2);
  checkResults("pthread_rwlock_unlock(2)\n", rc);

  printf("Destroy the rwlocks\n");
  rc = pthread_rwlock_destroy(&rwlock1);
  checkResults("pthread_rwlock_destroy(1)\n", rc);

  rc = pthread_rwlock_destroy(&rwlock2);
  esults("pthread_rwlock_destroy(2)\n", rc);

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

Output:

Enter Testcase - QP0WTEST/TPRWLAI0
Create a default rwlock attribute
Use the rwlock attributes to created rwlocks here
The rwlock is now ready for use.
The rwlock that was statically initialized was ready when
the main routine was entered
Destroy rwlock attribute
Use the rwlocks
Destroy the rwlocks
Main completed