pthread_rwlock_tryrdlock()--Get Shared Read Lock with No Wait


  Syntax:
 #include <pthread.h>
 int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);   
  Service Program Name: QP0WPTHR

  Default Public Authority: *USE

  Threadsafe: Yes

  Signal Safe: Yes

The pthread_rwlock_tryrdlock() function attempts to acquire a shared read lock on the read/write lock specified by rwlock. If the shared read lock cannot be acquired immediately, pthread_rwlock_tryrdlock() returns the EBUSY error.

Any number of threads can hold shared read locks on the same read/write lock object. If any thread holds an exclusive write lock on a read/write lock object, no other threads will be allowed to hold a shared read or exclusive write lock.

If there are no threads holding an exclusive write lock on the read/write lock, the calling thread will successfully acquire the shared read lock.

If the calling thread already holds a shared read lock on the read/write lock, another read lock can be successfully acquired by the calling thread. If more than one shared read lock is successfully acquired by a thread on a read/write lock object, that thread is required to successfully call pthread_rwlock_unlock() a matching number of times.

With a large number of readers, and relatively few writers, there is the possibility of writer starvation. If there are threads waiting for an exclusive write lock on the read/write lock and there are threads that currently hold a shared read lock, the shared read lock request will be granted.


Read/Write Lock Deadlocks

If a thread ends while holding a write lock, the attempt by another thread to acquire a shared read or exclusive write lock will not be successful. In this case, the attempt to acquire the lock will return the EBUSY error. If a thread ends while holding a read lock, the system automatically releases the read lock.


Upgrade / Downgrade a Lock

If the calling thread currently holds an exclusive write lock on the read/write lock object, the shared read lock request will be granted. After the shared read lock request is granted, the calling thread holds both the shared read, and the exclusive write lock for the specified read/write lock object. If the thread calls pthread_rwlock_unlock() while holding one or more shared read locks and one or more exclusive write locks, the exclusive write locks are unlocked first. If more than one outstanding exclusive write lock was held by the thread, a matching number of successful calls to pthread_rwlock_unlock() must be done before all write locks are unlocked. At that time, subsequent calls to pthread_rwlock_unlock() will unlock the shared read locks.

This behavior can be used to allow your application to upgrade or downgrade one lock type to another. See Read/write locks can be upgraded/downgraded.


Authorities and Locks

None.


Parameters

rwlock
(Input) The address of the read/write lock

Return Value

0
pthread_rwlock_tryrdlock() was successful.
value
pthread_rwlock_tryrdlock() was not successful. value is set to indicate the error condition.

Error Conditions

If pthread_rwlock_tryrdlock() was not successful, the error condition returned usually indicates one of the following errors. Under some conditions, the value returned could indicate an error other than those listed here.

[EINVAL]

The value specified for the argument is not correct.

[EBUSY]

The lock could not be immediately acquired.


Related Information


Example

See Code disclaimer information for information pertaining to code examples.

#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

API introduced: V4R3
Top | Pthread APIs | APIs by category