Read/write lock synchronization APIs
Read/write locks help you build more complex applications without using mutexes and condition variables to provide your own read/write locking primitive object. Read/Write locks provide a synchronization mechanism that allow threads in an application to more accurately reflect the type of access to a shared resource that they require.
Many threads can acquire the same read/write lock if they acquire a shared read lock on the read/write lock object. Only one thread can acquire an exclusive write lock on a read/write lock object. When an exclusive write lock is held, no other threads are allowed to hold any lock.
The table below lists important read/write lock attributes, their default values, and all supported values.
Attribute |
Default value |
Supported values |
pshared |
PTHREAD_PROCESS_PRIVATE |
PTHREAD_PROCESS_PRIVATE or PTHREAD_PROCESS_SHARED |
For information about the examples included with the APIs, see the information on the API examples.
The Read/write lock synchronization APIs are:
- pthread_rwlock_destroy() (Destroy Read/Write Lock) destroys the named read/write lock.
- pthread_rwlock_init() (Initialize Read/Write Lock) initializes a new read/write lock with the specified attributes for use.
- pthread_rwlock_rdlock() (Get Shared Read Lock) attempts to acquire a shared read lock on the read/write lock specified by rwlock.
- pthread_rwlock_timedrdlock_np() (Get Shared Read Lock with Time-Out) attempts to acquire a shared read lock on the read/write lock specified by rwlock.
- pthread_rwlock_timedwrlock_np() (Get Exclusive Write Lock with Time-Out) attempts to acquire an exclusive write lock on the read/write lock specified by rwlock.
- pthread_rwlock_tryrdlock() (Get Shared Read Lock with No Wait) attempts to acquire a shared read lock on the read/write lock specified by rwlock.
- pthread_rwlock_trywrlock() (Get Exclusive Write Lock with No Wait) attempts to acquire an exclusive write lock on the read/write lock specified by rwlock.
- pthread_rwlock_unlock() (Unlock Exclusive Write or Shared Read Lock) unlocks a shared read or exclusive write lock held by the calling thread.
- pthread_rwlock_wrlock() (Get Exclusive Write Lock) attempts to acquire an exclusive write lock on the read/write lock specified by rwlock.
- pthread_rwlockattr_destroy() (Destroy Read/Write Lock Attribute) destroys a read/write lock attributes object and allows the systems to reclaim any resources associated with that read/write lock attributes object.
- pthread_rwlockattr_getpshared() (Get Pshared Read/Write Lock Attribute) retrieves the current setting of the process shared attribute from the read/write lock attributes object.
- pthread_rwlockattr_init() (Initialize Read/Write Lock Attribute) initializes the read/write lock attributes object referred to by attr to the default attributes.
- pthread_rwlockattr_setpshared() (Set Pshared Read/Write Lock Attribute) sets the current pshared attribute for the read/write attributes object.