<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
a	O
lock	O
or	O
mutex	B-Operating_System
(	O
from	O
mutual	B-Operating_System
exclusion	I-Operating_System
)	O
is	O
a	O
synchronization	O
primitive	O
:	O
a	O
mechanism	O
that	O
enforces	O
limits	O
on	O
access	O
to	O
a	O
resource	O
when	O
there	O
are	O
many	O
threads	B-Operating_System
of	I-Operating_System
execution	I-Operating_System
.	O
</s>
<s>
A	O
lock	O
is	O
designed	O
to	O
enforce	O
a	O
mutual	B-Operating_System
exclusion	I-Operating_System
concurrency	B-Operating_System
control	I-Operating_System
policy	O
,	O
and	O
with	O
a	O
variety	O
of	O
possible	O
methods	B-Language
there	O
exists	O
multiple	O
unique	O
implementations	O
for	O
different	O
applications	O
.	O
</s>
<s>
Generally	O
,	O
locks	O
are	O
advisory	B-Operating_System
locks	I-Operating_System
,	O
where	O
each	O
thread	B-Operating_System
cooperates	O
by	O
acquiring	O
the	O
lock	O
before	O
accessing	O
the	O
corresponding	O
data	O
.	O
</s>
<s>
Some	O
systems	O
also	O
implement	O
mandatory	B-Operating_System
locks	I-Operating_System
,	O
where	O
attempting	O
unauthorized	O
access	O
to	O
a	O
locked	O
resource	O
will	O
force	O
an	O
exception	B-General_Concept
in	O
the	O
entity	O
attempting	O
to	O
make	O
the	O
access	O
.	O
</s>
<s>
The	O
simplest	O
type	O
of	O
lock	O
is	O
a	O
binary	B-Operating_System
semaphore	I-Operating_System
.	O
</s>
<s>
Another	O
way	O
to	O
classify	O
locks	O
is	O
by	O
what	O
happens	O
when	O
the	O
lock	O
strategy	O
prevents	O
the	O
progress	O
of	O
a	O
thread	B-Operating_System
.	O
</s>
<s>
Most	O
locking	B-Operating_System
designs	O
block	B-Operating_System
the	O
execution	B-General_Concept
of	O
the	O
thread	B-Operating_System
requesting	O
the	O
lock	O
until	O
it	O
is	O
allowed	O
to	O
access	O
the	O
locked	O
resource	O
.	O
</s>
<s>
With	O
a	O
spinlock	B-Operating_System
,	O
the	O
thread	B-Operating_System
simply	O
waits	O
(	O
"	O
spins	O
"	O
)	O
until	O
the	O
lock	O
becomes	O
available	O
.	O
</s>
<s>
This	O
is	O
efficient	O
if	O
threads	B-Operating_System
are	O
blocked	O
for	O
a	O
short	O
time	O
,	O
because	O
it	O
avoids	O
the	O
overhead	O
of	O
operating	O
system	O
process	O
re-scheduling	O
.	O
</s>
<s>
It	O
is	O
inefficient	O
if	O
the	O
lock	O
is	O
held	O
for	O
a	O
long	O
time	O
,	O
or	O
if	O
the	O
progress	O
of	O
the	O
thread	B-Operating_System
that	O
is	O
holding	O
the	O
lock	O
depends	O
on	O
preemption	O
of	O
the	O
locked	O
thread	B-Operating_System
.	O
</s>
<s>
This	O
support	O
usually	O
takes	O
the	O
form	O
of	O
one	O
or	O
more	O
atomic	B-General_Concept
instructions	I-General_Concept
such	O
as	O
"	O
test-and-set	B-Operating_System
"	O
,	O
"	O
fetch-and-add	B-Operating_System
"	O
or	O
"	O
compare-and-swap	B-Operating_System
"	O
.	O
</s>
<s>
These	O
instructions	O
allow	O
a	O
single	O
process	O
to	O
test	O
if	O
the	O
lock	O
is	O
free	O
,	O
and	O
if	O
free	O
,	O
acquire	O
the	O
lock	O
in	O
a	O
single	O
atomic	B-General_Concept
operation	I-General_Concept
.	O
</s>
<s>
Uniprocessor	B-Operating_System
architectures	O
have	O
the	O
option	O
of	O
using	O
uninterruptible	O
sequences	O
of	O
instructions	O
—	O
using	O
special	O
instructions	O
or	O
instruction	O
prefixes	O
to	O
disable	O
interrupts	O
temporarily	O
—	O
but	O
this	O
technique	O
does	O
not	O
work	O
for	O
multiprocessor	B-Operating_System
shared-memory	O
machines	O
.	O
</s>
<s>
Proper	O
support	O
for	O
locks	O
in	O
a	O
multiprocessor	B-Operating_System
environment	O
can	O
require	O
quite	O
complex	O
hardware	O
or	O
software	O
support	O
,	O
with	O
substantial	O
synchronization	O
issues	O
.	O
</s>
<s>
The	O
reason	O
an	O
atomic	B-General_Concept
operation	I-General_Concept
is	O
required	O
is	O
because	O
of	O
concurrency	O
,	O
where	O
more	O
than	O
one	O
task	O
executes	O
the	O
same	O
logic	O
.	O
</s>
<s>
For	O
example	O
,	O
consider	O
the	O
following	O
C	B-Language
code	O
:	O
</s>
<s>
Dekker	B-Operating_System
's	I-Operating_System
or	O
Peterson	B-Operating_System
's	I-Operating_System
algorithm	I-Operating_System
are	O
possible	O
substitutes	O
if	O
atomic	B-General_Concept
locking	B-Operating_System
operations	O
are	O
not	O
available	O
.	O
</s>
<s>
Careless	O
use	O
of	O
locks	O
can	O
result	O
in	O
deadlock	B-Operating_System
or	O
livelock	B-Application
.	O
</s>
<s>
A	O
number	O
of	O
strategies	O
can	O
be	O
used	O
to	O
avoid	O
or	O
recover	O
from	O
deadlocks	B-Operating_System
or	O
livelocks	B-Application
,	O
both	O
at	O
design-time	O
and	O
at	O
run-time	B-Library
.	O
</s>
<s>
An	O
example	O
in	O
C#	B-Application
follows	O
:	O
</s>
<s>
Similar	O
to	O
Java	B-Language
,	O
C#	B-Application
can	O
also	O
synchronize	O
entire	O
methods	B-Language
,	O
by	O
using	O
the	O
MethodImplOptionsSynchronized	O
attribute	O
.	O
</s>
<s>
Before	O
being	O
introduced	O
to	O
lock	O
granularity	B-Operating_System
,	O
one	O
needs	O
to	O
understand	O
three	O
concepts	O
about	O
locks	O
:	O
</s>
<s>
lock	O
contention	B-General_Concept
:	O
this	O
occurs	O
whenever	O
one	O
process	O
or	O
thread	B-Operating_System
attempts	O
to	O
acquire	O
a	O
lock	O
held	O
by	O
another	O
process	O
or	O
thread	B-Operating_System
.	O
</s>
<s>
The	O
more	O
fine-grained	O
the	O
available	O
locks	O
,	O
the	O
less	O
likely	O
one	O
process/thread	O
will	O
request	O
a	O
lock	O
held	O
by	O
the	O
other	O
.	O
</s>
<s>
(	O
For	O
example	O
,	O
locking	B-Operating_System
a	O
row	O
rather	O
than	O
the	O
entire	O
table	O
,	O
or	O
locking	B-Operating_System
a	O
cell	O
rather	O
than	O
the	O
entire	O
row	O
)	O
;	O
</s>
<s>
deadlock	B-Operating_System
:	O
the	O
situation	O
when	O
each	O
of	O
at	O
least	O
two	O
tasks	O
is	O
waiting	O
for	O
a	O
lock	O
that	O
the	O
other	O
task	O
holds	O
.	O
</s>
<s>
There	O
is	O
a	O
tradeoff	O
between	O
decreasing	O
lock	O
overhead	O
and	O
decreasing	O
lock	O
contention	B-General_Concept
when	O
choosing	O
the	O
number	O
of	O
locks	O
in	O
synchronization	O
.	O
</s>
<s>
An	O
important	O
property	O
of	O
a	O
lock	O
is	O
its	O
granularity	B-Operating_System
.	O
</s>
<s>
The	O
granularity	B-Operating_System
is	O
a	O
measure	O
of	O
the	O
amount	O
of	O
data	O
the	O
lock	O
is	O
protecting	O
.	O
</s>
<s>
In	O
general	O
,	O
choosing	O
a	O
coarse	O
granularity	B-Operating_System
(	O
a	O
small	O
number	O
of	O
locks	O
,	O
each	O
protecting	O
a	O
large	O
segment	O
of	O
data	O
)	O
results	O
in	O
less	O
lock	O
overhead	O
when	O
a	O
single	O
process	O
is	O
accessing	O
the	O
protected	O
data	O
,	O
but	O
worse	O
performance	O
when	O
multiple	O
processes	O
are	O
running	O
concurrently	O
.	O
</s>
<s>
This	O
is	O
because	O
of	O
increased	O
lock	O
contention	B-General_Concept
.	O
</s>
<s>
Conversely	O
,	O
using	O
a	O
fine	O
granularity	B-Operating_System
(	O
a	O
larger	O
number	O
of	O
locks	O
,	O
each	O
protecting	O
a	O
fairly	O
small	O
amount	O
of	O
data	O
)	O
increases	O
the	O
overhead	O
of	O
the	O
locks	O
themselves	O
but	O
reduces	O
lock	O
contention	B-General_Concept
.	O
</s>
<s>
Granular	O
locking	B-Operating_System
where	O
each	O
process	O
must	O
hold	O
multiple	O
locks	O
from	O
a	O
common	O
set	O
of	O
locks	O
can	O
create	O
subtle	O
lock	O
dependencies	O
.	O
</s>
<s>
This	O
subtlety	O
can	O
increase	O
the	O
chance	O
that	O
a	O
programmer	O
will	O
unknowingly	O
introduce	O
a	O
deadlock	B-Operating_System
.	O
</s>
<s>
In	O
a	O
database	B-Application
management	I-Application
system	I-Application
,	O
for	O
example	O
,	O
a	O
lock	O
could	O
protect	O
,	O
in	O
order	O
of	O
decreasing	O
granularity	B-Operating_System
,	O
part	O
of	O
a	O
field	O
,	O
a	O
field	O
,	O
a	O
record	O
,	O
a	O
data	O
page	O
,	O
or	O
an	O
entire	O
table	O
.	O
</s>
<s>
Coarse	O
granularity	B-Operating_System
,	O
such	O
as	O
using	O
table	O
locks	O
,	O
tends	O
to	O
give	O
the	O
best	O
performance	O
for	O
a	O
single	O
user	O
,	O
whereas	O
fine	O
granularity	B-Operating_System
,	O
such	O
as	O
record	O
locks	O
,	O
tends	O
to	O
give	O
the	O
best	O
performance	O
for	O
multiple	O
users	O
.	O
</s>
<s>
Database	B-Operating_System
locks	I-Operating_System
can	O
be	O
used	O
as	O
a	O
means	O
of	O
ensuring	O
transaction	O
synchronicity	O
.	O
</s>
<s>
when	O
making	O
transaction	O
processing	O
concurrent	O
(	O
interleaving	O
transactions	O
)	O
,	O
using	O
2-phased	B-Operating_System
locks	I-Operating_System
ensures	O
that	O
the	O
concurrent	O
execution	B-General_Concept
of	O
the	O
transaction	O
turns	O
out	O
equivalent	O
to	O
some	O
serial	O
ordering	O
of	O
the	O
transaction	O
.	O
</s>
<s>
However	O
,	O
deadlocks	B-Operating_System
become	O
an	O
unfortunate	O
side-effect	O
of	O
locking	B-Operating_System
in	O
databases	B-Application
.	O
</s>
<s>
Deadlocks	B-Operating_System
are	O
either	O
prevented	O
by	O
pre-determining	O
the	O
locking	B-Operating_System
order	O
between	O
transactions	O
or	O
are	O
detected	O
using	O
waits-for	B-Operating_System
graphs	I-Operating_System
.	O
</s>
<s>
An	O
alternate	O
to	O
locking	B-Operating_System
for	O
database	O
synchronicity	O
while	O
avoiding	O
deadlocks	B-Operating_System
involves	O
the	O
use	O
of	O
totally	O
ordered	O
global	O
timestamps	O
.	O
</s>
<s>
There	O
are	O
mechanisms	O
employed	O
to	O
manage	O
the	O
actions	O
of	O
multiple	O
concurrent	B-License
users	I-License
on	O
a	O
database	O
—	O
the	O
purpose	O
is	O
to	O
prevent	O
lost	O
updates	O
and	O
dirty	O
reads	O
.	O
</s>
<s>
The	O
two	O
types	O
of	O
locking	B-Operating_System
are	O
pessimistic	O
locking	B-Operating_System
and	O
optimistic	B-Operating_System
locking	I-Operating_System
:	O
</s>
<s>
Pessimistic	O
locking	B-Operating_System
:	O
a	O
user	O
who	O
reads	O
a	O
record	O
with	O
the	O
intention	O
of	O
updating	O
it	O
places	O
an	O
exclusive	O
lock	O
on	O
the	O
record	O
to	O
prevent	O
other	O
users	O
from	O
manipulating	O
it	O
.	O
</s>
<s>
Where	O
to	O
use	O
pessimistic	O
locking	B-Operating_System
:	O
this	O
is	O
mainly	O
used	O
in	O
environments	O
where	O
data-contention	O
(	O
the	O
degree	O
of	O
users	O
request	O
to	O
the	O
database	B-Application
system	I-Application
at	O
any	O
one	O
time	O
)	O
is	O
heavy	O
;	O
where	O
the	O
cost	O
of	O
protecting	O
data	O
through	O
locks	O
is	O
less	O
than	O
the	O
cost	O
of	O
rolling	O
back	O
transactions	O
,	O
if	O
concurrency	O
conflicts	O
occur	O
.	O
</s>
<s>
Optimistic	B-Operating_System
locking	I-Operating_System
:	O
this	O
allows	O
multiple	O
concurrent	B-License
users	I-License
access	O
to	O
the	O
database	O
whilst	O
the	O
system	O
keeps	O
a	O
copy	O
of	O
the	O
initial-read	O
made	O
by	O
each	O
user	O
.	O
</s>
<s>
It	O
improves	O
database	O
performance	O
by	O
reducing	O
the	O
amount	O
of	O
locking	B-Operating_System
required	O
,	O
thereby	O
reducing	O
the	O
load	O
on	O
the	O
database	O
server	O
.	O
</s>
<s>
The	O
downside	O
is	O
constant	O
update	O
failures	O
due	O
to	O
high	O
volumes	O
of	O
update	O
requests	O
from	O
multiple	O
concurrent	B-License
users	I-License
-	O
it	O
can	O
be	O
frustrating	O
for	O
users	O
.	O
</s>
<s>
Where	O
to	O
use	O
optimistic	B-Operating_System
locking	I-Operating_System
:	O
this	O
is	O
appropriate	O
in	O
environments	O
where	O
there	O
is	O
low	O
contention	B-General_Concept
for	O
data	O
,	O
or	O
where	O
read-only	O
access	O
to	O
data	O
is	O
required	O
.	O
</s>
<s>
Optimistic	B-Operating_System
concurrency	I-Operating_System
is	O
used	O
extensively	O
in	O
.NET	O
to	O
address	O
the	O
needs	O
of	O
mobile	O
and	O
disconnected	O
applications	O
,	O
where	O
locking	B-Operating_System
data	O
rows	O
for	O
prolonged	O
periods	O
of	O
time	O
would	O
be	O
infeasible	O
.	O
</s>
<s>
Lock-based	O
resource	O
protection	O
and	O
thread/process	O
synchronization	O
have	O
many	O
disadvantages	O
:	O
</s>
<s>
Contention	B-General_Concept
:	O
some	O
threads/processes	O
have	O
to	O
wait	O
until	O
a	O
lock	O
(	O
or	O
a	O
whole	O
set	O
of	O
locks	O
)	O
is	O
released	O
.	O
</s>
<s>
If	O
one	O
of	O
the	O
threads	B-Operating_System
holding	O
a	O
lock	O
dies	O
,	O
stalls	O
,	O
blocks	O
,	O
or	O
enters	O
an	O
infinite	O
loop	O
,	O
other	O
threads	B-Operating_System
waiting	O
for	O
the	O
lock	O
may	O
wait	O
indefinitely	O
until	O
the	O
computer	O
is	O
power	O
cycled	O
.	O
</s>
<s>
(	O
However	O
,	O
any	O
chance	O
for	O
such	O
collisions	O
is	O
a	O
race	B-Operating_System
condition	I-Operating_System
.	O
)	O
</s>
<s>
Debugging	O
:	O
bugs	O
associated	O
with	O
locks	O
are	O
time	O
dependent	O
and	O
can	O
be	O
very	O
subtle	O
and	O
extremely	O
hard	O
to	O
replicate	O
,	O
such	O
as	O
deadlocks	B-Operating_System
.	O
</s>
<s>
Instability	O
:	O
the	O
optimal	O
balance	O
between	O
lock	O
overhead	O
and	O
lock	O
contention	B-General_Concept
can	O
be	O
unique	O
to	O
the	O
problem	O
domain	O
(	O
application	O
)	O
and	O
sensitive	O
to	O
design	O
,	O
implementation	O
,	O
and	O
even	O
low-level	O
system	O
architectural	O
changes	O
.	O
</s>
<s>
Priority	B-Operating_System
inversion	I-Operating_System
:	O
a	O
low-priority	O
thread/process	O
holding	O
a	O
common	O
lock	O
can	O
prevent	O
high-priority	O
threads/processes	O
from	O
proceeding	O
.	O
</s>
<s>
Priority	B-Operating_System
inheritance	I-Operating_System
can	O
be	O
used	O
to	O
reduce	O
priority-inversion	O
duration	O
.	O
</s>
<s>
The	O
priority	B-Operating_System
ceiling	I-Operating_System
protocol	I-Operating_System
can	O
be	O
used	O
on	O
uniprocessor	B-Operating_System
systems	I-Operating_System
to	O
minimize	O
the	O
worst-case	O
priority-inversion	O
duration	O
,	O
as	O
well	O
as	O
prevent	O
deadlock	B-Operating_System
.	O
</s>
<s>
Convoying	B-Operating_System
:	O
all	O
other	O
threads	B-Operating_System
have	O
to	O
wait	O
if	O
a	O
thread	B-Operating_System
holding	O
a	O
lock	O
is	O
descheduled	O
due	O
to	O
a	O
time-slice	O
interrupt	O
or	O
page	O
fault	O
.	O
</s>
<s>
Some	O
concurrency	B-Operating_System
control	I-Operating_System
strategies	O
avoid	O
some	O
or	O
all	O
of	O
these	O
problems	O
.	O
</s>
<s>
For	O
example	O
,	O
a	O
funnel	B-Operating_System
or	O
serializing	B-Operating_System
tokens	I-Operating_System
can	O
avoid	O
the	O
biggest	O
problem	O
:	O
deadlocks	B-Operating_System
.	O
</s>
<s>
Alternatives	O
to	O
locking	B-Operating_System
include	O
non-blocking	B-Operating_System
synchronization	I-Operating_System
methods	B-Language
,	O
like	O
lock-free	B-Operating_System
programming	I-Operating_System
techniques	O
and	O
transactional	B-Operating_System
memory	I-Operating_System
.	O
</s>
<s>
However	O
,	O
such	O
alternative	O
methods	B-Language
often	O
require	O
that	O
the	O
actual	O
lock	O
mechanisms	O
be	O
implemented	O
at	O
a	O
more	O
fundamental	O
level	O
of	O
the	O
operating	O
software	O
.	O
</s>
<s>
In	O
most	O
cases	O
,	O
proper	O
locking	B-Operating_System
depends	O
on	O
the	O
CPU	O
providing	O
a	O
method	O
of	O
atomic	B-General_Concept
instruction	I-General_Concept
stream	O
synchronization	O
(	O
for	O
example	O
,	O
the	O
addition	O
or	O
deletion	O
of	O
an	O
item	O
into	O
a	O
pipeline	O
requires	O
that	O
all	O
contemporaneous	O
operations	O
needing	O
to	O
add	O
or	O
delete	O
other	O
items	O
in	O
the	O
pipe	O
be	O
suspended	O
during	O
the	O
manipulation	O
of	O
the	O
memory	O
content	O
required	O
to	O
add	O
or	O
delete	O
the	O
specific	O
item	O
)	O
.	O
</s>
<s>
One	O
of	O
lock-based	O
programming	O
's	O
biggest	O
problems	O
is	O
that	O
"	O
locks	O
do	O
n't	O
compose	B-Application
"	O
:	O
it	O
is	O
hard	O
to	O
combine	O
small	O
,	O
correct	O
lock-based	O
modules	O
into	O
equally	O
correct	O
larger	O
programs	O
without	O
modifying	O
the	O
modules	O
or	O
at	O
least	O
knowing	O
about	O
their	O
internals	O
.	O
</s>
<s>
Simon	O
Peyton	O
Jones	O
(	O
an	O
advocate	O
of	O
software	B-Operating_System
transactional	I-Operating_System
memory	I-Operating_System
)	O
gives	O
the	O
following	O
example	O
of	O
a	O
banking	O
application	O
:	O
</s>
<s>
In	O
a	O
concurrent	O
program	O
,	O
this	O
algorithm	O
is	O
incorrect	O
because	O
when	O
one	O
thread	B-Operating_System
is	O
halfway	O
through	O
,	O
another	O
might	O
observe	O
a	O
state	O
where	O
has	O
been	O
withdrawn	O
from	O
the	O
first	O
account	O
,	O
but	O
not	O
yet	O
deposited	O
into	O
the	O
other	O
account	O
:	O
money	O
has	O
gone	O
missing	O
from	O
the	O
system	O
.	O
</s>
<s>
This	O
problem	O
can	O
only	O
be	O
fixed	O
completely	O
by	O
taking	O
locks	O
on	O
both	O
account	O
prior	O
to	O
changing	O
any	O
of	O
the	O
two	O
accounts	O
,	O
but	O
then	O
the	O
locks	O
have	O
to	O
be	O
taken	O
according	O
to	O
some	O
arbitrary	O
,	O
global	O
ordering	O
to	O
prevent	O
deadlock	B-Operating_System
:	O
</s>
<s>
This	O
solution	O
gets	O
more	O
complicated	O
when	O
more	O
locks	O
are	O
involved	O
,	O
and	O
the	O
function	O
needs	O
to	O
know	O
about	O
all	O
of	O
the	O
locks	O
,	O
so	O
they	O
cannot	O
be	O
hidden	B-Application
.	O
</s>
<s>
Ada	B-Language
provides	O
protected	O
objects	O
that	O
have	O
visible	O
protected	O
subprograms	O
or	O
entries	O
as	O
well	O
as	O
rendezvous	O
.	O
</s>
<s>
The	O
ISO/IEC	O
C	B-Language
standard	O
provides	O
a	O
standard	O
mutual	B-Operating_System
exclusion	I-Operating_System
(	O
locks	O
)	O
API	B-Application
since	O
C11	O
.	O
</s>
<s>
The	O
current	O
ISO/IEC	O
C++	B-Language
standard	O
supports	O
threading	O
facilities	O
since	O
C++11	B-Language
.	I-Language
</s>
<s>
The	O
OpenMP	B-Application
standard	O
is	O
supported	O
by	O
some	O
compilers	O
,	O
and	O
allows	O
critical	B-Operating_System
sections	I-Operating_System
to	O
be	O
specified	O
using	O
pragmas	O
.	O
</s>
<s>
The	O
POSIX	B-Operating_System
pthread	I-Operating_System
API	B-Application
provides	O
lock	O
support	O
.	O
</s>
<s>
Visual	B-Application
C++	I-Application
provides	O
the	O
synchronize	O
attribute	O
of	O
methods	B-Language
to	O
be	O
synchronized	O
,	O
but	O
this	O
is	O
specific	O
to	O
COM	O
objects	O
in	O
the	O
Windows	B-Application
architecture	O
and	O
Visual	B-Application
C++	I-Application
compiler	O
.	O
</s>
<s>
C	B-Language
and	O
C++	B-Language
can	O
easily	O
access	O
any	O
native	O
operating	O
system	O
locking	B-Operating_System
features	O
.	O
</s>
<s>
C#	B-Application
provides	O
the	O
lock	O
keyword	O
on	O
a	O
thread	B-Operating_System
to	O
ensure	O
its	O
exclusive	O
access	O
to	O
a	O
resource	O
.	O
</s>
<s>
VB.NET	B-Language
provides	O
a	O
SyncLock	O
keyword	O
like	O
C#'s	O
lock	O
keyword	O
.	O
</s>
<s>
Java	B-Language
provides	O
the	O
keyword	O
synchronized	O
to	O
lock	O
code	O
blocks	O
,	O
methods	B-Language
or	O
objects	O
and	O
libraries	O
featuring	O
concurrency-safe	O
data	O
structures	O
.	O
</s>
<s>
Objective-C	B-Language
provides	O
the	O
keyword	O
@synchronized	O
to	O
put	O
locks	O
on	O
blocks	O
of	O
code	O
and	O
also	O
provides	O
the	O
classes	O
NSLock	O
,	O
NSRecursiveLock	O
,	O
and	O
NSConditionLock	O
along	O
with	O
the	O
NSLocking	O
protocol	O
for	O
locking	B-Operating_System
as	O
well	O
.	O
</s>
<s>
PHP	B-Application
provides	O
a	O
file-based	O
locking	B-Operating_System
as	O
well	O
as	O
a	O
Mutex	B-Operating_System
class	O
in	O
the	O
pthreads	B-Operating_System
extension	O
.	O
</s>
<s>
Python	B-Language
provides	O
a	O
low-level	O
mutex	B-Operating_System
mechanism	O
with	O
a	O
Lock	O
class	O
from	O
the	O
threading	O
module	O
.	O
</s>
<s>
The	O
ISO/IEC	O
Fortran	B-Application
standard	O
(	O
ISO/IEC	O
1539-1:2010	O
)	O
provides	O
the	O
lock_type	O
derived	O
type	O
in	O
the	O
intrinsic	O
module	O
iso_fortran_env	O
and	O
the	O
lock/unlock	O
statements	O
since	O
Fortran	B-Application
2008	O
.	O
</s>
<s>
Ruby	B-Language
provides	O
a	O
low-level	O
mutex	B-Operating_System
object	O
and	O
no	O
keyword	O
.	O
</s>
<s>
Rust	B-Application
provides	O
the	O
Mutexxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx1	O
struct	B-Application
.	O
</s>
<s>
x86	B-Language
assembly	I-Language
provides	O
the	O
LOCK	O
prefix	O
on	O
certain	O
operations	O
to	O
guarantee	O
their	O
atomicity	B-General_Concept
.	O
</s>
<s>
Haskell	B-Language
implements	O
locking	B-Operating_System
via	O
a	O
mutable	O
data	O
structure	O
called	O
an	O
MVar	O
,	O
which	O
can	O
either	O
be	O
empty	O
or	O
contain	O
a	O
value	O
,	O
typically	O
a	O
reference	O
to	O
a	O
resource	O
.	O
</s>
<s>
A	O
thread	B-Operating_System
that	O
wants	O
to	O
use	O
the	O
resource	O
‘	O
takes’	O
the	O
value	O
of	O
the	O
MVar	O
,	O
leaving	O
it	O
empty	O
,	O
and	O
puts	O
it	O
back	O
when	O
it	O
is	O
finished	O
.	O
</s>
<s>
Attempting	O
to	O
take	O
a	O
resource	O
from	O
an	O
empty	O
MVar	O
results	O
in	O
the	O
thread	B-Operating_System
blocking	B-Operating_System
until	O
the	O
resource	O
is	O
available	O
.	O
</s>
<s>
As	O
an	O
alternative	O
to	O
locking	B-Operating_System
,	O
an	O
implementation	O
of	O
software	B-Operating_System
transactional	I-Operating_System
memory	I-Operating_System
also	O
exists	O
.	O
</s>
<s>
Go	B-Application
provides	O
a	O
low-level	O
Mutex	B-Operating_System
object	O
in	O
standard	O
's	O
library	O
package	O
.	O
</s>
<s>
It	O
can	O
be	O
used	O
for	O
locking	B-Operating_System
code	O
blocks	O
,	O
methods	B-Language
or	O
objects	O
.	O
</s>
