<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
a	O
readers	O
–	O
writer	O
(	O
single-writer	O
lock	B-Operating_System
,	O
a	O
multi-reader	B-Operating_System
lock	I-Operating_System
,	O
a	O
push	O
lock	B-Operating_System
,	O
or	O
an	O
MRSW	O
lock	B-Operating_System
)	O
is	O
a	O
synchronization	O
primitive	O
that	O
solves	O
one	O
of	O
the	O
readers	B-Operating_System
–	I-Operating_System
writers	I-Operating_System
problems	I-Operating_System
.	O
</s>
<s>
An	O
RW	B-Operating_System
lock	I-Operating_System
allows	O
concurrent	B-Operating_System
access	I-Operating_System
for	O
read-only	O
operations	O
,	O
whereas	O
write	O
operations	O
require	O
exclusive	O
access	O
.	O
</s>
<s>
This	O
means	O
that	O
multiple	O
threads	O
can	O
read	O
the	O
data	O
in	O
parallel	O
but	O
an	O
exclusive	O
lock	B-Operating_System
is	O
needed	O
for	O
writing	O
or	O
modifying	O
data	O
.	O
</s>
<s>
A	O
common	O
use	O
might	O
be	O
to	O
control	O
access	O
to	O
a	O
data	O
structure	O
in	O
memory	O
that	O
cannot	O
be	O
updated	O
atomically	B-General_Concept
and	O
is	O
invalid	O
(	O
and	O
should	O
not	O
be	O
read	O
by	O
another	O
thread	O
)	O
until	O
the	O
update	O
is	O
complete	O
.	O
</s>
<s>
Readers	B-Operating_System
–	I-Operating_System
writer	I-Operating_System
locks	I-Operating_System
are	O
usually	O
constructed	O
on	O
top	O
of	O
mutexes	B-Operating_System
and	O
condition	O
variables	O
,	O
or	O
on	O
top	O
of	O
semaphores	B-Operating_System
.	O
</s>
<s>
Some	O
RW	B-Operating_System
locks	I-Operating_System
allow	O
the	O
lock	B-Operating_System
to	O
be	O
atomically	B-General_Concept
upgraded	O
from	O
being	O
locked	O
in	O
read-mode	O
to	O
write-mode	O
,	O
as	O
well	O
as	O
being	O
downgraded	O
from	O
write-mode	O
to	O
read-mode	O
.	O
</s>
<s>
Upgradable	O
RW	B-Operating_System
locks	I-Operating_System
can	O
be	O
tricky	O
to	O
use	O
safely	O
,	O
since	O
whenever	O
two	O
threads	O
holding	O
reader	O
locks	O
both	O
attempt	O
to	O
upgrade	O
to	O
writer	O
locks	O
,	O
a	O
deadlock	O
is	O
created	O
that	O
can	O
only	O
be	O
broken	O
by	O
one	O
of	O
the	O
threads	O
releasing	O
its	O
reader	O
lock	B-Operating_System
.	O
</s>
<s>
The	O
deadlock	O
can	O
be	O
avoided	O
by	O
allowing	O
only	O
one	O
thread	O
to	O
acquire	O
the	O
lock	B-Operating_System
in	O
"	O
read-mode	O
with	O
intent	O
to	O
upgrade	O
to	O
write	O
"	O
while	O
there	O
are	O
no	O
threads	O
in	O
write	O
mode	O
and	O
possibly	O
non-zero	O
threads	O
in	O
read-mode	O
.	O
</s>
<s>
RW	B-Operating_System
locks	I-Operating_System
can	O
be	O
designed	O
with	O
different	O
priority	O
policies	O
for	O
reader	O
vs.	O
writer	O
access	O
.	O
</s>
<s>
The	O
lock	B-Operating_System
can	O
either	O
be	O
designed	O
to	O
always	O
give	O
priority	O
to	O
readers	O
(	O
read-preferring	O
)	O
,	O
to	O
always	O
give	O
priority	O
to	O
writers	O
(	O
write-preferring	O
)	O
or	O
be	O
unspecified	O
with	O
regards	O
to	O
priority	O
.	O
</s>
<s>
These	O
policies	O
lead	O
to	O
different	O
tradeoffs	O
with	O
regards	O
to	O
concurrency	B-Operating_System
and	O
starvation	B-Operating_System
.	O
</s>
<s>
Read-preferring	O
RW	B-Operating_System
locks	I-Operating_System
allow	O
for	O
maximum	O
concurrency	B-Operating_System
,	O
but	O
can	O
lead	O
to	O
write-starvation	O
if	O
contention	O
is	O
high	O
.	O
</s>
<s>
This	O
is	O
because	O
writer	O
threads	O
will	O
not	O
be	O
able	O
to	O
acquire	O
the	O
lock	B-Operating_System
as	O
long	O
as	O
at	O
least	O
one	O
reading	O
thread	O
holds	O
it	O
.	O
</s>
<s>
Since	O
multiple	O
reader	O
threads	O
may	O
hold	O
the	O
lock	B-Operating_System
at	O
once	O
,	O
this	O
means	O
that	O
a	O
writer	O
thread	O
may	O
continue	O
waiting	O
for	O
the	O
lock	B-Operating_System
while	O
new	O
reader	O
threads	O
are	O
able	O
to	O
acquire	O
the	O
lock	B-Operating_System
,	O
even	O
to	O
the	O
point	O
where	O
the	O
writer	O
may	O
still	O
be	O
waiting	O
after	O
all	O
of	O
the	O
readers	O
which	O
were	O
holding	O
the	O
lock	B-Operating_System
when	O
it	O
first	O
attempted	O
to	O
acquire	O
it	O
have	O
released	O
the	O
lock	B-Operating_System
.	O
</s>
<s>
Priority	O
to	O
readers	O
may	O
be	O
weak	O
,	O
as	O
just	O
described	O
,	O
or	O
strong	O
,	O
meaning	O
that	O
whenever	O
a	O
writer	O
releases	O
the	O
lock	B-Operating_System
,	O
any	O
blocking	O
readers	O
always	O
acquire	O
it	O
next	O
.	O
</s>
<s>
Write-preferring	O
RW	B-Operating_System
locks	I-Operating_System
avoid	O
the	O
problem	O
of	O
writer	O
starvation	B-Operating_System
by	O
preventing	O
any	O
new	O
readers	O
from	O
acquiring	O
the	O
lock	B-Operating_System
if	O
there	O
is	O
a	O
writer	O
queued	O
and	O
waiting	O
for	O
the	O
lock	B-Operating_System
;	O
the	O
writer	O
will	O
acquire	O
the	O
lock	B-Operating_System
as	O
soon	O
as	O
all	O
readers	O
which	O
were	O
already	O
holding	O
the	O
lock	B-Operating_System
have	O
completed	O
.	O
</s>
<s>
The	O
downside	O
is	O
that	O
write-preferring	O
locks	O
allows	O
for	O
less	O
concurrency	B-Operating_System
in	O
the	O
presence	O
of	O
writer	O
threads	O
,	O
compared	O
to	O
read-preferring	O
RW	B-Operating_System
locks	I-Operating_System
.	O
</s>
<s>
Also	O
the	O
lock	B-Operating_System
is	O
less	O
performant	O
because	O
each	O
operation	O
,	O
taking	O
or	O
releasing	O
the	O
lock	B-Operating_System
for	O
either	O
read	O
or	O
write	O
,	O
is	O
more	O
complex	O
,	O
internally	O
requiring	O
taking	O
and	O
releasing	O
two	O
mutexes	B-Operating_System
instead	O
of	O
one	O
.	O
</s>
<s>
This	O
variation	O
is	O
sometimes	O
also	O
known	O
as	O
"	O
write-biased	O
"	O
readers	B-Operating_System
–	I-Operating_System
writer	I-Operating_System
lock	I-Operating_System
.	O
</s>
<s>
Unspecified	O
priority	O
RW	B-Operating_System
locks	I-Operating_System
does	O
not	O
provide	O
any	O
guarantees	O
with	O
regards	O
read	O
vs.	O
write	O
access	O
.	O
</s>
<s>
Several	O
implementation	O
strategies	O
for	O
readers	B-Operating_System
–	I-Operating_System
writer	I-Operating_System
locks	I-Operating_System
exist	O
,	O
reducing	O
them	O
to	O
synchronization	O
primitives	O
that	O
are	O
assumed	O
to	O
pre-exist	O
.	O
</s>
<s>
Raynal	O
demonstrates	O
how	O
to	O
implement	O
an	O
R/W	O
lock	B-Operating_System
using	O
two	O
mutexes	B-Operating_System
and	O
a	O
single	O
integer	O
counter	O
.	O
</s>
<s>
One	O
mutex	B-Operating_System
,	O
,	O
protects	O
and	O
is	O
only	O
used	O
by	O
readers	O
;	O
the	O
other	O
,	O
(	O
for	O
"	O
global	O
"	O
)	O
ensures	O
mutual	B-Operating_System
exclusion	I-Operating_System
of	O
writers	O
.	O
</s>
<s>
This	O
requires	O
that	O
a	O
mutex	B-Operating_System
acquired	O
by	O
one	O
thread	O
can	O
be	O
released	O
by	O
another	O
.	O
</s>
<s>
The	O
following	O
is	O
pseudocode	B-Language
for	O
the	O
operations	O
:	O
</s>
<s>
Lock	B-Operating_System
.	O
</s>
<s>
If	O
,	O
lock	B-Operating_System
.	O
</s>
<s>
Lock	B-Operating_System
.	O
</s>
<s>
Lock	B-Operating_System
.	O
</s>
<s>
Alternatively	O
an	O
RW	B-Operating_System
lock	I-Operating_System
can	O
be	O
implemented	O
in	O
terms	O
of	O
a	O
condition	O
variable	O
,	O
,	O
an	O
ordinary	O
(	O
mutex	B-Operating_System
)	O
lock	B-Operating_System
,	O
,	O
and	O
various	O
counters	O
and	O
flags	O
describing	O
the	O
threads	O
that	O
are	O
currently	O
active	O
or	O
waiting	O
.	O
</s>
<s>
For	O
a	O
write-preferring	O
RW	B-Operating_System
lock	I-Operating_System
one	O
can	O
use	O
two	O
integer	O
counters	O
and	O
one	O
boolean	O
flag	O
:	O
</s>
<s>
:	O
whether	O
a	O
writer	O
has	O
acquired	O
the	O
lock	B-Operating_System
(	O
boolean	O
)	O
.	O
</s>
<s>
SRWLock	O
,	O
added	O
to	O
the	O
Windows	B-Application
operating	I-Application
system	I-Application
API	O
as	O
of	O
Windows	B-Application
Vista	I-Application
.	O
</s>
<s>
The	O
read-copy-update	B-Operating_System
(	O
RCU	O
)	O
algorithm	O
is	O
one	O
solution	O
to	O
the	O
readers	B-Operating_System
–	I-Operating_System
writers	I-Operating_System
problem	I-Operating_System
.	O
</s>
<s>
RCU	O
is	O
wait-free	B-Operating_System
for	O
readers	O
.	O
</s>
<s>
The	O
Linux	B-Operating_System
kernel	I-Operating_System
implements	O
a	O
special	O
solution	O
for	O
few	O
writers	O
called	O
seqlock	B-Application
.	O
</s>
