读写锁三种关系的证明(读者和读者互补影响、写者和写者互斥、读者和写者互斥)
创始人
2024-03-15 07:05:19
0

        目录

1、读者和读者互不影响

2、写者和写者互斥

3、读者和写者互斥

(1) 读者持有锁

(2) 写者持有锁


1、读者和读者互不影响

假设现在只有读者线程,我们让一个读者线程申请锁以后,但是不释放读写锁。

#include 
#include 
#include pthread_rwlock_t rwlock;
void* read_thread(void* args){pthread_detach(pthread_self());int num = *(int*)args;while (1){pthread_rwlock_rdlock(&rwlock);        // 申请读者锁printf("读者[%d]正在读内容\n", num);sleep(1);//pthread_rwlock_unlock(&rwlock);      }
}int main(){pthread_t tid1,tid2,tid3,tid4;pthread_rwlock_init(&rwlock, NULL);int i = 1, j = 2;pthread_create(&tid1,NULL,read_thread, (void*)&i);pthread_create(&tid2,NULL,read_thread,(void*)&j);while(1){    sleep(1);} return 0;
}

我们发现,读者和读者之间不会去争抢锁,即便是某一个读者线程申请到锁,也不会影响其他读者线程来申请锁。

 

2、写者和写者互斥

我们采用和上面类似的做法,只有两个写者线程,其中一个写者线程获取到锁以后,不释放锁,看一下另一个写者线程能否得到锁。

#include 
#include 
#include pthread_rwlock_t rwlock;
void* write_thread(void* args){pthread_detach(pthread_self());int num = *(int*)args;while (1){pthread_rwlock_wrlock(&rwlock);printf("写者[%d]正在写内容\n", num);//pthread_rwlock_unlock(&rwlock);sleep(1);}
}int main(){pthread_t tid1,tid2,tid3,tid4;pthread_rwlock_init(&rwlock, NULL);int i = 1, j = 2;pthread_create(&tid3,NULL,write_thread,(void*)&i);pthread_create(&tid4,NULL,write_thread,(void*)&j);while(1){    sleep(1);} return 0;
}

我们发现始终只有写者2在打印,说明写者2拿到锁以后,写者1就无法获取到锁了。说明写者和写者之间是互斥的。

 

3、读者和写者互斥

这里就分为两种场景:读者持有锁、写者持有锁。

  • 读者持有锁时,写者申请锁会阻塞等待,但是读者申请锁不受影响。
  • 写者持有锁时,无论是读锁申请还是写锁申请,都会被阻塞

(1) 读者持有锁

现在有三个线程,两个读线程,一个写线程,他们执行的操作如下:

  • 读者[1]:立马申请锁,但是不释放
  • 写者[1]:延迟1s以后申请(验证写者申请锁是否会被阻塞)
  • 读者[2]:延迟2s后申请(验证写者之后的读锁申请是否会被阻塞)
#include 
#include 
#include pthread_rwlock_t rwlock;
void* read_thread1(void* args){pthread_detach(pthread_self());printf("读者[1]申请锁\n");while (1){pthread_rwlock_rdlock(&rwlock);printf("读者[1]正在读内容\n");sleep(1);// pthread_rwlock_unlock(&rwlock);        // 读者1不释放锁}    
}
void* read_thread2(void* args){pthread_detach(pthread_self());//int num = *(int*)args;sleep(2);    // 让读者[2]延迟2s再申请printf("读者[2]申请锁\n");while (1){pthread_rwlock_rdlock(&rwlock);printf("读者[2]正在读内容\n");pthread_rwlock_unlock(&rwlock);sleep(1);}
}void* write_thread(void* args){pthread_detach(pthread_self());sleep(1);                // 让写者[1]延迟1s再申请printf("写者1申请锁\n");while (1){pthread_rwlock_wrlock(&rwlock);printf("写者[1]正在写内容\n");pthread_rwlock_unlock(&rwlock);sleep(1);}
}int main(){pthread_t tid1,tid2,tid3,tid4;pthread_rwlock_init(&rwlock, NULL);int i = 1, j = 2;pthread_create(&tid1,NULL,read_thread1, NULL);pthread_create(&tid2,NULL,read_thread2, NULL);pthread_create(&tid3,NULL,write_thread, NULL);while(1){    sleep(1);} return 0;
}

(2) 写者持有锁

我们采用和上面类似的步骤,

  • 写者[1]:先申请锁,但不释放
  • 其他读者:延迟1s后申请
#include 
#include 
#include pthread_rwlock_t rwlock;
void* read_thread1(void* args){pthread_detach(pthread_self());sleep(1);                // 让读者[1]延迟1s再申请printf("读者[1]申请锁\n");while (1){pthread_rwlock_rdlock(&rwlock);printf("读者[1]正在读内容\n");pthread_rwlock_unlock(&rwlock);sleep(1);}
}
void* read_thread2(void* args){pthread_detach(pthread_self());//int num = *(int*)args;sleep(1);                // 让读者[2]延迟1s再申请printf("读者[2]申请锁\n");while (1){pthread_rwlock_rdlock(&rwlock);printf("读者[2]正在读内容\n");pthread_rwlock_unlock(&rwlock);sleep(1);}
}void* write_thread(void* args){pthread_detach(pthread_self());printf("写者1申请锁\n");while (1){pthread_rwlock_wrlock(&rwlock);printf("写者[1]正在读内容\n");// pthread_rwlock_unlock(&rwlock);   // 写者1不释放锁sleep(1);}
}int main(){pthread_t tid1,tid2,tid3,tid4;pthread_rwlock_init(&rwlock, NULL);int i = 1, j = 2;pthread_create(&tid1,NULL,read_thread1, NULL);pthread_create(&tid2,NULL,read_thread2, NULL);pthread_create(&tid3,NULL,write_thread, NULL);while(1){    sleep(1);} return 0;
}

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...
有效的括号 一、题目 给定一个只包括 '(',')','{','}'...
【Ctfer训练计划】——(三... 作者名:Demo不是emo  主页面链接:主页传送门 创作初心ÿ...