|
Syntax #include <pthread.h> int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock); Threadsafe: Yes Signal Safe: Yes |
The pthread_rwlock_wrlock() function attempts to acquire an exclusive write lock on the read/write lock specified by rwlock.
Only one thread can hold an exclusive write lock on a 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 or shared read lock on the read/write lock, the calling thread will successfully acquire the exclusive write lock.
If the calling thread already holds an exclusive write lock on the read/write lock, another write lock can be successfully acquired by the calling thread. If more than one exclusive write 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 subsequent attempts to acquire a shared read lock request will be granted, while attempts to acquire the exclusive write lock will wait.
If the read/write lock is destroyed while pthread_rwlock_wrlock() is waiting for the shared read lock, the EDESTROYED error is returned.
If a signal is delivered to the thread while it is waiting for the lock, the signal handler (if any) runs, and the thread resumes waiting.
If a thread terminates 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 not return and will deadlock.
For the pthread_rwlock_wrlock() function, the pthreads run-time simulates the deadlock that has occurred in your application. When attempting to debug these deadlock scenarios, the CL command WRKJOB, option 20 will show the thread as in a condition wait. Displaying the call stack will show that the function timedDeadlockOnOrphanedRWLock is in the call stack.
If the calling thread currently holds a shared read lock on the read/write lock object and there are no other threads holding a shared read lock, the exclusive write request will be granted. After the exclusive write 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" on page 36
None.
If pthread_rwlock_wrlock() 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.
See the pthread_rwlock_init() example.