Shared read/write locks are released at thread termination
If a thread is the owner of one or more shared read locks acquired by pthread_rwlock_unlock(), pthread_rwlock_tryrdlock(), or pthread_rwlock_timedrdlock_np(), when that thread terminates, the shared read locks are automatically released by the system. If a thread holds a shared read lock, it does not modify the resources associated with that lock. It is then safe for the runtime support to unlock the read lock without indicating an error condition or causing the process to wait. For performance reasons, your application should unlock all held locks before the thread ends.
If a thread is the owner of one or more exclusive write locks acquired by pthread_rwlock_wrlock(), pthread_rwlock_trywrlock(), or pthread_rwlock_timedwrlock_np(), when that thread terminates, the exclusive write locks are not automatically released by the system. This is an error in the application and indicates that the data associated with the lock is in an inconsistent state. If another thread attempts to get a shared read or exclusive write lock on the lock, that thread blocks forever.
Read/write locks can be upgraded/downgraded
The IBM® i implementation of read/write locks allows a thread to effectively change a read lock to a write lock, or change a write lock to a read lock, without an intervening unlocked and unprotected section of code. The following items describe read/write lock behavior that allows these changes. This behavior is outside of the definition of the Single UNIX® Specification. An application written to be portable to the Single UNIX Specification should not attempt to acquire a shared read lock and a shared write lock on the same read/write lock at the same time.
- If a thread currently holds a shared read lock, an attempt by the same thread to acquire an exclusive write lock succeeds if no other threads hold a shared read lock. The thread then holds both an exclusive write lock and a shared read lock.
- If a thread currently holds an exclusive write lock, an attempt by the thread to acquire a shared read lock succeeds. The thread then holds both an exclusive write lock and a shared read lock.
- If a thread holds one or more shared read locks and one or more exclusive write locks on the same read/write lock object at the same time, a call to pthread_rwlock_unlock() always unlocks the exclusive write lock FIRST.
- When multiple exclusive write locks and multiple exclusive read locks are
held by the same thread on the same read/write lock object, the behavior of
pthread_rwlock_unlock() is as follows:
- A call to the pthread_rwlock_unlock() function always unlocks the most recent exclusive write lock first.
- Subsequent calls to pthread_rwlock_unlock() first reduce the count of any outstanding exclusive write locks held by the thread until all exclusive write locks are unlocked.
- After all outstanding exclusive write locks are unlocked and the thread holds only shared read locks on the read/write lock object, a call to pthread_rwlock_unlock() function then unlocks the most recent shared read lock.
- Subsequent calls to pthread_rwlock_unlock() reduce the count of any outstanding shared read locks held by the thread until all shared read locks are unlocked.
For a thread to change a shared read lock to an exclusive write lock, the thread should perform the following actions:
{ pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER; pthread_rwlock_rdlock(&rwlock); ... /* Thread holding a read lock decides it needs to upgrade to a write lock */ /* Now Upgrade to write lock */ pthread_rwlock_wrlock(&rwlock); ... /* write lock (and read lock) are held here.*/ /* We have effectively upgraded to a write lock */ ... /* `Downgrade' back to a only the read lock */ pthread_rwlock_unlock(&rwlock); ... /* unlock the read lock */ pthread_rwlock_unlock(&rwlock); }
For a thread to change an exclusive write lock to a shared read lock, the thread should perform the following actions:
{ pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER; pthread_rwlock_wrlock(&rwlock); .. /* Thread holding the write lock decides it needs to downgrade to a read lock */ /* Get the read lock, so we are holding BOTH read and write locks */ pthread_rwlock_rdlock(&rwlock); ... /* An unlock always unlocks the write lock first */ pthread_wrlock_unlock(&rwlock); ... /* At this point, we are only holding the read lock. */ /* We have effectively downgraded the write lock to a read lock */ ... /* Use unlock to unlock the last read lock. */ pthread_wrlock_unlock(&rwlock); }
[ Back to top | Pthread APIs | APIs by category ]