Multi-thread programming-read/write locks
PthreadPOSIX threads is short for POSIXThread Standard.The pthread read/write lock divides visitors to shared resources into readers and writers. Readers only read and access shared resources and Writers only write shared resources. In the mutex mechanism, both the reader and the writer need to independently exclusive mutex to exclusively share resources. In the read/write lock mechanism, multiple readers are allowed to read and access Shared resources at the same time, only the writer needs to exclusively own the resource. Compared with the mutex mechanism, the read/write mechanism allows multiple readers to read and share resources at the same time, further improving the concurrency of multiple threads.1. read/write lock mechanism:
Writer: The writer uses the write lock. If there are no readers or other writers, the writer immediately acquires the write lock. Otherwise, the writer waits until no readers or writers are involved.Reader: the reader uses a read lock. If no writer is present, the reader immediately acquires the read lock; otherwise, the reader waits until no writer is present.2. read/write lock features:
At the same time, only one thread can obtain the write lock. At the same time, multiple threads can obtain the read lock.When the read/write lock is out of the write lock status, all threads that attempt to lock the read/write lock will be blocked, whether the reader attempts to apply the read lock or the writer tries to apply the write lock.When a writer tries to add a write lock when the read/write lock is in the read lock status, the read/write requests of other later threads will be blocked to prevent the writer from locking the lock for a long time.3. Basic read/write lock functions: # include <pthread. h>Read/write lock initialization:Intpthread_rwlock_init (pthread_rwlock_t * rwlock,Const pthread_rwlockattr_t * attr );The first parameter of this function is the read/write lock pointer, and the second parameter is the read/write lock attribute pointer. The function initializes the read/write Lock Based on the read/write lock attribute.Read lock:Intpthread_rwlock_rdlock (pthread_rwlock_t * rwlock );This function parameter is a read/write lock pointer. A function is used to add a read/write lock to a read/write lock.Write lock:Intpthread_rwlock_wrlock (pthread_rwlock_t * rwlock );This function parameter is a read/write lock pointer. The function is used to add a write lock to the read/write lock.Release the read/write lock:Intpthread_rwlock_unlock (pthread_rwlock_t * rwlock );This function parameter is a read/write lock pointer. The function is used to release read/write locks, including read and write locks.Delete read/write locks:Intpthread_rwlock_destroy (pthread_rwlock_t * rwlock );This function parameter is a read/write lock pointer. The function is used to destroy read/write locks.
4. Cow test:In this example, read/write locks are used to synchronize read and write data of shared resources. The readerM thread, readerN is the reader thread, writerA thread, and writerB is the writer thread.