Friday, April 26, 2013

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