Linux thread Sync (3)-read/write lock

Source: Internet
Author: User

I. Overview

A read-write lock is similar to a mutex, which protects the shared resources of a critical section! The mutex only allows one thread to enter the critical section at a time, and the read-write lock has higher parallelism than it. Read/write locks have the following characteristics:

1. If a thread locks a critical section with a read lock , other threads can also use a read lock to enter the critical section so that multiple threads can operate in parallel . But this time, if the write lock locking will be blocked, after the write lock request block, after the subsequent read lock to request, the subsequent reading lock will be blocked ! This avoids the long-term use of read lock resources to prevent write-lock starvation !

2. If a thread locks the critical section with a write lock , the other threads will block either read or write locks!

Two. Function interface

1. Create a read-write lock

1.1: Macro Constant initialization

1 pthread_rwlock_t rwlock = Pthread_rwlock_initializer;

1.2: Function Initialization

1 #include <pthread.h>23intconst pthread_rwlockattr_t *restrict attr);

Rwlock: pthread_rwlock_t structure pointer for read-write lock

attr: A property structure pointer to a read-write lock. No other properties are required to default to NULL.

2. Read-write lock locking and unlock

1 #include <pthread.h>23int pthread_rwlock_rdlock (pthread_rwlock_t *  Rwlock); 4 int pthread_rwlock_wrlock (pthread_rwlock_t *rwlock); 5 int pthread_rwlock_unlock (pthread_rwlock_t *rwlock);

Rwlock: Created read-write lock pointer

3. Other types of lock

1#include <pthread.h>2#include <time.h>3 4 5 intPthread_rwlock_tryrdlock (pthread_rwlock_t *rwlock);6 intPthread_rwlock_trywrlock (pthread_rwlock_t *rwlock);7 8 intPthread_rwlock_timedrdlock (pthread_rwlock_t *restrict Rwlock,Const structTimespec *restrict abs_timeout);9 intPthread_rwlock_timedwrlock (pthread_rwlock_t *restrict Rwlock,Const structTimespec *restrict abs_timeout);

Try class function Lock: If the lock is not acquired, an error Ebusywill be returned immediately!

Timed class function Lock: If the lock is not acquired within the specified time, a etimedout error will be returned!

4. Destroying the read-write lock

1 #include <pthread.h>23int Pthread_rwlock_destroy (pthread_rwlock_t *rwlock);

Three. A simple example

Create 4 threads, 2 thread read locks , 2 thread write locks , and observe the order of 4 threads entering the critical section:

1 /**2 * @file pthread_rwlock.c3  */4 5#include <stdio.h>6#include <stdlib.h>7#include <string.h>8#include <unistd.h>9#include <pthread.h>Ten  One /*initializing a read-write lock*/ Apthread_rwlock_t Rwlock =Pthread_rwlock_initializer; - /*Global Resources*/ - intGlobal_num =Ten; the  - voidErr_exit (Const Char*err_msg) - { -printf"error:%s\n", err_msg); +Exit1); - } +  A /*read lock thread function*/ at void*thread_read_lock (void*Arg) - { -     Char*pthr_name = (Char*) arg; -  -      while(1) -     { in         /*Read Locking*/ -Pthread_rwlock_rdlock (&rwlock); to  +printf"thread%s enters critical section, Global_num =%d\n", Pthr_name, global_num); -Sleep1); theprintf"thread%s left the critical section ... \ n", pthr_name); *  $         /*Read Unlock*/Panax NotoginsengPthread_rwlock_unlock (&rwlock); -  theSleep1); +     } A  the     returnNULL; + } -  $ /*Write lock thread function*/ $ void*thread_write_lock (void*Arg) - { -     Char*pthr_name = (Char*) arg; the  -      while(1)Wuyi     { the         /*Write and lock*/ -Pthread_rwlock_wrlock (&rwlock); Wu  -         /*Write Operations*/ Aboutglobal_num++; $printf"thread%s enters critical section, Global_num =%d\n", Pthr_name, global_num); -Sleep1); -printf"thread%s left the critical section ... \ n", pthr_name); -  A         /*Write Unlock*/ +Pthread_rwlock_unlock (&rwlock); the  -Sleep2); $     } the  the     returnNULL; the } the  - intMainvoid) in { the pthread_t tid_read_1, Tid_read_2, Tid_write_1, tid_write_2; the  About     /*Create 4 threads, 2 reads, 2 writes*/ the     if(Pthread_create (&tid_read_1, NULL, Thread_read_lock,"read_1") !=0) theErr_exit ("Create Tid_read_1"); the  +     if(Pthread_create (&tid_read_2, NULL, Thread_read_lock,"read_2") !=0) -Err_exit ("Create Tid_read_2"); the Bayi     if(Pthread_create (&tid_write_1, NULL, Thread_write_lock,"Write_1") !=0) theErr_exit ("Create Tid_write_1"); the  -     if(Pthread_create (&tid_write_2, NULL, Thread_write_lock,"write_2") !=0) -Err_exit ("Create tid_write_2"); the  the     /*Just wait for a thread to prevent main from ending*/ the     if(Pthread_join (tid_read_1, NULL)! =0) theErr_exit ("Pthread_join ()"); -  the     return 0; the}

The critical section of the 2-thread function is all sleep (1), and the test gives enough time to see if other threads can come in . 64 lines, write lock function inside, sleep (2), because the write lock request will block the subsequent read lock, 2 write lock together request will let read lock hunger , so than 39 rows of sleep (1) more than a second!

Compile run:

As can be seen, read locks can enter the critical section together , and the write lock in the critical section of the 1 seconds will not have other threads to come in!!!

Linux thread Synchronization (3)-read-write lock

Related Article

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.