Computer Operating System Fundamentals (13)-Read and Write Lock for Thread Synchronization

introduction

This article is the thirteenth article. Read-write locks for thread synchronization. Read- write locks are also one of the methods to solve thread synchronization. In the previous two articles, China has introduced two methods of mutual exclusion and spin lock. The principle of the read-write lock is similar to the previous two locks, but the read-write lock has made some improvements

Read-write lock

The improvement of read-write locks is considered from the following points, the most important of which is the consideration of critical resources. In a complex development environment, there is likely to be more reading and less writing of critical resources . For example, a table in a database mainly stores some historical data. For these historical data, queries are generally performed and seldom modified. Then the table that stores historical data is a critical resource with more reading and less writing. For reading, the value of the critical resource will not be changed . If we lock it every time we read or write, the efficiency is very low. So at this time should consider whether there is a more efficient method? At this time, a read-write lock is generated

Introduction to read-write locks

  • Read-write lock is a special spin lock
  • Allow multiple readers to access resources at the same time to improve reading performance
  • It is mutually exclusive for write operations (multiple write operations are not allowed to access the same resource at the same time)

About the model of read-write lock

For a read-write lock, it allows multiple readers to read critical resources at the same time . Therefore, the reader threads 1, 2, and 3 in the figure below can read critical resources at the same time, but while reading, it will not allow write operations to go. Access critical resources. Because the critical resource is not changed when reading, and the write operation may change the value of the critical resource, so in the read-write lock, reading and writing are mutually exclusive, and reading and reading are not mutually exclusive

Read-write lock example

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

//临界资源
int num=0;

//定义读写锁
pthread_rwlock_t relock=PTHREAD_RWLOCK_INITIALIZER;

void *reader(void*){
    int times=10000000;
    while(times--){
        //在读操作之前加读锁
        pthread_rwlock_rdlock(&rdlock);
        if(times%1000==0){
            usleep(10);
        }
        //释放读锁
        pthread_rwlock_unlock(&rdlock);
    }
}

void *writer(void*){
    int times=10000000;
    while(times--){
        //加写锁
        pthread_rwlock_wrlock(&rdlock);
        num+=1;
        pthread_rwlock_unlock(&rdlock);
    }
}

int main()
{
    printf("Start in main function.");
        //定义三个线程
        pthread_t thread1,thread2, thread3;
        //两个执行读操作,一个执行写操作
        pthread_create(&thread1, NULL, &reader, NULL);
        pthread_create(&thread2, NULL, &reader, NULL);
        pthread_create(&thread3, NULL, &writer, NULL);
        pthread_join(&thread1, NULL);
        pthread_join(&thread2, NULL);
        pthread_join(&thread3, NULL);
        //打印临界资源的值
        printf("Print in main function: num = %d\n", num);
        return 0;
}

As mentioned above, the read-write lock will have a significant performance improvement in the case of more reading and less writing . At this time, you can verify it. Run the program of the read-write lock used above and check the running time as follows:

Now replace the read-write lock with a mutex, and then look at the execution time

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

//临界资源
int num=0;

//初始化互斥量
pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;

//定义读写锁
//pthread_rwlock_t relock=PTHREAD_RWLOCK_INITIALIZER;

void *reader(void*){
    int times=10000000;
    while(times--){
        //在读操作之前加读锁
        //pthread_rwlock_rdlock(&rdlock);
        pthread_mutex_lock(&mutex);
        if(times%1000==0){
            usleep(10);
        }
        //释放读锁
        //pthread_rwlock_unlock(&rdlock);
        pthread_mutex_unlock(&mutex);
    }
}

void *writer(void*){
    int times=10000000;
    while(times--){
        //加写锁
        //pthread_rwlock_wrlock(&rdlock);
        pthread_mutex_lock(&mutex);
        num+=1;
        //pthread_rwlock_unlock(&rdlock);
        pthread_mutex_unlock(&mutex);
    }
}

int main()
{
    printf("Start in main function.");
        //定义三个线程
        pthread_t thread1,thread2, thread3;
        //两个执行读操作,一个执行写操作
        pthread_create(&thread1, NULL, &reader, NULL);
        pthread_create(&thread2, NULL, &reader, NULL);
        pthread_create(&thread3, NULL, &writer, NULL);
        pthread_join(&thread1, NULL);
        pthread_join(&thread2, NULL);
        pthread_join(&thread3, NULL);
        //打印临界资源的值
        printf("Print in main function: num = %d\n", num);
        return 0;
}

Results of the:

It can be seen that for critical resources with more reads and less writes, the efficiency of using read-write locks is about 5 times that of using mutex

API for reading and writing lock in PHP: https://www.php.net/manual/zh/class.syncreaderwriter.php

It is the core competitiveness of a technical person to find the constant in the rapidly changing technology. Unity of knowledge and action, combining theory with practice

Guess you like

Origin blog.csdn.net/self_realian/article/details/107155293