<s>
In	O
computing	O
,	O
a	O
memory	B-General_Concept
barrier	I-General_Concept
,	O
also	O
known	O
as	O
a	O
membar	B-General_Concept
,	O
memory	B-General_Concept
fence	I-General_Concept
or	O
fence	B-General_Concept
instruction	I-General_Concept
,	O
is	O
a	O
type	O
of	O
barrier	B-Operating_System
instruction	O
that	O
causes	O
a	O
central	B-General_Concept
processing	I-General_Concept
unit	I-General_Concept
(	O
CPU	O
)	O
or	O
compiler	B-Language
to	O
enforce	O
an	O
ordering	B-General_Concept
constraint	O
on	O
memory	B-Architecture
operations	O
issued	O
before	O
and	O
after	O
the	O
barrier	B-Operating_System
instruction	O
.	O
</s>
<s>
This	O
typically	O
means	O
that	O
operations	O
issued	O
prior	O
to	O
the	O
barrier	B-Operating_System
are	O
guaranteed	O
to	O
be	O
performed	O
before	O
operations	O
issued	O
after	O
the	O
barrier	B-Operating_System
.	O
</s>
<s>
Memory	B-General_Concept
barriers	I-General_Concept
are	O
necessary	O
because	O
most	O
modern	O
CPUs	O
employ	O
performance	O
optimizations	O
that	O
can	O
result	O
in	O
out-of-order	B-General_Concept
execution	I-General_Concept
.	O
</s>
<s>
This	O
reordering	O
of	O
memory	B-Architecture
operations	O
(	O
loads	O
and	O
stores	O
)	O
normally	O
goes	O
unnoticed	O
within	O
a	O
single	B-Operating_System
thread	I-Operating_System
of	O
execution	O
,	O
but	O
can	O
cause	O
unpredictable	O
behavior	O
in	O
concurrent	B-Architecture
programs	I-Architecture
and	O
device	B-Application
drivers	I-Application
unless	O
carefully	O
controlled	O
.	O
</s>
<s>
The	O
exact	O
nature	O
of	O
an	O
ordering	B-General_Concept
constraint	O
is	O
hardware	B-Architecture
dependent	O
and	O
defined	O
by	O
the	O
architecture	O
's	O
memory	B-General_Concept
ordering	I-General_Concept
model	I-General_Concept
.	O
</s>
<s>
Some	O
architectures	O
provide	O
multiple	O
barriers	O
for	O
enforcing	O
different	O
ordering	B-General_Concept
constraints	O
.	O
</s>
<s>
Memory	B-General_Concept
barriers	I-General_Concept
are	O
typically	O
used	O
when	O
implementing	O
low-level	O
machine	B-Language
code	I-Language
that	O
operates	O
on	O
memory	B-Architecture
shared	O
by	O
multiple	O
devices	O
.	O
</s>
<s>
Such	O
code	O
includes	O
synchronization	O
primitives	O
and	O
lock-free	B-Operating_System
data	I-Operating_System
structures	I-Operating_System
on	O
multiprocessor	B-Operating_System
systems	O
,	O
and	O
device	B-Application
drivers	I-Application
that	O
communicate	O
with	O
computer	B-Architecture
hardware	I-Architecture
.	O
</s>
<s>
When	O
a	O
program	O
runs	O
on	O
a	O
single-CPU	O
machine	O
,	O
the	O
hardware	B-Architecture
performs	O
the	O
necessary	O
bookkeeping	O
to	O
ensure	O
that	O
the	O
program	O
executes	O
as	O
if	O
all	O
memory	B-Architecture
operations	O
were	O
performed	O
in	O
the	O
order	O
specified	O
by	O
the	O
programmer	O
(	O
program	O
order	O
)	O
,	O
so	O
memory	B-General_Concept
barriers	I-General_Concept
are	O
not	O
necessary	O
.	O
</s>
<s>
However	O
,	O
when	O
the	O
memory	B-Architecture
is	O
shared	O
with	O
multiple	O
devices	O
,	O
such	O
as	O
other	O
CPUs	O
in	O
a	O
multiprocessor	B-Operating_System
system	O
,	O
or	O
memory-mapped	B-Architecture
peripherals	I-Architecture
,	O
out-of-order	O
access	O
may	O
affect	O
program	O
behavior	O
.	O
</s>
<s>
For	O
example	O
,	O
a	O
second	O
CPU	O
may	O
see	O
memory	B-Architecture
changes	O
made	O
by	O
the	O
first	O
CPU	O
in	O
a	O
sequence	O
that	O
differs	O
from	O
program	O
order	O
.	O
</s>
<s>
A	O
program	O
is	O
run	O
via	O
a	O
process	O
which	O
can	O
be	O
multi-threaded	B-Operating_System
(	O
i.e.	O
</s>
<s>
a	O
software	B-Operating_System
thread	I-Operating_System
such	O
as	O
pthreads	B-Operating_System
as	O
opposed	O
to	O
a	O
hardware	B-Architecture
thread	B-Operating_System
)	O
.	O
</s>
<s>
Different	O
processes	O
do	O
not	O
share	O
a	O
memory	B-Architecture
space	O
so	O
this	O
discussion	O
does	O
not	O
apply	O
to	O
two	O
programs	O
,	O
each	O
one	O
running	O
in	O
a	O
different	O
process	O
(	O
hence	O
a	O
different	O
memory	B-Architecture
space	O
)	O
.	O
</s>
<s>
It	O
applies	O
to	O
two	O
or	O
more	O
(	O
software	O
)	O
threads	B-Operating_System
running	O
in	O
a	O
single	O
process	O
(	O
i.e.	O
</s>
<s>
a	O
single	O
memory	B-Architecture
space	O
where	O
multiple	O
software	B-Operating_System
threads	I-Operating_System
share	O
a	O
single	O
memory	B-Architecture
space	O
)	O
.	O
</s>
<s>
Multiple	O
software	B-Operating_System
threads	I-Operating_System
,	O
within	O
a	O
single	O
process	O
,	O
may	O
run	O
concurrently	B-Operating_System
on	O
a	O
Multi-core	B-Architecture
processor	I-Architecture
.	O
</s>
<s>
The	O
following	O
multi-threaded	B-Operating_System
program	O
,	O
running	O
on	O
a	O
multi-core	B-Architecture
processor	I-Architecture
gives	O
an	O
example	O
of	O
how	O
such	O
out-of-order	B-General_Concept
execution	I-General_Concept
can	O
affect	O
program	O
behavior	O
:	O
</s>
<s>
Initially	O
,	O
memory	B-Architecture
locations	O
x	O
and	O
f	O
both	O
hold	O
the	O
value	O
0	O
.	O
</s>
<s>
The	O
software	B-Operating_System
thread	I-Operating_System
running	O
on	O
processor	O
#1	O
loops	O
while	O
the	O
value	O
of	O
f	O
is	O
zero	O
,	O
then	O
it	O
prints	O
the	O
value	O
of	O
x	O
.	O
</s>
<s>
The	O
software	B-Operating_System
thread	I-Operating_System
running	O
on	O
processor	O
#2	O
stores	O
the	O
value	O
42	O
into	O
x	O
and	O
then	O
stores	O
the	O
value	O
1	O
into	O
f	O
.	O
Pseudo-code	O
for	O
the	O
two	O
program	O
fragments	O
is	O
shown	O
below	O
.	O
</s>
<s>
Thread	B-Operating_System
#1	O
Core	O
#1	O
:	O
</s>
<s>
Thread	B-Operating_System
#2	O
Core	O
#2	O
:	O
</s>
<s>
One	O
might	O
expect	O
the	O
print	O
statement	O
to	O
always	O
print	O
the	O
number	O
"	O
42	O
"	O
;	O
however	O
,	O
if	O
thread	B-Operating_System
#2	O
'	O
s	O
store	O
operations	O
are	O
executed	O
out-of-order	O
,	O
it	O
is	O
possible	O
for	O
f	O
to	O
be	O
updated	O
x	O
,	O
and	O
the	O
print	O
statement	O
might	O
therefore	O
print	O
"	O
0	O
"	O
.	O
</s>
<s>
Similarly	O
,	O
thread	B-Operating_System
#1	O
'	O
s	O
load	O
operations	O
may	O
be	O
executed	O
out-of-order	O
and	O
it	O
is	O
possible	O
for	O
x	O
to	O
be	O
read	O
f	O
is	O
checked	O
,	O
and	O
again	O
the	O
print	O
statement	O
might	O
therefore	O
print	O
an	O
unexpected	O
value	O
.	O
</s>
<s>
A	O
memory	B-General_Concept
barrier	I-General_Concept
must	O
be	O
inserted	O
before	O
thread	B-Operating_System
#2	O
'	O
s	O
assignment	O
to	O
f	O
to	O
ensure	O
that	O
the	O
new	O
value	O
of	O
x	O
is	O
visible	O
to	O
other	O
processors	O
at	O
or	O
prior	O
to	O
the	O
change	O
in	O
the	O
value	O
of	O
f	O
.	O
Another	O
important	O
point	O
is	O
a	O
memory	B-General_Concept
barrier	I-General_Concept
must	O
also	O
be	O
inserted	O
before	O
thread	B-Operating_System
#1	O
'	O
s	O
access	O
to	O
x	O
to	O
ensure	O
the	O
value	O
of	O
x	O
is	O
not	O
read	O
prior	O
to	O
seeing	O
the	O
change	O
in	O
the	O
value	O
of	O
f	O
.	O
</s>
<s>
If	O
the	O
processor	O
's	O
store	O
operations	O
are	O
executed	O
out-of-order	O
,	O
the	O
hardware	B-Architecture
module	O
may	O
be	O
triggered	O
before	O
data	O
is	O
ready	O
in	O
memory	B-Architecture
.	O
</s>
<s>
For	O
another	O
illustrative	O
example	O
(	O
a	O
non-trivial	O
one	O
that	O
arises	O
in	O
actual	O
practice	O
)	O
,	O
see	O
double-checked	B-Operating_System
locking	I-Operating_System
.	O
</s>
<s>
Multithreaded	O
programs	O
usually	O
use	O
synchronization	O
primitives	O
provided	O
by	O
a	O
high-level	O
programming	O
environment	O
—	O
such	O
as	O
Java	B-Language
or	O
.NET	B-Application
—	O
or	O
an	O
application	B-Application
programming	I-Application
interface	I-Application
(	O
API	B-Application
)	O
such	O
as	O
POSIX	B-Operating_System
Threads	I-Operating_System
or	O
Windows	B-Library
API	I-Library
.	O
</s>
<s>
Synchronization	O
primitives	O
such	O
as	O
mutexes	B-Operating_System
and	O
semaphores	B-Operating_System
are	O
provided	O
to	O
synchronize	O
access	O
to	O
resources	O
from	O
parallel	O
threads	B-Operating_System
of	O
execution	O
.	O
</s>
<s>
These	O
primitives	O
are	O
usually	O
implemented	O
with	O
the	O
memory	B-General_Concept
barriers	I-General_Concept
required	O
to	O
provide	O
the	O
expected	O
memory	B-Architecture
visibility	O
semantics	O
.	O
</s>
<s>
In	O
such	O
environments	O
explicit	O
use	O
of	O
memory	B-General_Concept
barriers	I-General_Concept
is	O
not	O
generally	O
necessary	O
.	O
</s>
<s>
Memory	B-General_Concept
barrier	I-General_Concept
instructions	O
address	O
reordering	O
effects	O
only	O
at	O
the	O
hardware	B-Architecture
level	O
.	O
</s>
<s>
Compilers	B-Language
may	O
also	O
reorder	O
instructions	O
as	O
part	O
of	O
the	O
program	O
optimization	O
process	O
.	O
</s>
<s>
Although	O
the	O
effects	O
on	O
parallel	O
program	O
behavior	O
can	O
be	O
similar	O
in	O
both	O
cases	O
,	O
in	O
general	O
it	O
is	O
necessary	O
to	O
take	O
separate	O
measures	O
to	O
inhibit	O
compiler	B-Language
reordering	O
optimizations	O
for	O
data	O
that	O
may	O
be	O
shared	O
by	O
multiple	O
threads	B-Operating_System
of	O
execution	O
.	O
</s>
<s>
In	O
C	B-Language
and	O
C++	B-Language
,	O
the	O
keyword	O
was	O
intended	O
to	O
allow	O
C	B-Language
and	O
C++	B-Language
programs	I-Language
to	O
directly	O
access	O
memory-mapped	B-Architecture
I/O	I-Architecture
.	O
</s>
<s>
Memory-mapped	B-Architecture
I/O	I-Architecture
generally	O
requires	O
that	O
the	O
reads	O
and	O
writes	O
specified	O
in	O
source	O
code	O
happen	O
in	O
the	O
exact	O
order	O
specified	O
with	O
no	O
omissions	O
.	O
</s>
<s>
Omissions	O
or	O
reorderings	O
of	O
reads	O
and	O
writes	O
by	O
the	O
compiler	B-Language
would	O
break	O
the	O
communication	O
between	O
the	O
program	O
and	O
the	O
device	O
accessed	O
by	O
memory-mapped	B-Architecture
I/O	I-Architecture
.	O
</s>
<s>
A	O
C	B-Language
or	O
C++	B-Language
compiler	B-Language
may	O
not	O
omit	O
reads	O
from	O
and	O
writes	O
to	O
volatile	O
memory	B-Architecture
locations	O
,	O
nor	O
may	O
it	O
reorder	O
read/writes	O
relative	O
to	O
other	O
such	O
actions	O
for	O
the	O
same	O
volatile	O
location	O
(	O
variable	O
)	O
.	O
</s>
<s>
Therefore	O
,	O
the	O
use	O
of	O
alone	O
is	O
not	O
sufficient	O
to	O
use	O
a	O
variable	O
for	O
inter-thread	O
communication	O
on	O
all	O
systems	O
and	O
processors	O
.	O
</s>
<s>
The	O
C	B-Language
and	O
C++	B-Language
standards	O
prior	O
to	O
C11	O
and	O
C++11	O
do	O
not	O
address	O
multiple	O
threads	B-Operating_System
(	O
or	O
multiple	O
processors	O
)	O
,	O
and	O
as	O
such	O
,	O
the	O
usefulness	O
of	O
depends	O
on	O
the	O
compiler	B-Language
and	O
hardware	B-Architecture
.	O
</s>
<s>
Although	O
guarantees	O
that	O
the	O
volatile	O
reads	O
and	O
volatile	O
writes	O
will	O
happen	O
in	O
the	O
exact	O
order	O
specified	O
in	O
the	O
source	O
code	O
,	O
the	O
compiler	B-Language
may	O
generate	O
code	O
(	O
or	O
the	O
CPU	O
may	O
re-order	O
execution	O
)	O
such	O
that	O
a	O
volatile	O
read	O
or	O
write	O
is	O
reordered	O
with	O
regard	O
to	O
non-volatile	O
reads	O
or	O
writes	O
,	O
thus	O
limiting	O
its	O
usefulness	O
as	O
an	O
inter-thread	O
flag	O
or	O
mutex	B-Operating_System
.	O
</s>
<s>
Preventing	O
such	O
is	O
compiler	B-Language
specific	O
,	O
but	O
some	O
compilers	B-Language
,	O
like	O
gcc	B-Application
,	O
will	O
not	O
reorder	O
operations	O
around	O
in-line	O
assembly	O
code	O
with	O
and	O
tags	O
,	O
like	O
in	O
:	O
(	O
See	O
more	O
examples	O
in	O
Memory	B-General_Concept
ordering	I-General_Concept
#Compile	O
-time	O
memory	B-General_Concept
ordering	I-General_Concept
)	O
.	O
</s>
