当前位置:网站首页>Reader writer model
Reader writer model
2022-07-05 05:24:00 【abs(ln(1+NaN))】
Similar to the producer consumer model , The reader writer model also has a critical resource , Two roles , Three relationships , The reader writer model is characterized by :
(1) Writer ( Threads ) Less , readers ( Threads ) many
(2) Readers will not take away critical resources
Blackboard newspaper is a typical reader writer model , There is only one person who draws blackboard newspaper ( That is, the writer ), But there are many people who read blackboard newspaper ( That is, readers ), Here is an example to introduce the reader writer model .
Catalog
One 、 Three relationships in the model
Two 、 Lock reading and writing / Basic principle of unlocking
3、 ... and 、 Priority questions
One 、 Three relationships in the model
The two roles refer to the reader role and the writer role , The three relationships refer to readers and readers 、 Writer and writer 、 Readers and writers .
1、 Writer and writer
Only one person can draw blackboard newspaper every time , If two people draw at the same time , A person paints the theme of fire fighting , Another person is painting Animal Themes , This is not what we want to see .
==》 So there is a mutually exclusive relationship between writers , You have to wait for one person to finish painting , To make the next painting
2、 Readers and writers
sometimes , You might think , Drawing blackboard newspaper and reading blackboard newspaper can be carried out at the same time , But in the process of painting , The reader may not be able to get What are you drawing , Obviously you are painting dragons , The reader said you were painting snakes . This leads to misunderstanding , So you should wait until the writer finishes drawing , Readers read again . This is an understanding angle
Another angle is , Readers are still reading , The writer wants to erase the blackboard newspaper , Obviously, this is contradictory , So we should let readers finish reading , The writer erases again .
Both angles show , Readers and writers are mutually exclusive
3、 Readers and readers
Reading by one person does not affect reading by another , There is no mutual exclusion here .==》 There is no relationship between readers
Two 、 Lock reading and writing / Basic principle of unlocking
Linux The corresponding lock is provided for readers and writers —— Reader lock and writer lock
We use the number of readers as a critical resource , namely int readers = 0;
1、 Readers lock / Unlock
The reader's declaration of the locking function is as follows :

Because there is no relationship between readers , So locking and unlocking are two independent processes
Lock = People who read blackboard newspapers add 1
Unlock = The number of people reading blackboard newspaper is reduced 1
// Lock
lock();
readers++;
unlock();
// Unlock
lock();
readers--;
unlock();2、 The writer locks / Unlock
The writer's locking function is declared as follows :

When there are readers , namely readers Not for 0 when , The writer cannot modify the content , After locking at this time , You must wait until the writer writes the content , To unlock
// Lock
lock();
while(readers > 0)
{
wait(); // Enter the condition variable and wait
}
modify(); // Used to indicate the operation of the writer to modify the content
// Unlock
unlock(); // Generally, after modifying the content, unlock 3、 ... and 、 Priority questions
In the previous description of the relationship between readers and writers , We analyzed their relationship from two perspectives . One is the writer first , I haven't finished , Don't even read ; The other is reader first , I haven't finished reading , Don't wipe it .
In actual use , More of us will choose readers first , After all, the minority is subordinate to the majority , The most basic feature of the reader writer model is that there are many readers , Few writers . That is, the writer is less important than the reader .
Readers first : When readers and writers arrive at the same time , Readers have priority in accessing
Writer first : When readers and writers arrive at the same time , Readers who come later than the current writer , Do not enter the critical area for access , etc.
There are no readers in the critical area , namely readers = 0 when , The writer writes first .
边栏推荐
- [turn]: Apache Felix framework configuration properties
- Find a good teaching video for Solon framework test (Solon, lightweight application development framework)
- 使用Room数据库报警告: Schema export directory is not provided to the annotation processor so we cannot expor
- Haut OJ 1245: large factorial of CDs --- high precision factorial
- Under the national teacher qualification certificate in the first half of 2022
- 动漫评分数据分析与可视化 与 IT行业招聘数据分析与可视化
- [转]:Apache Felix Framework配置属性
- lxml.etree.XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8
- 搭建完数据库和网站后.打开app测试时候显示服务器正在维护.
- Web APIs DOM节点
猜你喜欢

Gbase database helps the development of digital finance in the Bay Area

Count sort

GBase数据库助力湾区数字金融发展

Ue4/ue5 illusory engine, material chapter, texture, compression and memory compression and memory

The present is a gift from heaven -- a film review of the journey of the soul

十年不用一次的JVM调用
![[speed pointer] 142 circular linked list II](/img/f8/222a360c01d8ef120b61bdd2025044.jpg)
[speed pointer] 142 circular linked list II
![[interval problem] 435 Non overlapping interval](/img/a3/2911ee72635b93b6430c2efd05ec9a.jpg)
[interval problem] 435 Non overlapping interval
![[to be continued] [depth first search] 547 Number of provinces](/img/c4/b4ee3d936776dafc15ac275d2059cd.jpg)
[to be continued] [depth first search] 547 Number of provinces

JVM call not used once in ten years
随机推荐
[轉]: OSGI規範 深入淺出
[转]MySQL操作实战(三):表联结
SDEI初探-透过事务看本质
Under the national teacher qualification certificate in the first half of 2022
Merge sort
What is the agile proportion of PMP Exam? Dispel doubts
[interval problem] 435 Non overlapping interval
FVP和Juno平台的Memory Layout介绍
质量体系建设之路的分分合合
On-off and on-off of quality system construction
Reverse one-way linked list of interview questions
Solon Logging 插件的添加器级别控制和日志器的级别控制
Double pointer Foundation
2022上半年全国教师资格证下
Optimization scheme of win10 virtual machine cluster
小程序直播+電商,想做新零售電商就用它吧!
搭建完数据库和网站后.打开app测试时候显示服务器正在维护.
Support multi-mode polymorphic gbase 8C database continuous innovation and heavy upgrade
嵌入式数据库开发编程(五)——DQL
Development error notes