Pthread_rwlock_t description of the read/write lock function

Source: Internet
Author: User

Read/write lock

Index:

  1. Initialize a read/write lock pthread_rwlock_init
  2. Read lock pthread_rwlock_rdlock
  3. Non-blocking read lock pthread_rwlock_tryrdlock
  4. Write lock read/write lock pthread_rwlock_wrlock
  5. Non-blocking write lock pthread_rwlock_trywrlock
  6. Unlock the read/write lock pthread_rwlock_unlock
  7. Release the read/write lock pthread_rwlock_destroy

 

Read/write locks are used to solve the reader writer's problem. Read operations can be shared, write operations are exclusive, and read operations can have multiple reads, and only write locks can be written, at the same time, reading is not allowed during writing.

Strong reader synchronization and strong writer synchronization:

Strong reader synchronization: When the writer does not perform write operations, the reader can access them;

Strong writer synchronization: only after all the writers have finished writing can they perform read operations. The reader needs the latest information, and some fact-sensitive systems may use this institution, such as ticketing.

 

1. initialize a read/write lock pthread_rwlock_init

# Include <pthread. h>

Int pthread_rwlock_init (pthread_rwlock_t * rwlock, \ const pthread_rwlockattr_t * ATTR );

Returned value: 0 is returned if the function is successful. Any other returned values indicate an error.

Initializes the read/write lock specified by rwlock. Its attribute is specified by the ATTR parameter. If the ATTR value is null, the default read/write lock attribute is used when the read/write locks are initialized. The effect is the same as that of ATTR pointing to a default read/write lock attribute object.

After successful initialization, the read/write lock status is not locked.

Calling the pthread_rwlock_init function for an initialized read/write lock produces unpredictable consequences. Using an uninitialized read/write lock also produces unpredictable consequences.

If you need a read/write lock of the default attribute, you can use the macro pthread_rwlock_initializer to initialize the static read/write lock variable. However, the static initialization read/write lock does not perform an error check. For example:

Pthread_rwlock_t rwlock = pthreasd_rwlock_initializer;

 

Read Lock operation for obtaining read/write locks: block-based or non-blocking-based access. If a read/write lock is held by a writer, the read thread blocks until the writer releases the read/write lock.

2. Read lock read/write lock pthread_rwlock_rdlock

# Include <pthread. h>

Int pthread_rwlock_rdlock (pthread_rwlock_t * rwlock );

Returned value: 0 is returned if the function is successful. Any other returned values indicate an error.

The function performs read lock on the rwlock read/write lock.

If a thread writes a read/write lock, the thread that calls the pthread_rwlock_rdlock function cannot read and lock the read/write lock until the thread can read and lock the read/write lock.

If a thread locks the read/write lock and then calls the pthread_rwlock_rdlock function to lock the same read/write lock, the result cannot be predicted.

The standard does not specify whether the current thread can read and lock the read/write lock when there is no thread writing to lock the read/write lock, but a thread attempts to write to lock the lock and is blocked in this read/write lock. In most thread libraries, priority is often given to activating the thread that is blocked by attempting to lock the write. This is done to prevent the blocked thread from being scheduled for a long time.

A thread may lock a read/write lock Multiple times (that is, the pthread_rwlock_rdlock () function is successfully called multiple times ). In this case, the thread must call the pthread_rwlock_unlock () function to unlock the read/write locks for the same number of times.

When a read/write lock is locked and a thread is blocked in the read/write lock, if a signal is generated, when the thread returns from the signal processing program, the read/write locks will continue to be blocked. As if the thread has not been interrupted.

 

3. Non-blocking read lock pthread_rwlock_tryrdlock

# Include <pthread. h>

Int pthread_rwlock_tryrdlock (pthread_rwlock_t * rwlock );

Returned value: 0 is returned if the function is successful. Any other returned values indicate an error.

The pthread_rwlock_tryrdlock () function is similar to the pthread_rwlock_rdlock function. The difference is that when an existing thread writes a read/write lock or a thread attempts to write a lock is blocked, the pthread_rwlock_tryrdlock function fails to return.

 

The write Lock operation for obtaining read/write locks is divided into blocking and non-blocking. If the corresponding read/write lock is held by another writer or the read/write lock is held by the reader, the thread will block and wait.

4. Write lock read/write lock pthread_rwlock_wrlock

# Include <pthread. h>

Int pthread_rwlock_wrlock (pthread_rwlock_t * rwlock );

Returned value: 0 is returned if the function is successful. Any other returned values indicate an error.

Write lock rwlock. If there is no thread read or write lock rwlock, the current thread will write lock rwlock. Otherwise, the thread will be blocked until no thread locks the read/write lock.

If the read/write lock has been locked by this thread before this function is called, the running result of the pthread_rwlock_wrlock function is unpredictable.

After a read/write lock is unlocked, when a thread blocking the read/write lock is activated, it is often preferred to attempt to write the lock and the thread is blocked, this is done to prevent the blocked thread from being scheduled for a long time while trying to lock the write.

When a read/write lock is locked and a thread is blocked in the read/write lock, if a signal is generated, when the thread returns from the signal processing program, the read/write locks will continue to be blocked. It seems that the thread has not been interrupted.

 

5. Non-blocking write lock pthread_rwlock_trywrlock

# Include <pthread. h>

Int pthread_rwlock_trywrlock (pthread_rwlock_t * rwlock );

Returned value: 0 is returned if the function is successful. Any other returned values indicate an error.

The function of pthread_rwlock_trywrlock is similar to that of pthread_rwlock_wrlock. The difference is that if other threads lock the read/write lock, the pthread_rwlock_trywrlock function will fail to return.

 

6. Unlock the read/write lock pthread_rwlock_unlock

# Include <pthread. h>

Int pthread_rwlock_unlock (pthread_rwlock_t * rwlock );

Returned value: 0 is returned if the function is successful. Any other returned values indicate an error.

Unlock a read/write lock.

Before calling the pthread_rwlock_unlock function, you must call the pthread_rwlock_rdlock function or the pthread_rwlock_wrlock function to lock the read/write lock. Otherwise, the results are unpredictable.

After the pthread_rwlock_unlock function is used to unlock the read lock on the read/write lock, if the thread has other read locks on the read/write lock, the read/write lock will continue to lock the thread. If the pthread_rwlock_unlock function unlocks the last read lock of the current thread in this read/write lock, the current thread no longer has a read lock on this read/write lock. If the pthread_rwlock_unlock function unlocks the last lock of the read/write lock, the read/write lock is not locked.

If the pthread_rwlock_unlock function is used to unlock the write lock of the read/write lock, the read/write lock will be unlocked after the function returns.

If you use the pthread_rwlock_unlock function to unlock a read/write lock, multiple threads are waiting to lock the read/write lock. The system uses the scheduling policy to determine which thread to activate to lock the read/write lock.

If the pthread_rwlock_unlock function is used to unlock a read/write lock, multiple threads are waiting to lock the read/write lock, the system uses the scheduling policy to determine the sequence in which each thread is activated to lock the read/write locks.

If the pthread_rwlock_unlock function is used to unlock a read/write lock, multiple threads are waiting to lock and read the read/write lock, generally, the Read and Write locks are read and written by the threads that need to be written to be locked first (the standard does not specify whether to activate the write thread first or activate the read thread first in this case ).

 

7. Release the read/write lock pthread_rwlock_destroy

# Include <pthread. h>

Int pthread_rwlock_destroy (pthread_rwlock_t * rwlock );

Returned value: 0 is returned if the function is successful. Any other returned values indicate an error.

Release the read/write lock rwlock and release the resources occupied by the read/write lock. Generally, the pthread_rwlock_destroy function sets the mutex lock object pointed to by rwlock to an invalid value.

After a read/write lock is released, before pthread_rwlock_init reinitializes the lock, it may be unpredictable.

If a thread locks a read/write lock, releasing the read/write lock using the pthread_rwlock_destroy () function will cause unpredictable results.

An uninitialized read/write lock may cause unpredictable results.

 

 

Summary (transfer ):

The difference between mutex lock and read/write lock:

When you access resources in the critical section (the meaning of access includes all operations: read and write), a mutex lock is required;

When reading data (Resources in the critical zone in the mutex lock), you need to read the lock. When writing data, you need to write the lock.

Advantages of the read/write lock:

For applications that frequently read data than modify data, using read/write locks instead of mutex locks can improve efficiency.. Because when mutex lock is used, even reading data (equivalent to operating critical zone resources) requires mutex lock, and using a read/write lock, multiple readers can be allowed at any time, this improves the concurrency and protects the data when a writer modifies the data to avoid interference from other readers or writers.

Read/write lock description:

A read/write lock is used to obtain a read/write lock. An exclusive lock is used to obtain a read/write lock.Therefore, shared access to a given resource is also called share-exclusive lock.

Other arguments about this type of problem (multiple readers and one writer) include the problem of the reader and the writer, and the lock of the Multi-reader-single writer.

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.