Showing posts with label pthread example. Show all posts
Showing posts with label pthread example. Show all posts

Friday, April 26, 2013

pthread_cleanup_pop example c c++


NAME

pthread_cleanup_push, pthread_cleanup_pop - establish cancellation handlers

 SYNOPSIS

#include <pthread.h>

void pthread_cleanup_push(void (*routine)(void*), void *arg);
void pthread_cleanup_pop(int execute);

 DESCRIPTION

The pthread_cleanup_push() function pushes the specified cancellation cleanup handler routine onto the calling thread's cancellation cleanup stack. The cancellation cleanup handler is popped from the cancellation cleanup stack and invoked with the argument argwhen: (a) the thread exits (that is, calls pthread_exit()), (b) the thread acts upon a cancellation request, or (c) the thread calls pthread_cleanup_pop() with a non-zero execute argument.The pthread_cleanup_pop() function removes the routine at the top of the calling thread's cancellation cleanup stack and optionally invokes it (if execute is non-zero).
These functions may be implemented as macros and will appear as statements and in pairs within the same lexical scope (that is, the pthread_cleanup_push() macro may be thought to expand to a token list whose first token is `{' with pthread_cleanup_pop()expanding to a token list whose last token is the corresponding `}'.
The effect of calling longjmp() or siglongjmp() is undefined if there have been any calls to pthread_cleanup_push() or pthread_cleanup_pop() made without the matching call since the jump buffer was filled. The effect of calling longjmp() orsiglongjmp() from inside a cancellation cleanup handler is also undefined unless the jump buffer was also filled in the cancellation cleanup handler.

 RETURN VALUE

The pthread_cleanup_push() and pthread_cleanup_pop() functions return no value.

 ERRORS

No errors are defined.These functions will not return an error code of [EINTR].

 EXAMPLES of (pthread_cleanup_push, pthread_cleanup_pop)

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"

void cleanupHandler(void *arg)
{
  printf("In the cleanup handler\n");
}

void *threadfunc(void *parm)
{
  printf("Entered secondary thread\n");
  pthread_cleanup_push(cleanupHandler, NULL);
  while (1) {
    pthread_testcancel();
    sleep(1);
  }
  pthread_cleanup_pop(0);
  return NULL;
}

int main(int argc, char **argv)
{
  pthread_t             thread;
  int                   rc=0;

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

  /* Create a thread using default attributes */
  printf("Create thread using the NULL attributes\n");
  rc = pthread_create(&thread, NULL, threadfunc, NULL);
  checkResults("pthread_create(NULL)\n", rc);

  /* sleep() is not a very robust way to wait for the thread */
  sleep(2);

  printf("Cancel the thread\n");
  rc = pthread_cancel(thread);
  checkResults("pthread_cancel()\n", rc);

  /* sleep() is not a very robust way to wait for the thread */
  sleep(3);
  printf("Main completed\n");
  return 0;
}

Output:

Enter Testcase - QP0WTEST/TPCLPU0
Create thread using the NULL attributes
Entered secondary thread
Cancel the thread
In the cleanup handler
Main completed

pthread_cleanup_push example c c++


NAME

pthread_cleanup_push, pthread_cleanup_pop - establish cancellation handlers

 SYNOPSIS

#include <pthread.h>

void pthread_cleanup_push(void (*routine)(void*), void *arg);
void pthread_cleanup_pop(int execute);

 DESCRIPTION

The pthread_cleanup_push() function pushes the specified cancellation cleanup handler routine onto the calling thread's cancellation cleanup stack. The cancellation cleanup handler is popped from the cancellation cleanup stack and invoked with the argument argwhen: (a) the thread exits (that is, calls pthread_exit()), (b) the thread acts upon a cancellation request, or (c) the thread callspthread_cleanup_pop() with a non-zero execute argument.The pthread_cleanup_pop() function removes the routine at the top of the calling thread's cancellation cleanup stack and optionally invokes it (if execute is non-zero).
These functions may be implemented as macros and will appear as statements and in pairs within the same lexical scope (that is, the pthread_cleanup_push() macro may be thought to expand to a token list whose first token is `{' with pthread_cleanup_pop()expanding to a token list whose last token is the corresponding `}'.
The effect of calling longjmp() or siglongjmp() is undefined if there have been any calls to pthread_cleanup_push() orpthread_cleanup_pop() made without the matching call since the jump buffer was filled. The effect of calling longjmp() orsiglongjmp() from inside a cancellation cleanup handler is also undefined unless the jump buffer was also filled in the cancellation cleanup handler.

 RETURN VALUE

The pthread_cleanup_push() and pthread_cleanup_pop() functions return no value.

 ERRORS

No errors are defined.These functions will not return an error code of [EINTR].

 EXAMPLES of (pthread_cleanup_push, pthread_cleanup_pop)

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"

void cleanupHandler(void *arg)
{
  printf("In the cleanup handler\n");
}

void *threadfunc(void *parm)
{
  printf("Entered secondary thread\n");
  pthread_cleanup_push(cleanupHandler, NULL);
  while (1) {
    pthread_testcancel();
    sleep(1);
  }
  pthread_cleanup_pop(0);
  return NULL;
}

int main(int argc, char **argv)
{
  pthread_t             thread;
  int                   rc=0;

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

  /* Create a thread using default attributes */
  printf("Create thread using the NULL attributes\n");
  rc = pthread_create(&thread, NULL, threadfunc, NULL);
  checkResults("pthread_create(NULL)\n", rc);

  /* sleep() is not a very robust way to wait for the thread */
  sleep(2);

  printf("Cancel the thread\n");
  rc = pthread_cancel(thread);
  checkResults("pthread_cancel()\n", rc);

  /* sleep() is not a very robust way to wait for the thread */
  sleep(3);
  printf("Main completed\n");
  return 0;
}

Output:

Enter Testcase - QP0WTEST/TPCLPU0
Create thread using the NULL attributes
Entered secondary thread
Cancel the thread
In the cleanup handler
Main completed

pthread_setspecific example c c++


NAME

pthread_getspecific, pthread_setspecific - thread-specific data management

SYNOPSIS

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

void *pthread_getspecific(pthread_key_t
 key);
int pthread_setspecific(pthread_key_t
 key, const void *value); [Option End]

DESCRIPTION

The pthread_getspecific() function shall return the value currently bound to the specified key on behalf of the calling thread.
The pthread_setspecific() function shall associate a thread-specific value with a key obtained via a previous call to pthread_key_create(). Different threads may bind different values to the same key. These values are typically pointers to blocks of dynamically allocated memory that have been reserved for use by the calling thread.
The effect of calling pthread_getspecific() or pthread_setspecific() with a key value not obtained from pthread_key_create() or after key has been deleted with pthread_key_delete() is undefined.
Both pthread_getspecific() and pthread_setspecific() may be called from a thread-specific data destructor function. A call topthread_getspecific() for the thread-specific data key being destroyed shall return the value NULL, unless the value is changed (after the destructor starts) by a call to pthread_setspecific(). Calling pthread_setspecific() from a thread-specific data destructor routine may result either in lost storage (after at least PTHREAD_DESTRUCTOR_ITERATIONS attempts at destruction) or in an infinite loop.
Both functions may be implemented as macros.

RETURN VALUE

The pthread_getspecific() function shall return the thread-specific data value associated with the given key. If no thread-specific data value is associated with key, then the value NULL shall be returned.
If successful, the pthread_setspecific() function shall return zero; otherwise, an error number shall be returned to indicate the error.

ERRORS

No errors are returned from pthread_getspecific().
The pthread_setspecific() function shall fail if:
[ENOMEM]
Insufficient memory exists to associate the non-NULL value with the key.
The pthread_setspecific() function may fail if:
[EINVAL]
The key value is invalid.
These functions shall not return an error code of [EINTR].
Example of (pthread_getspecific, pthread_setspecific)
 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"

#define         NUMTHREADS     3
pthread_key_t   tlsKey = 0;

void globalDestructor(void *value)
{
  printf("In the globalDestructor\n");
  free(value);
  pthread_setspecific(tlsKey, NULL);
}


void showGlobal(void)
{
  void                 *global;
  pthread_id_np_t       tid;

  global  = pthread_getspecific(tlsKey);
  pthread_getunique_np((pthread_t *)global, &tid);
  printf("showGlobal: global data stored for thread 0x%.8x%.8x\n",
         tid);
}

void *threadfunc(void *parm)
{
  int           rc;
  int          *myThreadDataStructure;
  pthread_t     me = pthread_self();

  printf("Inside secondary thread\n");

  myThreadDataStructure = malloc(sizeof(pthread_t) + sizeof(int) * 10);
  memcpy(myThreadDataStructure, &me, sizeof(pthread_t));
  pthread_setspecific(tlsKey, myThreadDataStructure);
  showGlobal();
  pthread_exit(NULL);
}

int main(int argc, char **argv)
{
  pthread_t             thread[NUMTHREADS];
  int                   rc=0;
  int                   i=0;

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

  printf("Create a thread local storage key\n");
  rc = pthread_key_create(&tlsKey, globalDestructor);
  checkResults("pthread_key_create()\n", rc);
  /* The key can now be used from all threads */

  printf("Create %d threads using joinable attributes\n",
         NUMTHREADS);
  for (i=0; i<NUMTHREADS; ++i) {
    rc = pthread_create(&thread[i], NULL, threadfunc, NULL);
    checkResults("pthread_create()\n", rc);
  }

  printf("Join to threads\n");
  for (i=0; i<NUMTHREADS; ++i) {
    rc = pthread_join(thread[i], NULL);
    checkResults("pthread_join()\n", rc);
  }

  printf("Delete a thread local storage key\n");
  rc = pthread_key_delete(tlsKey);
  checkResults("pthread_key_delete()\n", rc);
  /* The key and any remaining values are now gone. */
  printf("Main completed\n");
  return 0;
}

Output:

Enter Testcase - QP0WTEST/TPGETS0
Create a thread local storage key
Create 3 threads using joinable attributes
Join to threads
Inside secondary thread
showGlobal: global data stored for thread 0x000000000000000b
In the globalDestructor
Inside secondary thread
showGlobal: global data stored for thread 0x000000000000000d
In the globalDestructor
Inside secondary thread
showGlobal: global data stored for thread 0x000000000000000c
In the globalDestructor
Delete a thread local storage key
Main completed

pthread_getspecific example c c++


NAME

pthread_getspecific, pthread_setspecific - thread-specific data management

SYNOPSIS

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

void *pthread_getspecific(pthread_key_t
 key);
int pthread_setspecific(pthread_key_t
 key, const void *value); [Option End]

DESCRIPTION

The pthread_getspecific() function shall return the value currently bound to the specified key on behalf of the calling thread.
The pthread_setspecific() function shall associate a thread-specific value with a key obtained via a previous call to pthread_key_create(). Different threads may bind different values to the same key. These values are typically pointers to blocks of dynamically allocated memory that have been reserved for use by the calling thread.
The effect of calling pthread_getspecific() or pthread_setspecific() with a key value not obtained from pthread_key_create() or after key has been deleted with pthread_key_delete() is undefined.
Both pthread_getspecific() and pthread_setspecific() may be called from a thread-specific data destructor function. A call to pthread_getspecific() for the thread-specific data key being destroyed shall return the value NULL, unless the value is changed (after the destructor starts) by a call to pthread_setspecific(). Calling pthread_setspecific() from a thread-specific data destructor routine may result either in lost storage (after at least PTHREAD_DESTRUCTOR_ITERATIONS attempts at destruction) or in an infinite loop.
Both functions may be implemented as macros.

RETURN VALUE

The pthread_getspecific() function shall return the thread-specific data value associated with the given key. If no thread-specific data value is associated with key, then the value NULL shall be returned.
If successful, the pthread_setspecific() function shall return zero; otherwise, an error number shall be returned to indicate the error.

ERRORS

No errors are returned from pthread_getspecific().
The pthread_setspecific() function shall fail if:
[ENOMEM]
Insufficient memory exists to associate the non-NULL value with the key.
The pthread_setspecific() function may fail if:
[EINVAL]
The key value is invalid.
These functions shall not return an error code of [EINTR].
Example of (pthread_getspecific, pthread_setspecific)
 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"

#define         NUMTHREADS     3
pthread_key_t   tlsKey = 0;

void globalDestructor(void *value)
{
  printf("In the globalDestructor\n");
  free(value);
  pthread_setspecific(tlsKey, NULL);
}


void showGlobal(void)
{
  void                 *global;
  pthread_id_np_t       tid;

  global  = pthread_getspecific(tlsKey);
  pthread_getunique_np((pthread_t *)global, &tid);
  printf("showGlobal: global data stored for thread 0x%.8x%.8x\n",
         tid);
}

void *threadfunc(void *parm)
{
  int           rc;
  int          *myThreadDataStructure;
  pthread_t     me = pthread_self();

  printf("Inside secondary thread\n");

  myThreadDataStructure = malloc(sizeof(pthread_t) + sizeof(int) * 10);
  memcpy(myThreadDataStructure, &me, sizeof(pthread_t));
  pthread_setspecific(tlsKey, myThreadDataStructure);
  showGlobal();
  pthread_exit(NULL);
}

int main(int argc, char **argv)
{
  pthread_t             thread[NUMTHREADS];
  int                   rc=0;
  int                   i=0;

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

  printf("Create a thread local storage key\n");
  rc = pthread_key_create(&tlsKey, globalDestructor);
  checkResults("pthread_key_create()\n", rc);
  /* The key can now be used from all threads */

  printf("Create %d threads using joinable attributes\n",
         NUMTHREADS);
  for (i=0; i<NUMTHREADS; ++i) {
    rc = pthread_create(&thread[i], NULL, threadfunc, NULL);
    checkResults("pthread_create()\n", rc);
  }

  printf("Join to threads\n");
  for (i=0; i<NUMTHREADS; ++i) {
    rc = pthread_join(thread[i], NULL);
    checkResults("pthread_join()\n", rc);
  }

  printf("Delete a thread local storage key\n");
  rc = pthread_key_delete(tlsKey);
  checkResults("pthread_key_delete()\n", rc);
  /* The key and any remaining values are now gone. */
  printf("Main completed\n");
  return 0;
}

Output:

Enter Testcase - QP0WTEST/TPGETS0
Create a thread local storage key
Create 3 threads using joinable attributes
Join to threads
Inside secondary thread
showGlobal: global data stored for thread 0x000000000000000b
In the globalDestructor
Inside secondary thread
showGlobal: global data stored for thread 0x000000000000000d
In the globalDestructor
Inside secondary thread
showGlobal: global data stored for thread 0x000000000000000c
In the globalDestructor
Delete a thread local storage key
Main completed

pthread_key_delete example c c++


NAME

pthread_key_delete - thread-specific data key deletion

SYNOPSIS

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

int pthread_key_delete(pthread_key_t
 key); [Option End]

DESCRIPTION

The pthread_key_delete() function shall delete a thread-specific data key previously returned by pthread_key_create(). The thread-specific data values associated with key need not be NULL at the time pthread_key_delete() is called. It is the responsibility of the application to free any application storage or perform any cleanup actions for data structures related to the deleted key or associated thread-specific data in any threads; this cleanup can be done either before or after pthread_key_delete() is called. Any attempt to use key following the call to pthread_key_delete() results in undefined behavior.
The pthread_key_delete() function shall be callable from within destructor functions. No destructor functions shall be invoked by pthread_key_delete(). Any destructor function that may have been associated with key shall no longer be called upon thread exit.

RETURN VALUE

If successful, the pthread_key_delete() function shall return zero; otherwise, an error number shall be returned to indicate the error.

ERRORS

The pthread_key_delete() function may fail if:
[EINVAL]
The key value is invalid.
The pthread_key_delete() function shall not return an error code of [EINTR].
Example

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 <sched.h>
#include <stdio.h>
#include "check.h"

pthread_key_t        tlsKey = 0;

void globalDestructor(void *value)
{
  printf("In the data destructor\n");
  free(value);
  pthread_setspecific(tlsKey, NULL);
}


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

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

  printf("Create a thread local storage key\n");
  rc = pthread_key_create(&tlsKey, globalDestructor);
  checkResults("pthread_key_create()\n", rc);
  /* The key can now be used from all threads */

  printf("- The key can now be used from all threads\n");
  printf("- in the process to storage thread local\n");
  printf("- (but global to all functions in that thread)\n");
  printf("- storage\n");

  printf("Delete a thread local storage key\n");
  rc = pthread_key_delete(tlsKey);
  checkResults("pthread_key_delete()\n", rc);
  /* The key and any remaining values are now gone. */
  printf("Main completed\n");
  return 0;
}

Output:

Enter Testcase - QP0WTEST/TPKEYC0
Create a thread local storage key
- The key can now be used from all threads
- in the process to storage thread local
- (but global to all functions in that thread)
- storage
Delete a thread local storage key
Main completed

pthread_key_create example c c++


NAME

pthread_key_create - thread-specific data key creation

SYNOPSIS

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

int pthread_key_create(pthread_key_t *
key, void (*destructor)(void*)); [Option End]

DESCRIPTION

The pthread_key_create() function shall create a thread-specific data key visible to all threads in the process. Key values provided by pthread_key_create() are opaque objects used to locate thread-specific data. Although the same key value may be used by different threads, the values bound to the key by pthread_setspecific() are maintained on a per-thread basis and persist for the life of the calling thread.
Upon key creation, the value NULL shall be associated with the new key in all active threads. Upon thread creation, the value NULL shall be associated with all defined keys in the new thread.
An optional destructor function may be associated with each key value. At thread exit, if a key value has a non-NULL destructor pointer, and the thread has a non-NULL value associated with that key, the value of the key is set to NULL, and then the function pointed to is called with the previously associated value as its sole argument. The order of destructor calls is unspecified if more than one destructor exists for a thread when it exits. (pthread_key_create)
If, after all the destructors have been called for all non-NULL values with associated destructors, there are still some non-NULL values with associated destructors, then the process is repeated. If, after at least {PTHREAD_DESTRUCTOR_ITERATIONS} iterations of destructor calls for outstanding non-NULL values, there are still some non-NULL values with associated destructors, implementations may stop calling destructors, or they may continue calling destructors until no non-NULL values with associated destructors exist, even though this might result in an infinite loop.

RETURN VALUE

If successful, the pthread_key_create() function shall store the newly created key value at *key and shall return zero. Otherwise, an error number shall be returned to indicate the error.

ERRORS

The pthread_key_create() function shall fail if:
[EAGAIN]
The system lacked the necessary resources to create another thread-specific data key, or the system-imposed limit on the total number of keys per process {PTHREAD_KEYS_MAX} has been exceeded.
[ENOMEM]
Insufficient memory exists to create the key.
The pthread_key_create() function shall not return an error code of [EINTR].
Example of pthread_key_create
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 <sched.h>
#include <stdio.h>
#include "check.h"

pthread_key_t        tlsKey = 0;

void globalDestructor(void *value)
{
  printf("In the data destructor\n");
  free(value);
  pthread_setspecific(tlsKey, NULL);
}


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

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

  printf("Create a thread local storage key\n");
  rc = pthread_key_create(&tlsKey, globalDestructor);
  checkResults("pthread_key_create()\n", rc);
  /* The key can now be used from all threads */

  printf("- The key can now be used from all threads\n");
  printf("- in the process to storage thread local\n");
  printf("- (but global to all functions in that thread)\n");
  printf("- storage\n");

  printf("Delete a thread local storage key\n");
  rc = pthread_key_delete(tlsKey);
  checkResults("pthread_key_delete()\n", rc);
  /* The key and any remaining values are now gone. */
  printf("Main completed\n");
  return 0;
}

Output:

Enter Testcase - QP0WTEST/TPKEYC0
Create a thread local storage key
- The key can now be used from all threads
- in the process to storage thread local
- (but global to all functions in that thread)
- storage
Delete a thread local storage key
Main completed

pthread_rwlockattr_init 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

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

pthread_rwlock_unlock example c c++


NAME

pthread_rwlock_unlock - unlock a read-write lock object

 SYNOPSIS

#include <pthread.h>

int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);

 DESCRIPTION

The pthread_rwlock_unlock() function is called to release a lock held on the read-write lock object referenced by rwlock. Results are undefined if the read-write lock rwlock is not held by the calling thread. (pthread_rwlock_unlock)If this function is called to release a read lock from the read-write lock object and there are other read locks currently held on this read-write lock object, the read-write lock object remains in the read locked state. If this function releases the calling thread's last read lock on this read-write lock object, then the calling thread is no longer one of the owners of the object. If this function releases the last read lock for this read-write lock object, the read-write lock object will be put in the unlocked state with no owners.
If this function is called to release a write lock for this read-write lock object, the read-write lock object will be put in the unlocked state with no owners.
If the call to the pthread_rwlock_unlock() function results in the read-write lock object becoming unlocked and there are multiple threads waiting to acquire the read-write lock object for writing, the scheduling policy is used to determine which thread acquires the read-write lock object for writing. If there are multiple threads waiting to acquire the read-write lock object for reading, the scheduling policy is used to determine the order in which the waiting threads acquire the read-write lock object for reading. If there are multiple threads blocked on rwlock for both read locks and write locks, it is unspecified whether the readers acquire the lock first or whether a writer acquires the lock first.
Results are undefined if any of these functions are called with an uninitialised read-write lock.

 RETURN VALUE

If successful, the pthread_rwlock_unlock() function returns zero. Otherwise, an error number is returned to indicate the error.

 ERRORS

The pthread_rwlock_unlock() function may fail if:
[EINVAL]
The value specified by rwlock does not refer to an initialised read-write lock object.
[EPERM]
The current thread does not own the read-write lock.

 EXAMPLES of pthread_rwlock_unlock

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

pthread_rwlock_t       rwlock;

void *rdlockThread(void *arg)
{
  int rc;

  printf("Entered thread, getting read lock\n");
  rc = pthread_rwlock_rdlock(&rwlock);
  checkResults("pthread_rwlock_rdlock()\n", rc);
  printf("got the rwlock read lock\n");

  sleep(5);

  printf("unlock the read lock\n");
  rc = pthread_rwlock_unlock(&rwlock);
  checkResults("pthread_rwlock_unlock()\n", rc);
  printf("Secondary thread unlocked\n");
  return NULL;
}

void *wrlockThread(void *arg)
{
  int rc;

  printf("Entered thread, getting write lock\n");
  rc = pthread_rwlock_wrlock(&rwlock);
  checkResults("pthread_rwlock_wrlock()\n", rc);

  printf("Got the rwlock write lock, now unlock\n");
  rc = pthread_rwlock_unlock(&rwlock);
  checkResults("pthread_rwlock_unlock()\n", rc);
  printf("Secondary thread unlocked\n");
  return NULL;
}

int main(int argc, char **argv)
{
  int                   rc=0;
  pthread_t             thread, thread1;

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

  printf("Main, initialize the read write lock\n");
  rc = pthread_rwlock_init(&rwlock, NULL);
  checkResults("pthread_rwlock_init()\n", rc);

  printf("Main, grab a read lock\n");
  rc = pthread_rwlock_rdlock(&rwlock);
  checkResults("pthread_rwlock_rdlock()\n",rc);

  printf("Main, grab the same read lock again\n");
  rc = pthread_rwlock_rdlock(&rwlock);
  checkResults("pthread_rwlock_rdlock() second\n", rc);

  printf("Main, create the read lock thread\n");
  rc = pthread_create(&thread, NULL, rdlockThread, NULL);
  checkResults("pthread_create\n", rc);

  printf("Main - unlock the first read lock\n");
  rc = pthread_rwlock_unlock(&rwlock);
  checkResults("pthread_rwlock_unlock()\n", rc);

  printf("Main, create the write lock thread\n");
  rc = pthread_create(&thread1, NULL, wrlockThread, NULL);
  checkResults("pthread_create\n", rc);

  sleep(5);
  printf("Main - unlock the second read lock\n");
  rc = pthread_rwlock_unlock(&rwlock);
  checkResults("pthread_rwlock_unlock()\n", rc);

  printf("Main, wait for the threads\n");
  rc = pthread_join(thread, NULL);
  checkResults("pthread_join\n", rc);

  rc = pthread_join(thread1, NULL);
  checkResults("pthread_join\n", rc);

  rc = pthread_rwlock_destroy(&rwlock);
  checkResults("pthread_rwlock_destroy()\n", rc);

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

Output:

Enter Testcase - QP0WTEST/TPRWLINI0
Main, initialize the read write lock
Main, grab a read lock
Main, grab the same read lock again
Main, create the read lock thread
Main - unlock the first read lock
Main, create the write lock thread
Entered thread, getting read lock
got the rwlock read lock
Entered thread, getting write lock
Main - unlock the second read lock
Main, wait for the threads
unlock the read lock
Secondary thread unlocked
Got the rwlock write lock, now unlock
Secondary thread unlocked
Main completed

pthread_rwlock_wrlock example c c++


NAME

pthread_rwlock_trywrlock, pthread_rwlock_wrlock - lock a read-write lock object for writing

SYNOPSIS

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

int pthread_rwlock_trywrlock(pthread_rwlock_t
 *rwlock);
int pthread_rwlock_wrlock(pthread_rwlock_t
 *rwlock); [Option End]

DESCRIPTION

The pthread_rwlock_trywrlock() function shall apply a write lock like the pthread_rwlock_wrlock() function, with the exception that the function shall fail if any thread currently holds rwlock (for reading or writing).
The pthread_rwlock_wrlock() function shall apply a write lock to the read-write lock referenced by rwlock. The calling thread acquires the write lock if no other thread (reader or writer) holds the read-write lock rwlock. Otherwise, the thread shall block until it can acquire the lock. The calling thread may deadlock if at the time the call is made it holds the read-write lock (whether a read or write lock).
Implementations may favor writers over readers to avoid writer starvation.
Results are undefined if any of these functions are called with an uninitialized read-write lock.(pthread_rwlock_trywrlock, pthread_rwlock_wrlock)
If a signal is delivered to a thread waiting for a read-write lock for writing, upon return from the signal handler the thread resumes waiting for the read-write lock for writing as if it was not interrupted.

RETURN VALUE

The pthread_rwlock_trywrlock() function shall return zero if the lock for writing on the read-write lock object referenced by rwlock is acquired. Otherwise, an error number shall be returned to indicate the error.
If successful, the pthread_rwlock_wrlock() function shall return zero; otherwise, an error number shall be returned to indicate the error.

ERRORS

The pthread_rwlock_trywrlock() function shall fail if:
[EBUSY]
The read-write lock could not be acquired for writing because it was already locked for reading or writing.
The pthread_rwlock_trywrlock() and pthread_rwlock_wrlock() functions may fail if:
[EINVAL]
The value specified by rwlock does not refer to an initialized read-write lock object.
The pthread_rwlock_wrlock() function may fail if:
[EDEADLK]
A deadlock condition was detected or the current thread already owns the read-write lock for writing or reading.
These functions shall not return an error code of [EINTR].
Example of (pthread_rwlock_trywrlock, pthread_rwlock_wrlock)
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       rwlock = PTHREAD_RWLOCK_INITIALIZER;

void *wrlockThread(void *arg)
{
  int             rc;
  int             count=0;

  printf("%.8x %.8x: Entered thread, getting write lock with timeout\n",
         pthread_getthreadid_np());
  Retry:
  rc = pthread_rwlock_trywrlock(&rwlock);
  if (rc == EBUSY) {
    if (count >= 10) {
      printf("%.8x %.8x: Retried too many times, failure!\n",
             pthread_getthreadid_np());
      exit(EXIT_FAILURE);
    }
    ++count;
    printf("%.8x %.8x: Go off an do other work, then RETRY...\n",
           pthread_getthreadid_np());
    sleep(1);
    goto Retry;
  }
  checkResults("pthread_rwlock_trywrlock() 1\n", rc);
  printf("%.8x %.8x: Got the write lock\n", pthread_getthreadid_np());

  sleep(2);

  printf("%.8x %.8x: Unlock the write lock\n",
         pthread_getthreadid_np());
  rc = pthread_rwlock_unlock(&rwlock);
  checkResults("pthread_rwlock_unlock()\n", rc);

  printf("%.8x %.8x: Secondary thread complete\n",
         pthread_getthreadid_np());
  return NULL;
}

int main(int argc, char **argv)
{
  int                   rc=0;
  pthread_t             thread, thread2;

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

  printf("Main, get the write lock\n");
  rc = pthread_rwlock_wrlock(&rwlock);
  checkResults("pthread_rwlock_wrlock()\n", rc);

  printf("Main, create the timed write lock threads\n");
  rc = pthread_create(&thread, NULL, wrlockThread, NULL);
  checkResults("pthread_create\n", rc);

  rc = pthread_create(&thread2, NULL, wrlockThread, NULL);
  checkResults("pthread_create\n", rc);

  printf("Main, wait a bit holding this write lock\n");
  sleep(1);

  printf("Main, Now unlock the write lock\n");
  rc = pthread_rwlock_unlock(&rwlock);
  checkResults("pthread_rwlock_unlock()\n", rc);

  printf("Main, wait for the threads to end\n");
  rc = pthread_join(thread, NULL);
  checkResults("pthread_join\n", rc);

  rc = pthread_join(thread2, NULL);
  checkResults("pthread_join\n", rc);

  rc = pthread_rwlock_destroy(&rwlock);
  checkResults("pthread_rwlock_destroy()\n", rc);
  printf("Main completed\n");
  return 0;
}

Output:

Enter Testcase - QP0WTEST/TPRWLWR1
Main, get the write lock
Main, create the timed write lock threads
00000000 0000000d: Entered thread, getting write lock with timeout
00000000 0000000d: Go off an do other work, then RETRY...
Main, wait a bit holding this write lock
00000000 0000000e: Entered thread, getting write lock with timeout
00000000 0000000e: Go off an do other work, then RETRY...
00000000 0000000d: Go off an do other work, then RETRY...
Main, Now unlock the write lock
Main, wait for the threads to end
00000000 0000000e: Got the write lock
00000000 0000000d: Go off an do other work, then RETRY...
00000000 0000000e: Unlock the write lock
00000000 0000000e: Secondary thread complete
00000000 0000000d: Got the write lock
00000000 0000000d: Unlock the write lock
00000000 0000000d: Secondary thread complete 
Main completed 

pthread_rwlock_trywrlock example c c++


NAME

pthread_rwlock_trywrlock, pthread_rwlock_wrlock - lock a read-write lock object for writing

SYNOPSIS

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

int pthread_rwlock_trywrlock(pthread_rwlock_t
 *rwlock);
int pthread_rwlock_wrlock(pthread_rwlock_t
 *rwlock); [Option End]

DESCRIPTION

The pthread_rwlock_trywrlock() function shall apply a write lock like the pthread_rwlock_wrlock() function, with the exception that the function shall fail if any thread currently holds rwlock (for reading or writing).
The pthread_rwlock_wrlock() function shall apply a write lock to the read-write lock referenced by rwlock. The calling thread acquires the write lock if no other thread (reader or writer) holds the read-write lock rwlock. Otherwise, the thread shall block until it can acquire the lock. The calling thread may deadlock if at the time the call is made it holds the read-write lock (whether a read or write lock).
Implementations may favor writers over readers to avoid writer starvation.
Results are undefined if any of these functions are called with an uninitialized read-write lock.(pthread_rwlock_trywrlock, pthread_rwlock_wrlock)
If a signal is delivered to a thread waiting for a read-write lock for writing, upon return from the signal handler the thread resumes waiting for the read-write lock for writing as if it was not interrupted.

RETURN VALUE

The pthread_rwlock_trywrlock() function shall return zero if the lock for writing on the read-write lock object referenced by rwlock is acquired. Otherwise, an error number shall be returned to indicate the error.
If successful, the pthread_rwlock_wrlock() function shall return zero; otherwise, an error number shall be returned to indicate the error.

ERRORS

The pthread_rwlock_trywrlock() function shall fail if:
[EBUSY]
The read-write lock could not be acquired for writing because it was already locked for reading or writing.
The pthread_rwlock_trywrlock() and pthread_rwlock_wrlock() functions may fail if:
[EINVAL]
The value specified by rwlock does not refer to an initialized read-write lock object.
The pthread_rwlock_wrlock() function may fail if:
[EDEADLK]
A deadlock condition was detected or the current thread already owns the read-write lock for writing or reading.
These functions shall not return an error code of [EINTR].
Example of (pthread_rwlock_trywrlock, pthread_rwlock_wrlock)
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       rwlock = PTHREAD_RWLOCK_INITIALIZER;

void *wrlockThread(void *arg)
{
  int             rc;
  int             count=0;

  printf("%.8x %.8x: Entered thread, getting write lock with timeout\n",
         pthread_getthreadid_np());
  Retry:
  rc = pthread_rwlock_trywrlock(&rwlock);
  if (rc == EBUSY) {
    if (count >= 10) {
      printf("%.8x %.8x: Retried too many times, failure!\n",
             pthread_getthreadid_np());
      exit(EXIT_FAILURE);
    }
    ++count;
    printf("%.8x %.8x: Go off an do other work, then RETRY...\n",
           pthread_getthreadid_np());
    sleep(1);
    goto Retry;
  }
  checkResults("pthread_rwlock_trywrlock() 1\n", rc);
  printf("%.8x %.8x: Got the write lock\n", pthread_getthreadid_np());

  sleep(2);

  printf("%.8x %.8x: Unlock the write lock\n",
         pthread_getthreadid_np());
  rc = pthread_rwlock_unlock(&rwlock);
  checkResults("pthread_rwlock_unlock()\n", rc);

  printf("%.8x %.8x: Secondary thread complete\n",
         pthread_getthreadid_np());
  return NULL;
}

int main(int argc, char **argv)
{
  int                   rc=0;
  pthread_t             thread, thread2;

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

  printf("Main, get the write lock\n");
  rc = pthread_rwlock_wrlock(&rwlock);
  checkResults("pthread_rwlock_wrlock()\n", rc);

  printf("Main, create the timed write lock threads\n");
  rc = pthread_create(&thread, NULL, wrlockThread, NULL);
  checkResults("pthread_create\n", rc);

  rc = pthread_create(&thread2, NULL, wrlockThread, NULL);
  checkResults("pthread_create\n", rc);

  printf("Main, wait a bit holding this write lock\n");
  sleep(1);

  printf("Main, Now unlock the write lock\n");
  rc = pthread_rwlock_unlock(&rwlock);
  checkResults("pthread_rwlock_unlock()\n", rc);

  printf("Main, wait for the threads to end\n");
  rc = pthread_join(thread, NULL);
  checkResults("pthread_join\n", rc);

  rc = pthread_join(thread2, NULL);
  checkResults("pthread_join\n", rc);

  rc = pthread_rwlock_destroy(&rwlock);
  checkResults("pthread_rwlock_destroy()\n", rc);
  printf("Main completed\n");
  return 0;
}

Output:

Enter Testcase - QP0WTEST/TPRWLWR1
Main, get the write lock
Main, create the timed write lock threads
00000000 0000000d: Entered thread, getting write lock with timeout
00000000 0000000d: Go off an do other work, then RETRY...
Main, wait a bit holding this write lock
00000000 0000000e: Entered thread, getting write lock with timeout
00000000 0000000e: Go off an do other work, then RETRY...
00000000 0000000d: Go off an do other work, then RETRY...
Main, Now unlock the write lock
Main, wait for the threads to end
00000000 0000000e: Got the write lock
00000000 0000000d: Go off an do other work, then RETRY...
00000000 0000000e: Unlock the write lock
00000000 0000000e: Secondary thread complete
00000000 0000000d: Got the write lock
00000000 0000000d: Unlock the write lock
00000000 0000000d: Secondary thread complete 
Main completed 

pthread_rwlock_tryrdlock example c c++


NAME

pthread_rwlock_rdlock, pthread_rwlock_tryrdlock - lock a read-write lock object for reading

 SYNOPSIS

#include <pthread.h>

int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);
int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);

 DESCRIPTION of (pthread_rwlock_rdlock, pthread_rwlock_tryrdlock)

The pthread_rwlock_rdlock() function applies a read lock to the read-write lock referenced by rwlock. The calling thread acquires the read lock if a writer does not hold the lock and there are no writers blocked on the lock. It is unspecified whether the calling thread acquires the lock when a writer does not hold the lock and there are writers waiting for the lock. If a writer holds the lock, the calling thread will not acquire the read lock. If the read lock is not acquired, the calling thread blocks (that is, it does not return from the pthread_rwlock_rdlock() call) until it can acquire the lock. Results are undefined if the calling thread holds a write lock on rwlockat the time the call is made.Implementations are allowed to favour writers over readers to avoid writer starvation.
A thread may hold multiple concurrent read locks on rwlock (that is, successfully call the pthread_rwlock_rdlock() function ntimes). If so, the thread must perform matching unlocks (that is, it must call the pthread_rwlock_unlock() function n times).
The function pthread_rwlock_tryrdlock() applies a read lock as in the pthread_rwlock_rdlock() function with the exception that the function fails if any thread holds a write lock on rwlock or there are writers blocked on rwlock.
Results are undefined if any of these functions are called with an uninitialised read-write lock.
If a signal is delivered to a thread waiting for a read-write lock for reading, upon return from the signal handler the thread resumes waiting for the read-write lock for reading as if it was not interrupted.

 RETURN VALUE

If successful, the pthread_rwlock_rdlock() function returns zero. Otherwise, an error number is returned to indicate the error.The function pthread_rwlock_tryrdlock() returns zero if the lock for reading on the read-write lock object referenced by rwlock is acquired. Otherwise an error number is returned to indicate the error.

 ERRORS

The pthread_rwlock_tryrdlock() function will fail if:
[EBUSY]
The read-write lock could not be acquired for reading because a writer holds the lock or was blocked on it.
The pthread_rwlock_rdlock() and pthread_rwlock_tryrdlock() functions may fail if:
[EINVAL]
The value specified by rwlock does not refer to an initialised read-write lock object.
[EDEADLK]
The current thread already owns the read-write lock for writing.
[EAGAIN]
The read lock could not be acquired because the maximum number of read locks for rwlock has been exceeded.
Example of (pthread_rwlock_rdlock, pthread_rwlock_tryrdlock)
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       rwlock = PTHREAD_RWLOCK_INITIALIZER;

void *rdlockThread(void *arg)
{
  int             rc;
  int             count=0;

  printf("Entered thread, getting read lock with mp wait\n");
  Retry:
  rc = pthread_rwlock_tryrdlock(&rwlock);
  if (rc == EBUSY) {
    if (count >= 10) {
      printf("Retried too many times, failure!\n");

      exit(EXIT_FAILURE);
    }
    ++count;
    printf("Could not get lock, do other work, then RETRY...\n");
    sleep(1);
    goto Retry;
  }
  checkResults("pthread_rwlock_tryrdlock() 1\n", rc);

  sleep(2);

  printf("unlock the read lock\n");
  rc = pthread_rwlock_unlock(&rwlock);
  checkResults("pthread_rwlock_unlock()\n", rc);

  printf("Secondary thread complete\n");
  return NULL;
}

int main(int argc, char **argv)
{
  int                   rc=0;
  pthread_t             thread;

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

  printf("Main, get the write lock\n");
  rc = pthread_rwlock_wrlock(&rwlock);
  checkResults("pthread_rwlock_wrlock()\n", rc);

  printf("Main, create the try read lock thread\n");
  rc = pthread_create(&thread, NULL, rdlockThread, NULL);
  checkResults("pthread_create\n", rc);

  printf("Main, wait a bit holding the write lock\n");
  sleep(5);

  printf("Main, Now unlock the write lock\n");
  rc = pthread_rwlock_unlock(&rwlock);
  checkResults("pthread_rwlock_unlock()\n", rc);

  printf("Main, wait for the thread to end\n");
  rc = pthread_join(thread, NULL);
  checkResults("pthread_join\n", rc);

  rc = pthread_rwlock_destroy(&rwlock);
  checkResults("pthread_rwlock_destroy()\n", rc);
  printf("Main completed\n");
  return 0;
}

Output

Enter Testcase - QP0WTEST/TPRWLRD1
Main, get the write lock
Main, create the try read lock thread
Main, wait a bit holding the write lock

Entered thread, getting read lock with mp wait
Could not get lock, do other work, then RETRY...
Could not get lock, do other work, then RETRY...
Could not get lock, do other work, then RETRY...
Could not get lock, do other work, then RETRY...
Could not get lock, do other work, then RETRY...
Main, Now unlock the write lock
Main, wait for the thread to end
unlock the read lock
Secondary thread complete
Main completed