<s>
Thread	B-Operating_System
safety	I-Operating_System
is	O
a	O
computer	B-General_Concept
programming	I-General_Concept
concept	O
applicable	O
to	O
multi-threaded	B-Operating_System
code	O
.	O
</s>
<s>
Thread-safe	B-Operating_System
code	O
only	O
manipulates	O
shared	O
data	O
structures	O
in	O
a	O
manner	O
that	O
ensures	O
that	O
all	O
threads	B-Operating_System
behave	O
properly	O
and	O
fulfill	O
their	O
design	O
specifications	O
without	O
unintended	O
interaction	O
.	O
</s>
<s>
There	O
are	O
various	O
strategies	O
for	O
making	O
thread-safe	B-Operating_System
data	O
structures	O
.	O
</s>
<s>
A	O
program	O
may	O
execute	O
code	O
in	O
several	O
threads	B-Operating_System
simultaneously	O
in	O
a	O
shared	O
address	B-General_Concept
space	I-General_Concept
where	O
each	O
of	O
those	O
threads	B-Operating_System
has	O
access	O
to	O
virtually	O
all	O
of	O
the	O
memory	B-General_Concept
of	O
every	O
other	O
thread	B-Operating_System
.	O
</s>
<s>
Thread	B-Operating_System
safety	I-Operating_System
is	O
a	O
property	O
that	O
allows	O
code	O
to	O
run	O
in	O
multithreaded	O
environments	O
by	O
re-establishing	O
some	O
of	O
the	O
correspondences	O
between	O
the	O
actual	O
flow	O
of	O
control	O
and	O
the	O
text	O
of	O
the	O
program	O
,	O
by	O
means	O
of	O
synchronization	O
.	O
</s>
<s>
Software	B-Library
libraries	I-Library
can	O
provide	O
certain	O
thread-safety	B-Operating_System
guarantees	O
.	O
</s>
<s>
For	O
example	O
,	O
concurrent	O
reads	O
might	O
be	O
guaranteed	O
to	O
be	O
thread-safe	B-Operating_System
,	O
but	O
concurrent	O
writes	O
might	O
not	O
be	O
.	O
</s>
<s>
Whether	O
a	O
program	O
using	O
such	O
a	O
library	O
is	O
thread-safe	B-Operating_System
depends	O
on	O
whether	O
it	O
uses	O
the	O
library	O
in	O
a	O
manner	O
consistent	O
with	O
those	O
guarantees	O
.	O
</s>
<s>
Different	O
vendors	O
use	O
slightly	O
different	O
terminology	O
for	O
thread-safety	B-Operating_System
:	O
</s>
<s>
Thread	B-Operating_System
safe	I-Operating_System
:	O
Implementation	O
is	O
guaranteed	O
to	O
be	O
free	O
of	O
race	O
conditions	O
when	O
accessed	O
by	O
multiple	O
threads	B-Operating_System
simultaneously	O
.	O
</s>
<s>
Conditionally	O
safe	O
:	O
Different	O
threads	B-Operating_System
can	O
access	O
different	O
objects	O
simultaneously	O
,	O
and	O
access	O
to	O
shared	O
data	O
is	O
protected	O
from	O
race	O
conditions	O
.	O
</s>
<s>
Not	O
thread	B-Operating_System
safe	I-Operating_System
:	O
Data	O
structures	O
should	O
not	O
be	O
accessed	O
simultaneously	O
by	O
different	O
threads	B-Operating_System
.	O
</s>
<s>
Thread	B-Operating_System
safety	I-Operating_System
guarantees	O
usually	O
also	O
include	O
design	O
steps	O
to	O
prevent	O
or	O
limit	O
the	O
risk	O
of	O
different	O
forms	O
of	O
deadlocks	B-Operating_System
,	O
as	O
well	O
as	O
optimizations	O
to	O
maximize	O
concurrent	O
performance	O
.	O
</s>
<s>
However	O
,	O
deadlock-free	O
guarantees	O
cannot	O
always	O
be	O
given	O
,	O
since	O
deadlocks	B-Operating_System
can	O
be	O
caused	O
by	O
callbacks	O
and	O
violation	O
of	O
architectural	O
layering	O
independent	O
of	O
the	O
library	O
itself	O
.	O
</s>
<s>
Below	O
we	O
discuss	O
two	O
classes	O
of	O
approaches	O
for	O
avoiding	O
race	O
conditions	O
to	O
achieve	O
thread-safety	B-Operating_System
.	O
</s>
<s>
The	O
first	O
class	O
of	O
approaches	O
focuses	O
on	O
avoiding	O
shared	O
state	B-Application
and	O
includes	O
:	O
</s>
<s>
Re-entrancy	B-Operating_System
Writing	O
code	O
in	O
such	O
a	O
way	O
that	O
it	O
can	O
be	O
partially	O
executed	O
by	O
a	O
thread	B-Operating_System
,	O
executed	O
by	O
the	O
same	O
thread	B-Operating_System
,	O
or	O
simultaneously	O
executed	O
by	O
another	O
thread	B-Operating_System
and	O
still	O
correctly	O
complete	O
the	O
original	O
execution	O
.	O
</s>
<s>
This	O
requires	O
the	O
saving	O
of	O
state	B-Application
information	O
in	O
variables	O
local	O
to	O
each	O
execution	O
,	O
usually	O
on	O
a	O
stack	O
,	O
instead	O
of	O
in	O
static	B-General_Concept
or	O
global	O
variables	O
or	O
other	O
non-local	O
state	B-Application
.	O
</s>
<s>
All	O
non-local	O
states	O
must	O
be	O
accessed	O
through	O
atomic	B-General_Concept
operations	I-General_Concept
and	O
the	O
data-structures	O
must	O
also	O
be	O
reentrant	B-Operating_System
.	O
</s>
<s>
Thread-local	O
storage	O
Variables	O
are	O
localized	O
so	O
that	O
each	O
thread	B-Operating_System
has	O
its	O
own	O
private	O
copy	O
.	O
</s>
<s>
These	O
variables	O
retain	O
their	O
values	O
across	O
subroutine	O
and	O
other	O
code	O
boundaries	O
and	O
are	O
thread-safe	B-Operating_System
since	O
they	O
are	O
local	O
to	O
each	O
thread	B-Operating_System
,	O
even	O
though	O
the	O
code	O
which	O
accesses	O
them	O
might	O
be	O
executed	O
simultaneously	O
by	O
another	O
thread	B-Operating_System
.	O
</s>
<s>
Immutable	B-Application
objects	I-Application
The	O
state	B-Application
of	O
an	O
object	O
cannot	O
be	O
changed	O
after	O
construction	O
.	O
</s>
<s>
This	O
implies	O
both	O
that	O
only	O
read-only	O
data	O
is	O
shared	O
and	O
that	O
inherent	O
thread	B-Operating_System
safety	I-Operating_System
is	O
attained	O
.	O
</s>
<s>
Mutable	B-Application
(	O
non-const	O
)	O
operations	O
can	O
then	O
be	O
implemented	O
in	O
such	O
a	O
way	O
that	O
they	O
create	O
new	O
objects	O
instead	O
of	O
modifying	O
existing	O
ones	O
.	O
</s>
<s>
This	O
approach	O
is	O
characteristic	O
of	O
functional	B-Language
programming	I-Language
and	O
is	O
also	O
used	O
by	O
the	O
string	O
implementations	O
in	O
Java	B-Language
,	O
C#	O
,	O
and	O
Python	O
.	O
</s>
<s>
(	O
See	O
Immutable	B-Application
object	I-Application
.	O
)	O
</s>
<s>
The	O
second	O
class	O
of	O
approaches	O
are	O
synchronization-related	O
,	O
and	O
are	O
used	O
in	O
situations	O
where	O
shared	O
state	B-Application
cannot	O
be	O
avoided	O
:	O
</s>
<s>
Mutual	B-Operating_System
exclusion	I-Operating_System
Access	O
to	O
shared	O
data	O
is	O
serialized	O
using	O
mechanisms	O
that	O
ensure	O
only	O
one	O
thread	B-Operating_System
reads	O
or	O
writes	O
to	O
the	O
shared	O
data	O
at	O
any	O
time	O
.	O
</s>
<s>
Incorporation	O
of	O
mutual	B-Operating_System
exclusion	I-Operating_System
needs	O
to	O
be	O
well	O
thought	O
out	O
,	O
since	O
improper	O
usage	O
can	O
lead	O
to	O
side-effects	O
like	O
deadlocks	B-Operating_System
,	O
livelocks	B-Application
,	O
and	O
resource	B-Operating_System
starvation	I-Operating_System
.	O
</s>
<s>
Atomic	B-General_Concept
operations	I-General_Concept
Shared	O
data	O
is	O
accessed	O
by	O
using	O
atomic	B-General_Concept
operations	I-General_Concept
which	O
cannot	O
be	O
interrupted	O
by	O
other	O
threads	B-Operating_System
.	O
</s>
<s>
This	O
usually	O
requires	O
using	O
special	O
machine	B-Language
language	I-Language
instructions	O
,	O
which	O
might	O
be	O
available	O
in	O
a	O
runtime	B-Library
library	I-Library
.	O
</s>
<s>
Since	O
the	O
operations	O
are	O
atomic	O
,	O
the	O
shared	O
data	O
is	O
always	O
kept	O
in	O
a	O
valid	O
state	B-Application
,	O
no	O
matter	O
how	O
other	O
threads	B-Operating_System
access	O
it	O
.	O
</s>
<s>
Atomic	B-General_Concept
operations	I-General_Concept
form	O
the	O
basis	O
of	O
many	O
thread	B-Operating_System
locking	B-Operating_System
mechanisms	O
,	O
and	O
are	O
used	O
to	O
implement	O
mutual	B-Operating_System
exclusion	I-Operating_System
primitives	O
.	O
</s>
<s>
In	O
the	O
following	O
piece	O
of	O
Java	B-Language
code	I-Language
,	O
the	O
Java	B-Language
keyword	O
synchronized	O
makes	O
the	O
method	O
thread-safe	B-Operating_System
:	O
</s>
<s>
In	O
the	O
C	B-Language
programming	I-Language
language	I-Language
,	O
each	O
thread	B-Operating_System
has	O
its	O
own	O
stack	O
.	O
</s>
<s>
However	O
,	O
a	O
static	B-General_Concept
variable	I-General_Concept
is	O
not	O
kept	O
on	O
the	O
stack	O
;	O
all	O
threads	B-Operating_System
share	O
simultaneous	O
access	O
to	O
it	O
.	O
</s>
<s>
If	O
multiple	O
threads	B-Operating_System
overlap	O
while	O
running	O
the	O
same	O
function	O
,	O
it	O
is	O
possible	O
that	O
a	O
static	B-General_Concept
variable	I-General_Concept
might	O
be	O
changed	O
by	O
one	O
thread	B-Operating_System
while	O
another	O
is	O
midway	O
through	O
checking	O
it	O
.	O
</s>
<s>
This	O
difficult-to-diagnose	O
logic	B-General_Concept
error	I-General_Concept
,	O
which	O
may	O
compile	O
and	O
run	O
properly	O
most	O
of	O
the	O
time	O
,	O
is	O
called	O
a	O
race	O
condition	O
.	O
</s>
<s>
One	O
common	O
way	O
to	O
avoid	O
this	O
is	O
to	O
use	O
another	O
shared	O
variable	O
as	O
a	O
"	B-Operating_System
lock	I-Operating_System
"	I-Operating_System
or	I-Operating_System
"	I-Operating_System
mutex	I-Operating_System
"	I-Operating_System
(	O
from	O
mutual	B-Operating_System
exclusion	I-Operating_System
)	O
.	O
</s>
<s>
In	O
the	O
following	O
piece	O
of	O
C	B-Language
code	O
,	O
the	O
function	O
is	O
thread-safe	B-Operating_System
,	O
but	O
not	O
reentrant	B-Operating_System
:	O
</s>
<s>
In	O
the	O
above	O
,	O
increment_counter	O
can	O
be	O
called	O
by	O
different	O
threads	B-Operating_System
without	O
any	O
problem	O
since	O
a	O
mutex	B-Operating_System
is	O
used	O
to	O
synchronize	O
all	O
access	O
to	O
the	O
shared	O
counter	O
variable	O
.	O
</s>
<s>
But	O
if	O
the	O
function	O
is	O
used	O
in	O
a	O
reentrant	B-Operating_System
interrupt	O
handler	O
and	O
a	O
second	O
interrupt	O
arises	O
while	O
the	O
mutex	B-Operating_System
is	O
locked	O
,	O
the	O
second	O
routine	O
will	O
hang	O
forever	O
.	O
</s>
<s>
The	O
same	O
function	O
can	O
be	O
implemented	O
to	O
be	O
both	O
thread-safe	B-Operating_System
and	O
reentrant	B-Operating_System
using	O
the	O
lock-free	O
atomics	B-General_Concept
in	O
C++11	B-Language
:	O
</s>
