Linux Environment Programming Synchronization (III): Read/write lock

Source: Internet
Author: User

Overview

The mutex blocks all other threads attempting to enter what we call the critical section. This critical section typically involves access or updates that share one or more data by these threads. A read-write lock is used to read a reading and writing lock for reading a data and acquiring a read-write lock for writing directly. The allocation rules for read and write locks are as follows:

1. As long as no thread holds a given read-write lock for writing, any number of threads can hold the read-write lock for reading.

2. Only if no thread holds a given read-write lock for reading or for writing, the read-write lock can be allocated for writing.

That is, as long as no thread is modifying a given data, any number of threads can have read access to that data. The current thread can modify a given data only if no other thread is reading or modifying it.

This shared access to a given resource is also known as shared-exclusive lockout , because acquiring a read-write lock for reading is called a shared lock , and acquiring a read-write lock for writing is called an exclusive lock .

Acquiring and releasing read-write locks

The data type of the read-write lock is pthread_rwlock_t. If a variable of this type is statically assigned, it can be initialized by assigning it a constant value of pthread_rwlock_initializer.

Pthread_rwlock_rdlock gets a read-out lock that blocks the calling thread if the corresponding read-write lock is already held by a writer. Pthread_rwlock_wrlock gets a write lock that blocks the calling thread if the corresponding read-write lock has been held by another writer or has been held by one or more readers. Pthread_rwlock_unlock releases a read-out lock or write lock.

#include <pthread.h>intpthread_rwlock_rdlock (pthread_rwlock_t *rwptr); Intpthread_rwlock_wrlock (Pthread_ rwlock_t *rwptr); Intpthread_rwlock_unlock (pthread_rwlock_t *rwptr)    ; Successful return 0, error returned as positive exxx value.
The following two functions attempt to acquire a read or write lock, but if the lock cannot be obtained immediately, it returns a ebusy error instead of the calling thread putting into sleep.

#include <pthread.h>intpthread_rwlock_tryrdlock (pthread_rwlock_t *rwptr); Intpthread_rwlock_trywrlock ( pthread_rwlock_t *rwptr); Successful return 0, error return positive value
read-write lock Properties
Initializes a statically assigned read-write lock assignment Pthread_rwlock_initializer. Read-write lock variables can also be dynamically initialized by calling Pthread_rwlock_init. When a read-write lock is no longer needed by a thread, the Pthread_rwlock_destroy can be called to destroy it.

#include <pthread.h>int pthread_rwlock_init (pthread_rwlock_t *rwptr, const pthread_rwlockattr_t *attr); int Pthread_rwlock_destroy (pthread_rwlock_t *rwptr);//success is 0, error is positive
When a read-write lock is initialized, the default property is used if attr is a null pointer. To give it a non-default property, you need to use the following two functions:

#include <pthread.h>int pthread_rwlockattr_init (pthread_rwlockattr_t *rwptr); int Pthread_rwlockattr_destroy ( pthread_rwlockattr_t *rwptr);//success is 0, error is positive
When a Property object with a data type of pthread_rwlockattr_t is initialized, a specific property is enabled or suppressed by invoking a different function. The only property currently defined is pthread_process_shared, which specifies that the corresponding read-write lock will be shared between different processes, not just among different threads within a single process. The following two functions get and set this property, respectively.

#include <pthread.h>int pthread_rwlockattr_getpshared (const pthread_rwlockattr_t *attr, int *valptr); int pthread_rwlockattr_setpshared (const pthread_rwlockattr_t *attr, int value);  Successful return 0, error return positive value

Read-write lock implementation can refer to "UNP2" P142





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.