Read/write Lock

Source: Internet
Author: User

Read and write locks are similar to mutexes, but read-write locks allow for higher parallelism. The mutex is either locked or unlocked, and only one thread can lock it at a time. Read-write lock can have three states: Read mode lock state, write mode lock state, no lock state. Only one thread at a time can occupy write-mode read-write locks, but the lock-up thread can occupy read-mode reading and writing locks at the same time.

When a read-write lock is a write-lock state, all threads attempting to lock the lock are blocked until the lock is unlocked. When a read-write lock reads the locking state, all threads attempting to lock him in read mode are granted access, but any thread that wants to lock the lock in write mode will be blocked until all threads release their read lock. Read-write locks usually block subsequent read-mode lock requests when a read-write lock is in a state that is locked in reading mode, while a thread attempts to acquire a lock in write mode. This avoids the long-term use of read-mode locks and waits for write-mode lock requests to remain unmet.

Read-write locks are well suited for reading data structures much more often than written. When the read-write lock is in write mode, the data structure it protects can be safely modified. Because only one thread at a time can have this lock in write mode. When the read-write lock is in reading mode, the data structure protected by the lock can be read by multiple threads that receive a read-mode lock as long as the thread acquires read-write locks in reading mode first.

Read-write locks are also called shared mutexes, which can be said to be locked in shared mode when read-write locks are locked. When it is locked in write mode, it can be said to be locked in mutually exclusive mode.

Read-write locks must be initialized before they are used, and must be destroyed after use, with the following functions:

int Pthread_rwlock_init (pthread_rwlock_t *restrict rwlock,const pthread_rwlockattr_t *restrict attr)

int Pthread_rwlock_destroy (pthread_rwlock_t *rwlock)

To lock the read-write lock in reading mode, call Pthread_rwlock_rdlock to lock the read-write lock in write mode, call Pthred_rwlock_wrlock, and call Pthread_rwlock_unlock to unlock it. The functions are as follows:


int Pthread_rwlock_rdlock (pthread_rwlock_t *rwlock)

int Pthread_rwlock_wrlock (pthread_rwlock_t *rwlock)

int Pthread_rwlock_unlock (pthread_rwlock_t *rwlock)


Now let's write a small program to practice practiced hand:

In the following program, we define a global variable that reads constantly to accumulate it, and the reader keeps reading it.

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

pthread_rwlock_t lock;
int g_val=0;

void *reader (void *arg)
{
while (1)
{
Pthread_rwlock_rdlock (&lock);
printf ("%d\n", g_val);
Pthread_rwlock_unlock (&lock);
}
}

void *writer (void *arg)
{
while (1)
{
Pthread_rwlock_wrlock (&lock);
g_val++;
Pthread_rwlock_unlock (&lock);
Sleep (1);
}
}

int main ()
{
Pthread_rwlock_init (&lock,null);

pthread_t Tid1,tid2;
Pthread_create (&tid1,null,reader,null);
Pthread_create (&tid2,null,writer,null);

      Pthread_join (tid1,null);
      Pthread_join (tid2,null);
  
     Pthread_rwlock_destroy (&lock);                                                                                                                              
      return 0;
 }


The results of the operation are as follows: (Results not shown)

650) this.width=650; "title=" @]]3x6lif8z ' @y$b_llejbw.png "src=" http://s5.51cto.com/wyfs02/M00/7F/61/ Wkiol1ccxqzthamwaaasqqkqiks069.png "alt=" Wkiol1ccxqzthamwaaasqqkqiks069.png "/>

As a result, from 1 to a gradual increase in printing, I only intercepted part of it.



























Read/write Lock

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.