1. features:
Only one thread can occupy the read/write lock in the write mode at a time, but multiple threads can simultaneously occupy the read/write lock in the Read mode.Because of this feature,
- When the read/write lock is in the write lock status, all threads trying to lock the lock will be blocked before the lock is unlocked.
- When a read/write lock is in the locking status, all threads that attempt to lock it in the Read mode can obtain the access permission. However, if the thread wants to lock the lock in the write mode, it must be blocked to know that all threads release the lock.
- Generally, when a read/write lock is locked in Read mode, if another thread attempts to lock the lock in write mode, the read/write lock will usually block subsequent read Mode Lock requests, in this way, the Read mode lock can be used for a long time, while the waiting write mode lock requests will be blocked for a long time.
2. Applicability:
The read/write lock is suitable for the case where the number of reads to the data structure is much higher than the number of writes. because the read-write lock can be shared at regular intervals and exclusive when it is locked in write mode, the read-write lock is also called share-exclusive lock.
3. Initialization and destruction:
# Include <pthread. h>
Int pthread_rwlock_init (pthread_rwlock_t * restrict rwlock, const pthread_rwlockattr_t * restrict ATTR );
Int pthread_rwlock_destroy (pthread_rwlock_t * rwlock );
If the call succeeds, 0 is returned. If the call fails, the error number is returned.
Above mutex, before releasing the memory occupied by read/write locks, you must clear the read/write locks through pthread_rwlock_destroy to release the resources allocated by init.
4. Read and Write:
# Include <pthread. h>
Int pthread_rwlock_rdlock (pthread_rwlock_t * rwlock );
Int pthread_rwlock_wrlock (pthread_rwlock_t * rwlock );
Int pthread_rwlock_unlock (pthread_rwlock_t * rwlock );
If the call succeeds, 0 is returned. If the call fails, the error number is returned.
These three functions implement read lock acquisition, write lock acquisition, and lock release Operations respectively. The two functions used to obtain the lock are blocking operations. Similarly, the non-blocking functions are:
# Include <pthread. h>
Int pthread_rwlock_tryrdlock (pthread_rwlock_t * rwlock );
Int pthread_rwlock_trywrlock (pthread_rwlock_t * rwlock );
If the call succeeds, 0 is returned. If the call fails, the error number is returned.
Non-blocking lock acquisition operation. If yes, 0 is returned; otherwise, an error ebusy is returned.
5. Set Properties
# Include <pthread. h> int pthread_rwlockattr_init (pthread_rwlockattr_t* ATTR); Int pthread_rwlockattr_destroy (pthread_rwlockattr_t* ATTR);
Int pthread_rwlockattr_getpshared (const pthread_rwlockattr_t * restrictATTR, Int * restrict Pshared);
Int pthread_rwlockattr_setpshared (pthread_rwlockattr_t* ATTR,IntPshared);
Pthread_rwlockattr_setkind_np(ATTR, Pref)
Reference: http://blog.csdn.net/dai_weitao/archive/2007/08/21/1752843.aspx