UNIX Environment Advanced Programming: Thread synchronization of Read and write locks and attributes

Source: Internet
Author: User
Tags error code posix thread

Read-Write Locks and mutexes (mutexes) are similar and are another thread synchronization mechanism, but are not POSIX-standard and can be used to synchronize individual threads in the same process. Of course, if a read-write lock is stored in a memory area shared by several processes, it can also be used to synchronize between processes, and the mutex is either locked or unlocked, and only one thread can lock it at a time. Read and write locks can have three states: lock state in read mode, lock state in write mode, no lock state.

Only one thread at a time can occupy a read-write lock in writing mode, but more than one thread can occupy reading and writing locks at the same time.

When a read-write lock is a write-lock state, all threads attempting to lock (read or write) The lock are blocked until the lock is unlocked.

When a read-write lock is in the lock state, all threads attempting to lock it in read mode can gain access, but if the thread wants to lock the lock in write mode, it must block until all threads release the read lock.

Read-write locks often block subsequent read-mode lock requests when read-write locks are in the lock state, and if another thread attempts to lock in write mode. (following code validation found in the Ubuntu 10.04 system, will not block subsequent read mode requests, resulting in the request write lock thread starvation state) This can avoid the long possession of read mode lock, while waiting for the write mode lock request has not been satisfied, the occurrence of starvation. The Ubuntu 10.04 system will be tested later. (Note the previous article on the record lock Fcntl, Ubuntu 10.04 System, the process has read lock, and then priority to deal with the subsequent read lock, and then deal with the write lock, resulting in the write lock appears to starve to death)

Read-write locks are also known as shared-exclusive (shared-exclusive) locks, which are locked in shared mode when read-write locks are locked in reading mode, and are locked in exclusive mode when locked in write mode. Read-write locks are ideal for reading data much more frequently than the frequency of writing data from applications. This allows multiple read threads to run concurrently at any time, bringing a higher degree of concurrency to the program.

What needs to be mentioned is that read-write locks are still not part of the POSIX standard so far.

1 initialization and destruction of read and write locks

#include <pthread.h>  
int pthread_rwlock_init (pthread_rwlock_t *rwlock,const pthread_rwlockattr_t *attr);  
      
int Pthread_rwlock_destroy (pthread_rwlock_t *rwlock);  
      
                                               Return value: 0 returned successfully, otherwise error code returned

As with mutexes, read-write locks must be initialized before they are used, and must be destroyed before releasing their underlying memory.

The above two functions are initialized and destroyed by read-write locks respectively. As with mutexes, conditional variables, if read-write locks are statically allocated, they can be initialized by constants, as follows:

<span style= "FONT-SIZE:14PX;" >pthread_rwlock_t Rwlock = pthread_rwlock_initializer;</span>

can also be initialized by Pthread_rwlock_init (). Dynamically allocated read-write locks can only be initialized in this way because they cannot be initialized directly by assignment. Pthread_rwlock_init () The second parameter is a property that reads and writes the lock, and if the default property is used, you can pass in a null pointer null.

Then you need to invoke Pthread_rwlock_destroy () to destroy the read-write lock resource before you need to use it and release (automatically or manually) the memory occupied by the lock.

2 use of read-write locks

/* Read mode lock
/int pthread_rwlock_rdlock (pthread_rwlock_t *rwlock);  
      
/* non-blocking read mode with lock
/int pthread_rwlock_tryrdlock (pthread_rwlock_t *rwlock);  /* Timed waiting Read mode lock
/int pthread_rwlock_timedrdlock (pthread_rwlock_t *rwlock,const struct timespec *abstime);  
      
      
/* Write mode with lock
/int pthread_rwlock_wrlock (pthread_rwlock_t *rwlock);  
      
* * non-blocking write mode with lock
/int Pthread_rwlock_trywrlock (pthread_rwlock_t *rwlock);  
      
/* Timed waiting Write mode lock
/int pthread_rwlock_timedwrlock (pthread_rwlock_t *rwlock,const struct timespec *abstime);  
      
/* unlock *
/int pthread_rwlock_unlock (pthread_rwlock_t *rwlock);  
      
                                                   Return value: 0 returned successfully, otherwise error code returned

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.