|
|
cc [options] -Kthread file#include <pthread.h>
int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);
int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);
If the lock is free, or is currently held by another reader and there are no writers waiting, pthread_rwlock_rdlock increments the reader count and the caller proceeds. If a writer holds the lock or if any writer is waiting for the lock, the caller blocks to wait for the lock.
From the point of view of the caller, this function is atomic: even if interrupted by a signal or forkall (see fork(S)), pthread_rwlock_rdlock will not return until it holds the lock. As a consequence, if pthread_rwlock_rdlock is interrupted, an error indication, such as EINTR, is never returned to the user.
pthread_rwlock_tryrdlock is used when the caller does not want to block if the lock is unavailable. It attempts once to acquire the read-write lock pointed to by lock in read mode; it does not block the caller if the lock is unavailable.
If the lock is free or is currently held by another reader and there are no writers waiting, pthread_rwlock_tryrdlock increments the reader count and the caller proceeds.
If the lock is currently held by a writer, or there are writers waiting, pthread_rwlock_tryrdlock immediately returns EBUSY to the caller, without acquiring the lock.
Results are undefined if either of these functions is called with an uninitialized read-write lock.
A read-write lock can be held by any number of readers at one time, but only one writer at a time can hold the lock. Once a writer has requested the lock with pthread_rwlock_wrlock, all subsequent requests for the lock in either read or write mode are queued.
A thread may hold multiple concurrent read locks on rwlock (that is, successfully call the pthread_rwlock_rdlock function n times). If so, the thread must perform matching unlocks (that is, it must call the pthread_rwlock_unlock function n times). Deadlock can occur here so pthread_rwlock_tryrdlock should only be used when acquiring more than one read lock on any rwlock.
For consistency, locks acquired with pthread_rwlock_rdlock and pthread_rwlock_tryrdlock should be released with pthread_rwlock_unlock.
pthread_rwlock_rdlock returns the following value if the corresponding condition is detected:
pthread_rwlock_tryrdlock returns the following value if the corresponding condition is detected: