Read-write Lock (Read-write lock) mechanism-----The solution of multithreading synchronization problem

Source: Internet
Author: User
Tags posix

Read/write Lock (Read-write lock)
A review
In some programs there are reader-writer problems, that is, access to some resources there are two possible situations, one is that access must be exclusive line, that is, the meaning of exclusivity, which is called the write operation, the other is that access can be shared, that is, you can have multiple threads to access a resource at the same time, This is called a read operation. This problem model is derived from the read and write operation of the file.
Read-write locks are more adaptable than mutexes, have higher parallelism, and can have multiple threads concurrently occupying read-mode reading and writing locks, but only one thread consumes write-mode read-write locks and read-write locks in three states:
1. When the read-write lock is write-locked, all threads attempting to lock the lock will be blocked until the lock is unlocked.
2. When the read-write lock is in the locking state, all threads attempting to lock it in read mode can gain access, but threads that lock it in write mode will be blocked
3. When a read-write lock is in the lock state of reading mode, if another thread attempts to lock in write mode, the read-write lock usually blocks the subsequent read-mode lock request, which avoids the long-term use of the read-mode lock, while the waiting write-mode lock request is blocked for a long time.
Read-write locks are most suitable for applications where there are more read operations than write operations on data structures, because read mode locks can be shared, while write mode locks on only one thread exclusive of resources, so a read-write lock can also be called a shared-exclusive lock.
Two common strategies for dealing with reader-writer issues are strong reader synchronization (strong reader synchronization) and strong writer synchronization (strong writer synchronization). In the strong reader synchronization, always give the reader a higher priority, as long as the writer is not currently writing, the reader can gain access, and in the strong writer synchronization, the priority is often delivered to the writer, and the reader can only wait until all are waiting or is executing after the end of the writer can be executed. In the reader-writer model, because the reader often requests to view the latest information records, the flight booking system often uses the strong writer synchronization strategy, while the library access system adopts the strong reader synchronization strategy.
The read-write lock mechanism is provided by POSIX, and if the writer does not hold a read-write lock, then all readers can hold the lock, and once a writer is blocked in the lock, it is up to the POSIX system to decide whether to allow the reader to acquire the lock.
Two read-write lock related APIs
1. Initialize and destroy read-write locks
There are two ways to initialize a read-write lock variable, one is to initialize it by assigning a static read-write lock to a constant pthread_rwlock_initializer, and the other is to initialize it dynamically by calling Pthread_rwlock_init (). When a thread no longer needs a read-write lock, the lock can be destroyed by calling Pthread_rwlock_destroy. The function prototypes are as follows:
#include <pthread.h>
int Pthread_rwlock_init (pthread_rwlock_t *rwptr, const pthread_rwlockattr_t *attr);
int Pthread_rwlock_destroy (pthread_rwlock_t *rwptr);
Both functions return 0 if the execution succeeds, and an error code is returned if an error occurs.
Before releasing the memory occupied by a read-write lock, the read-write lock is cleaned by Pthread_rwlock_destroy and the resources allocated by Pthread_rwlock_init are freed.
When initializing a read-write lock, if the property pointer attr is a null pointer, it represents the default property, and if you want to use a non-default property, use the following two functions:
#include <pthread.h>
int Pthread_rwlockattr_init (pthread_rwlockattr_t *attr);
int Pthread_rwlockattr_destroy (pthread_rwlockatttr_t *attr);
These two functions are the same, and if the execution returns 0 successfully, the error code is returned.
It is also necessary to note that the lock is in a non-locked state after the initialization of the read-write lock is complete.
When a Property object with a data type of pthread_rwlockattr_t is initialized, it is possible to enable or disable a particular property by using a different function tune.
2. Acquiring and releasing read and write locks
The data type of a read-write lock is pthread_rwlock_t, and if a variable in that data type is statically allocated, it can be initialized by assigning it a constant value Pthread_rwlock_initializar. Pthread_rwlock_rdlock () is used to obtain a read-out lock, blocking the calling thread if the corresponding read-out lock is already occupied by a writer. Pthread_rwlock_wrlock () is used to obtain a write lock that, if the corresponding write lock is already occupied by another writer or one or more readers, blocks the calling thread; Pthread_rwlock_unlock () is used to release a read or write lock. The function prototypes are as follows:
#include <pthread.h>
int Pthread_rwlock_rdlock (pthread_rwlock_t *rwptr);
int Pthread_rwlock_wrlock (pthread_rwlock_t *rwptr);
int Pthread_rwlock_unlock (pthread_rwlock_t *rwptr);
The three function returns 0 if the call succeeds, and the error code is returned if it fails. It is important to note that the operation of the two functions in which the lock is acquired is a blocking operation, that is, if the lock is not obtained, then the calling thread does not return immediately, but instead blocks execution. In the case of writing, this blocking approach may not be very suitable, so the next step is to introduce two functions Pthread_rwlock_tryrdlock () and Pthread_rwlock_trywrlock () that take a non-blocking approach to read and write locks, When a lock is acquired in a non-blocking manner, a ebusy error is returned immediately if it cannot be obtained immediately, instead of putting the calling thread into a sleep wait. The function prototypes are as follows:
#include <pthread.h>
int Pthread_rwlock_tryrdlock (pthread_rwlock_t *rwptr);
int Pthread_rwlock_trywrlock (pthread_rwlock_t *rwptr);
Similarly, these two function calls successfully return 0 and fail to return an error code.
Three examples
Reader-writer model to realize multithreading synchronization problem (original Http://hi.baidu.com/tekuba/item/d67e82ce52444522e90f2ee8)
Https://github.com/helianthuslulu/LINUX_IPC

Read-write Lock (Read-write lock) mechanism-----The solution of multithreading synchronization problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.