<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
an	O
algorithm	O
is	O
called	O
non-blocking	O
if	O
failure	O
or	O
suspension	O
of	O
any	O
thread	B-Operating_System
cannot	O
cause	O
failure	O
or	O
suspension	O
of	O
another	O
thread	B-Operating_System
;	O
for	O
some	O
operations	O
,	O
these	O
algorithms	O
provide	O
a	O
useful	O
alternative	O
to	O
traditional	O
blocking	B-Operating_System
implementations	I-Operating_System
.	O
</s>
<s>
A	O
non-blocking	B-Operating_System
algorithm	I-Operating_System
is	O
lock-free	B-Operating_System
if	O
there	O
is	O
guaranteed	O
system-wide	O
progress	B-Operating_System
,	O
and	O
wait-free	B-Operating_System
if	O
there	O
is	O
also	O
guaranteed	O
per-thread	O
progress	B-Operating_System
.	O
</s>
<s>
"	O
Non-blocking	O
"	O
was	O
used	O
as	O
a	O
synonym	O
for	O
"	O
lock-free	B-Operating_System
"	O
in	O
the	O
literature	O
until	O
the	O
introduction	O
of	O
obstruction-freedom	O
in	O
2003	O
.	O
</s>
<s>
The	O
word	O
"	O
non-blocking	O
"	O
was	O
traditionally	O
used	O
to	O
describe	O
telecommunications	O
networks	O
that	O
could	O
route	O
a	O
connection	O
through	O
a	O
set	O
of	O
relays	O
"	O
without	O
having	O
to	O
re-arrange	O
existing	O
calls	O
"	O
(	O
see	O
Clos	B-Device
network	I-Device
)	O
.	O
</s>
<s>
Also	O
,	O
if	O
the	O
telephone	O
exchange	O
"	O
is	O
not	O
defective	O
,	O
it	O
can	O
always	O
make	O
the	O
connection	O
"	O
(	O
see	O
nonblocking	B-Algorithm
minimal	I-Algorithm
spanning	I-Algorithm
switch	I-Algorithm
)	O
.	O
</s>
<s>
The	O
traditional	O
approach	O
to	O
multi-threaded	B-Operating_System
programming	I-Operating_System
is	O
to	O
use	O
locks	B-Operating_System
to	O
synchronize	O
access	O
to	O
shared	O
resources	B-General_Concept
.	O
</s>
<s>
Synchronization	O
primitives	O
such	O
as	O
mutexes	B-Operating_System
,	O
semaphores	B-Operating_System
,	O
and	O
critical	B-Operating_System
sections	I-Operating_System
are	O
all	O
mechanisms	O
by	O
which	O
a	O
programmer	O
can	O
ensure	O
that	O
certain	O
sections	O
of	O
code	O
do	O
not	O
execute	O
concurrently	O
,	O
if	O
doing	O
so	O
would	O
corrupt	O
shared	O
memory	O
structures	O
.	O
</s>
<s>
If	O
one	O
thread	B-Operating_System
attempts	O
to	O
acquire	O
a	O
lock	O
that	O
is	O
already	O
held	O
by	O
another	O
thread	B-Operating_System
,	O
the	O
thread	B-Operating_System
will	O
block	O
until	O
the	O
lock	O
is	O
free	O
.	O
</s>
<s>
Blocking	O
a	O
thread	B-Operating_System
can	O
be	O
undesirable	O
for	O
many	O
reasons	O
.	O
</s>
<s>
An	O
obvious	O
reason	O
is	O
that	O
while	O
the	O
thread	B-Operating_System
is	O
blocked	O
,	O
it	O
cannot	O
accomplish	O
anything	O
:	O
if	O
the	O
blocked	O
thread	B-Operating_System
had	O
been	O
performing	O
a	O
high-priority	O
or	O
real-time	B-General_Concept
task	O
,	O
it	O
would	O
be	O
highly	O
undesirable	O
to	O
halt	O
its	O
progress	B-Operating_System
.	O
</s>
<s>
For	O
example	O
,	O
certain	O
interactions	O
between	O
locks	B-Operating_System
can	O
lead	O
to	O
error	O
conditions	O
such	O
as	O
deadlock	B-Operating_System
,	O
livelock	B-Application
,	O
and	O
priority	B-Operating_System
inversion	I-Operating_System
.	O
</s>
<s>
Using	O
locks	B-Operating_System
also	O
involves	O
a	O
trade-off	O
between	O
coarse-grained	O
locking	B-Operating_System
,	O
which	O
can	O
significantly	O
reduce	O
opportunities	O
for	O
parallelism	B-Operating_System
,	O
and	O
fine-grained	O
locking	B-Operating_System
,	O
which	O
requires	O
more	O
careful	O
design	O
,	O
increases	O
locking	B-Operating_System
overhead	O
and	O
is	O
more	O
prone	O
to	O
bugs	O
.	O
</s>
<s>
Unlike	O
blocking	O
algorithms	O
,	O
non-blocking	B-Operating_System
algorithms	I-Operating_System
do	O
not	O
suffer	O
from	O
these	O
downsides	O
,	O
and	O
in	O
addition	O
are	O
safe	O
for	O
use	O
in	O
interrupt	B-General_Concept
handlers	I-General_Concept
:	O
even	O
though	O
the	O
preempted	O
thread	B-Operating_System
cannot	O
be	O
resumed	O
,	O
progress	B-Operating_System
is	O
still	O
possible	O
without	O
it	O
.	O
</s>
<s>
In	O
contrast	O
,	O
global	O
data	B-General_Concept
structures	I-General_Concept
protected	O
by	O
mutual	B-Operating_System
exclusion	I-Operating_System
cannot	O
safely	O
be	O
accessed	O
in	O
an	O
interrupt	B-General_Concept
handler	I-General_Concept
,	O
as	O
the	O
preempted	O
thread	B-Operating_System
may	O
be	O
the	O
one	O
holding	O
the	O
lock	O
—	O
but	O
this	O
can	O
be	O
rectified	O
easily	O
by	O
masking	O
the	O
interrupt	O
request	O
during	O
the	O
critical	B-Operating_System
section	I-Operating_System
.	O
</s>
<s>
A	O
lock-free	B-Operating_System
data	B-General_Concept
structure	I-General_Concept
can	O
be	O
used	O
to	O
improve	O
performance	O
.	O
</s>
<s>
A	O
lock-free	B-Operating_System
data	B-General_Concept
structure	I-General_Concept
increases	O
the	O
amount	O
of	O
time	O
spent	O
in	O
parallel	O
execution	O
rather	O
than	O
serial	O
execution	O
,	O
improving	O
performance	O
on	O
a	O
multi-core	B-Architecture
processor	I-Architecture
,	O
because	O
access	O
to	O
the	O
shared	O
data	B-General_Concept
structure	I-General_Concept
does	O
not	O
need	O
to	O
be	O
serialized	O
to	O
stay	O
coherent	O
.	O
</s>
<s>
With	O
few	O
exceptions	O
,	O
non-blocking	B-Operating_System
algorithms	I-Operating_System
use	O
atomic	B-General_Concept
read-modify-write	B-Operating_System
primitives	O
that	O
the	O
hardware	O
must	O
provide	O
,	O
the	O
most	O
notable	O
of	O
which	O
is	O
compare	B-Operating_System
and	I-Operating_System
swap	I-Operating_System
(	O
CAS	B-Operating_System
)	O
.	O
</s>
<s>
Critical	B-Operating_System
sections	I-Operating_System
are	O
almost	O
always	O
implemented	O
using	O
standard	O
interfaces	O
over	O
these	O
primitives	O
(	O
in	O
the	O
general	O
case	O
,	O
critical	B-Operating_System
sections	I-Operating_System
will	O
be	O
blocking	O
,	O
even	O
when	O
implemented	O
with	O
these	O
primitives	O
)	O
.	O
</s>
<s>
In	O
the	O
1990s	O
all	O
non-blocking	B-Operating_System
algorithms	I-Operating_System
had	O
to	O
be	O
written	O
"	O
natively	O
"	O
with	O
the	O
underlying	O
primitives	O
to	O
achieve	O
acceptable	O
performance	O
.	O
</s>
<s>
However	O
,	O
the	O
emerging	O
field	O
of	O
software	B-Operating_System
transactional	I-Operating_System
memory	I-Operating_System
promises	O
standard	O
abstractions	O
for	O
writing	O
efficient	O
non-blocking	O
code	O
.	O
</s>
<s>
Much	O
research	O
has	O
also	O
been	O
done	O
in	O
providing	O
basic	O
data	B-General_Concept
structures	I-General_Concept
such	O
as	O
stacks	B-Application
,	O
queues	B-Application
,	O
sets	O
,	O
and	O
hash	B-Algorithm
tables	I-Algorithm
.	O
</s>
<s>
These	O
allow	O
programs	O
to	O
easily	O
exchange	O
data	O
between	O
threads	B-Operating_System
asynchronously	O
.	O
</s>
<s>
Additionally	O
,	O
some	O
non-blocking	B-Operating_System
data	I-Operating_System
structures	I-Operating_System
are	O
weak	O
enough	O
to	O
be	O
implemented	O
without	O
special	O
atomic	B-General_Concept
primitives	I-General_Concept
.	O
</s>
<s>
Read-copy-update	B-Operating_System
with	O
a	O
single	O
writer	O
and	O
any	O
number	O
of	O
readers	O
.	O
</s>
<s>
(	O
The	O
readers	O
are	O
wait-free	B-Operating_System
;	O
the	O
writer	O
is	O
usually	O
lock-free	B-Operating_System
,	O
until	O
it	O
needs	O
to	O
reclaim	O
memory	O
)	O
.	O
</s>
<s>
Read-copy-update	B-Operating_System
with	O
multiple	O
writers	O
and	O
any	O
number	O
of	O
readers	O
.	O
</s>
<s>
(	O
The	O
readers	O
are	O
wait-free	B-Operating_System
;	O
multiple	O
writers	O
generally	O
serialize	O
with	O
a	O
lock	O
and	O
are	O
not	O
obstruction-free	O
)	O
.	O
</s>
<s>
Several	O
libraries	O
internally	O
use	O
lock-free	B-Operating_System
techniques	O
,	O
but	O
it	O
is	O
difficult	O
to	O
write	O
lock-free	B-Operating_System
code	O
that	O
is	O
correct	O
.	O
</s>
<s>
Non-blocking	B-Operating_System
algorithms	I-Operating_System
generally	O
involve	O
a	O
series	O
of	O
read	O
,	O
read-modify-write	B-Operating_System
,	O
and	O
write	O
instructions	O
in	O
a	O
carefully	O
designed	O
order	O
.	O
</s>
<s>
Even	O
when	O
they	O
do	O
n't	O
,	O
many	O
modern	O
CPUs	O
often	O
re-arrange	O
such	O
operations	O
(	O
they	O
have	O
a	O
"	O
weak	O
consistency	B-General_Concept
model	I-General_Concept
"	O
)	O
,	O
</s>
<s>
unless	O
a	O
memory	B-General_Concept
barrier	I-General_Concept
is	O
used	O
to	O
tell	O
the	O
CPU	O
not	O
to	O
reorder	O
.	O
</s>
<s>
C++11	B-Language
programmers	O
can	O
use	O
std::atomic	O
in	O
<atomic>	O
,	O
</s>
<s>
both	O
of	O
which	O
supply	O
types	O
and	O
functions	O
that	O
tell	O
the	O
compiler	O
not	O
to	O
re-arrange	O
such	O
instructions	O
,	O
and	O
to	O
insert	O
the	O
appropriate	O
memory	B-General_Concept
barriers	I-General_Concept
.	O
</s>
<s>
Wait-freedom	O
is	O
the	O
strongest	O
non-blocking	O
guarantee	O
of	O
progress	B-Operating_System
,	O
combining	O
guaranteed	O
system-wide	O
throughput	O
with	O
starvation-freedom	B-Operating_System
.	O
</s>
<s>
An	O
algorithm	O
is	O
wait-free	B-Operating_System
if	O
every	O
operation	O
has	O
a	O
bound	O
on	O
the	O
number	O
of	O
steps	O
the	O
algorithm	O
will	O
take	O
before	O
the	O
operation	O
completes	O
.	O
</s>
<s>
This	O
property	O
is	O
critical	O
for	O
real-time	B-General_Concept
systems	I-General_Concept
and	O
is	O
always	O
nice	O
to	O
have	O
as	O
long	O
as	O
the	O
performance	O
cost	O
is	O
not	O
too	O
high	O
.	O
</s>
<s>
It	O
was	O
shown	O
in	O
the	O
1980s	O
that	O
all	O
algorithms	O
can	O
be	O
implemented	O
wait-free	B-Operating_System
,	O
and	O
many	O
transformations	O
from	O
serial	O
code	O
,	O
called	O
universal	O
constructions	O
,	O
have	O
been	O
demonstrated	O
.	O
</s>
<s>
Several	O
papers	O
have	O
investigated	O
the	O
difficulty	O
of	O
creating	O
wait-free	B-Operating_System
algorithms	I-Operating_System
.	O
</s>
<s>
For	O
example	O
,	O
it	O
has	O
been	O
shown	O
that	O
the	O
widely	O
available	O
atomic	B-General_Concept
conditional	O
primitives	O
,	O
CAS	B-Operating_System
and	O
LL/SC	B-Operating_System
,	O
cannot	O
provide	O
starvation-free	O
implementations	O
of	O
many	O
common	O
data	B-General_Concept
structures	I-General_Concept
without	O
memory	O
costs	O
growing	O
linearly	O
in	O
the	O
number	O
of	O
threads	B-Operating_System
.	O
</s>
<s>
But	O
in	O
practice	O
these	O
lower	O
bounds	O
do	O
not	O
present	O
a	O
real	O
barrier	O
as	O
spending	O
a	O
cache	O
line	O
or	O
exclusive	O
reservation	O
granule	O
(	O
up	O
to	O
2KB	O
on	O
ARM	O
)	O
of	O
store	O
per	O
thread	B-Operating_System
in	O
the	O
shared	O
memory	O
is	O
not	O
considered	O
too	O
costly	O
for	O
practical	O
systems	O
(	O
typically	O
the	O
amount	O
of	O
store	O
logically	O
required	O
is	O
a	O
word	O
,	O
but	O
physically	O
CAS	B-Operating_System
operations	O
on	O
the	O
same	O
cache	O
line	O
will	O
collide	O
,	O
and	O
LL/SC	B-Operating_System
operations	O
in	O
the	O
same	O
exclusive	O
reservation	O
granule	O
will	O
collide	O
,	O
so	O
the	O
amount	O
of	O
store	O
physically	O
required	O
is	O
greater	O
)	O
.	O
</s>
<s>
Wait-free	B-Operating_System
algorithms	I-Operating_System
were	O
rare	O
until	O
2011	O
,	O
both	O
in	O
research	O
and	O
in	O
practice	O
.	O
</s>
<s>
However	O
,	O
in	O
2011	O
Kogan	O
and	O
Petrank	O
presented	O
a	O
wait-free	B-Operating_System
queue	B-Application
building	O
on	O
the	O
CAS	B-Operating_System
primitive	O
,	O
generally	O
available	O
on	O
common	O
hardware	O
.	O
</s>
<s>
Their	O
construction	O
expanded	O
the	O
lock-free	B-Operating_System
queue	B-Application
of	O
Michael	O
and	O
Scott	O
,	O
which	O
is	O
an	O
efficient	O
queue	B-Application
often	O
used	O
in	O
practice	O
.	O
</s>
<s>
A	O
follow-up	O
paper	O
by	O
Kogan	O
and	O
Petrank	O
provided	O
a	O
method	O
for	O
making	O
wait-free	B-Operating_System
algorithms	I-Operating_System
fast	O
and	O
used	O
this	O
method	O
to	O
make	O
the	O
wait-free	B-Operating_System
queue	B-Application
practically	O
as	O
fast	O
as	O
its	O
lock-free	B-Operating_System
counterpart	O
.	O
</s>
<s>
A	O
subsequent	O
paper	O
by	O
Timnat	O
and	O
Petrank	O
provided	O
an	O
automatic	O
mechanism	O
for	O
generating	O
wait-free	B-Operating_System
data	B-General_Concept
structures	I-General_Concept
from	O
lock-free	B-Operating_System
ones	O
.	O
</s>
<s>
Thus	O
,	O
wait-free	B-Operating_System
implementations	O
are	O
now	O
available	O
for	O
many	O
data-structures	O
.	O
</s>
<s>
Lock-freedom	O
allows	O
individual	O
threads	B-Operating_System
to	O
starve	O
but	O
guarantees	O
system-wide	O
throughput	O
.	O
</s>
<s>
progress	B-Operating_System
(	O
for	O
some	O
sensible	O
definition	O
of	O
progress	B-Operating_System
)	O
.	O
</s>
<s>
All	O
wait-free	B-Operating_System
algorithms	I-Operating_System
are	O
lock-free	B-Operating_System
.	O
</s>
<s>
In	O
particular	O
,	O
if	O
one	O
thread	B-Operating_System
is	O
suspended	O
,	O
then	O
a	O
lock-free	B-Operating_System
algorithm	I-Operating_System
guarantees	O
that	O
the	O
remaining	O
threads	B-Operating_System
can	O
still	O
make	O
progress	B-Operating_System
.	O
</s>
<s>
Hence	O
,	O
if	O
two	O
threads	B-Operating_System
can	O
contend	O
for	O
the	O
same	O
mutex	B-Operating_System
lock	I-Operating_System
or	O
spinlock	O
,	O
then	O
the	O
algorithm	O
is	O
not	O
lock-free	B-Operating_System
.	O
</s>
<s>
(	O
If	O
we	O
suspend	O
one	O
thread	B-Operating_System
that	O
holds	O
the	O
lock	O
,	O
then	O
the	O
second	O
thread	B-Operating_System
will	O
block	O
.	O
)	O
</s>
<s>
An	O
algorithm	O
is	O
lock-free	B-Operating_System
if	O
infinitely	O
often	O
operation	O
by	O
some	O
processors	O
will	O
succeed	O
in	O
a	O
finite	O
number	O
of	O
steps	O
.	O
</s>
<s>
The	O
difference	O
between	O
wait-free	B-Operating_System
and	O
lock-free	B-Operating_System
is	O
that	O
wait-free	B-Operating_System
operation	O
by	O
each	O
process	O
is	O
guaranteed	O
to	O
succeed	O
in	O
a	O
finite	O
number	O
of	O
steps	O
,	O
regardless	O
of	O
the	O
other	O
processors	O
.	O
</s>
<s>
In	O
general	O
,	O
a	O
lock-free	B-Operating_System
algorithm	I-Operating_System
can	O
run	O
in	O
four	O
phases	O
:	O
completing	O
one	O
's	O
own	O
operation	O
,	O
assisting	O
an	O
obstructing	O
operation	O
,	O
aborting	O
an	O
obstructing	O
operation	O
,	O
and	O
waiting	O
.	O
</s>
<s>
Completing	O
one	O
's	O
own	O
operation	O
is	O
complicated	O
by	O
the	O
possibility	O
of	O
concurrent	B-Operating_System
assistance	O
and	O
abortion	O
,	O
but	O
is	O
invariably	O
the	O
fastest	O
path	O
to	O
completion	O
.	O
</s>
<s>
Correct	O
concurrent	B-Operating_System
assistance	O
is	O
typically	O
the	O
most	O
complex	O
part	O
of	O
a	O
lock-free	B-Operating_System
algorithm	I-Operating_System
,	O
and	O
often	O
very	O
costly	O
to	O
execute	O
:	O
not	O
only	O
does	O
the	O
assisting	O
thread	B-Operating_System
slow	O
down	O
,	O
but	O
thanks	O
to	O
the	O
mechanics	O
of	O
shared	O
memory	O
,	O
the	O
thread	B-Operating_System
being	O
assisted	O
will	O
be	O
slowed	O
,	O
too	O
,	O
if	O
it	O
is	O
still	O
running	O
.	O
</s>
<s>
Obstruction-freedom	O
is	O
the	O
weakest	O
natural	O
non-blocking	O
progress	B-Operating_System
guarantee	O
.	O
</s>
<s>
An	O
algorithm	O
is	O
obstruction-free	O
if	O
at	O
any	O
point	O
,	O
a	O
single	B-Operating_System
thread	I-Operating_System
executed	O
in	O
isolation	O
(	O
i.e.	O
,	O
with	O
all	O
obstructing	O
threads	B-Operating_System
suspended	O
)	O
for	O
a	O
bounded	O
number	O
of	O
steps	O
will	O
complete	O
its	O
operation	O
.	O
</s>
<s>
All	O
lock-free	B-Operating_System
algorithms	I-Operating_System
are	O
obstruction-free	O
.	O
</s>
<s>
Dropping	O
concurrent	B-Operating_System
assistance	O
can	O
often	O
result	O
in	O
much	O
simpler	O
algorithms	O
that	O
are	O
easier	O
to	O
validate	O
.	O
</s>
<s>
Preventing	O
the	O
system	O
from	O
continually	O
live-locking	B-Application
is	O
the	O
task	O
of	O
a	O
contention	O
manager	O
.	O
</s>
<s>
Some	O
obstruction-free	O
algorithms	O
use	O
a	O
pair	O
of	O
"	O
consistency	O
markers	O
"	O
in	O
the	O
data	B-General_Concept
structure	I-General_Concept
.	O
</s>
<s>
Processes	O
reading	O
the	O
data	B-General_Concept
structure	I-General_Concept
first	O
read	O
one	O
consistency	O
marker	O
,	O
then	O
read	O
the	O
relevant	O
data	O
into	O
an	O
internal	O
buffer	O
,	O
then	O
read	O
the	O
other	O
marker	O
,	O
and	O
then	O
compare	O
the	O
markers	O
.	O
</s>
<s>
Markers	O
may	O
be	O
non-identical	O
when	O
the	O
read	O
is	O
interrupted	O
by	O
another	O
process	O
updating	O
the	O
data	B-General_Concept
structure	I-General_Concept
.	O
</s>
