15. Operating system - read-write lock

Table of contents

1. Disadvantages of mutual exclusion locks

2. Advantages of read-write lock

3. Operation steps of read-write lock

4、API

(1) Initialize/destroy read-write locks (pthread_rwlock_destroy, pthread_rwlock_init)

 (2) Blocking add read/write lock/non-blocking add read/write lock/unlock

 5. Code

 6. Precautions

1. Disadvantages of mutual exclusion locks

When most of the program resources are read operations, the mutex will cause multiple threads to block and wait while acquiring the resource, resulting in reduced program efficiency.

2. Advantages of read-write lock

(1) Read lock: Multiple threads are allowed to read resources at the same time, and multiple read locks can be added at the same time

(2) Write lock: Only one thread is allowed to read resources at the same time, and no other thread is allowed to hold lock resources

3. Operation steps of read-write lock

(1) Initialize the read-write lock pthread_rwlock_init()

(2) Add read lock / write lock pthread_rwlock_rdlock / pthread_rwlock_wrlock

(3) Unlock pthread_rwlock_unlock() when shared resources are no longer used

(4) pthread_rwlock_destroy() can be destroyed when the read-write lock is not needed

4、API

(1) Initialize/destroy read-write locks (pthread_rwlock_destroy, pthread_rwlock_init)

 (2) Blocking add read/write lock/non-blocking add read/write lock/unlock

 5. Code

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

char * msg = NULL ;
pthread_rwlock_t rwlock ;

void * FUNC(void * arg )
{
    
    int num = (int ) arg ;
    // printf("num:%d\n" , num); 
    while(1)
    {
        pthread_rwlock_rdlock(&rwlock);
        printf("我是%d号线程,我看到的消息是:%s\n" , (int)arg , msg) ;
        pthread_rwlock_unlock(&rwlock);
        sleep(1);//刚好所有的读锁都睡觉了,就上写锁
    }
}

int main(int argc, char const *argv[])
{
    msg = calloc(128,1);
    
    // 初始化锁资源
    pthread_rwlock_init(&rwlock, NULL );
    
    // 创建多个线程
    pthread_t thread1 ;
    pthread_t thread2 ;
    pthread_t thread3 ;
    pthread_t thread4 ;
    pthread_t thread5 ;
    pthread_t thread6 ;
    
    pthread_create(&thread1, NULL, FUNC , (void *)1 ); 
    pthread_create(&thread2, NULL, FUNC , (void *)2 ); 
    pthread_create(&thread3, NULL, FUNC , (void *)3 ); 
    pthread_create(&thread4, NULL, FUNC , (void *)4 ); 
    pthread_create(&thread5, NULL, FUNC , (void *)5 ); 
    pthread_create(&thread6, NULL, FUNC , (void *)6 ); 
    

    while (1)
    {
        // 申请上写锁 --> 写锁在同一时间内只允许上一个
        printf("等待上写锁.....\n");
        pthread_rwlock_wrlock(&rwlock);
        printf("成功获得写锁.....\n");
        fgets(msg , 128 , stdin) ;
        pthread_rwlock_unlock(&rwlock);
        printf("解除写锁状态.....\n");
        sleep(5);
    }
    

    
    return 0;
}

6. Precautions

 Regardless of whether it is a read lock or a write lock, it is necessary to judge the status of the current shared resource when locking;

Read lock: When a thread reads information, first check whether the write lock exists, if it exists, wait, otherwise add a read lock

Write lock: When a thread writes information, check whether someone reads it, if so, wait, otherwise add a write lock

Guess you like

Origin blog.csdn.net/weixin_45981798/article/details/129884074