<s>
In	O
computer	B-General_Concept
programming	I-General_Concept
,	O
volatile	B-Operating_System
means	O
that	O
a	O
value	O
is	O
prone	O
to	O
change	O
over	O
time	O
,	O
outside	O
the	O
control	O
of	O
some	O
code	O
.	O
</s>
<s>
In	O
the	O
C	B-Language
,	O
C++	B-Language
,	O
C#	B-Application
,	O
and	O
Java	B-Language
programming	I-Language
languages	I-Language
,	O
the	O
volatile	B-Operating_System
keyword	O
indicates	O
that	O
a	O
value	O
may	O
change	O
between	O
different	O
accesses	O
,	O
even	O
if	O
it	O
does	O
not	O
appear	O
to	O
be	O
modified	O
.	O
</s>
<s>
This	O
keyword	O
prevents	O
an	O
optimizing	B-Application
compiler	I-Application
from	O
optimizing	O
away	O
subsequent	O
reads	O
or	O
writes	O
and	O
thus	O
incorrectly	O
reusing	O
a	O
stale	O
value	O
or	O
omitting	O
writes	O
.	O
</s>
<s>
Volatile	B-Operating_System
values	O
primarily	O
arise	O
in	O
hardware	O
access	O
(	O
memory-mapped	B-Architecture
I/O	I-Architecture
)	O
,	O
where	O
reading	O
from	O
or	O
writing	O
to	O
memory	O
is	O
used	O
to	O
communicate	O
with	O
peripheral	B-Device
devices	I-Device
,	O
and	O
in	O
threading	B-Operating_System
,	O
where	O
a	O
different	O
thread	B-Operating_System
may	O
have	O
modified	O
a	O
value	O
.	O
</s>
<s>
Despite	O
being	O
a	O
common	O
keyword	O
,	O
the	O
behavior	O
of	O
volatile	B-Operating_System
differs	O
significantly	O
between	O
programming	O
languages	O
,	O
and	O
is	O
easily	O
misunderstood	O
.	O
</s>
<s>
In	O
C	B-Language
and	O
C++	B-Language
,	O
it	O
is	O
a	O
type	B-Language
qualifier	I-Language
,	O
like	O
const	O
,	O
and	O
is	O
a	O
property	O
of	O
the	O
type	O
.	O
</s>
<s>
Furthermore	O
,	O
in	O
C	B-Language
and	O
C++	B-Language
it	O
does	O
not	O
work	O
in	O
most	O
threading	B-Operating_System
scenarios	O
,	O
and	O
that	O
use	O
is	O
discouraged	O
.	O
</s>
<s>
In	O
Java	B-Language
and	O
C#	B-Application
,	O
it	O
is	O
a	O
property	O
of	O
a	O
variable	O
and	O
indicates	O
that	O
the	O
object	O
to	O
which	O
the	O
variable	O
is	O
bound	O
may	O
mutate	O
,	O
and	O
is	O
specifically	O
intended	O
for	O
threading	B-Operating_System
.	O
</s>
<s>
In	O
the	O
D	B-Application
programming	I-Application
language	I-Application
,	O
there	O
is	O
a	O
separate	O
keyword	O
shared	O
for	O
the	O
threading	B-Operating_System
usage	O
,	O
but	O
no	O
volatile	B-Operating_System
keyword	O
exists	O
.	O
</s>
<s>
While	O
intended	O
by	O
both	O
C	B-Language
and	O
C++	B-Language
,	O
the	O
C	B-Language
standards	O
fail	O
to	O
express	O
that	O
the	O
volatile	B-Operating_System
semantics	O
refer	O
to	O
the	O
lvalue	O
,	O
not	O
the	O
referenced	O
object	O
.	O
</s>
<s>
Operations	O
on	O
volatile	B-Operating_System
variables	I-Operating_System
are	O
not	O
atomic	B-General_Concept
,	O
nor	O
do	O
they	O
establish	O
a	O
proper	O
happens-before	B-Operating_System
relationship	I-Operating_System
for	O
threading	B-Operating_System
.	O
</s>
<s>
This	O
is	O
specified	O
in	O
the	O
relevant	O
standards	O
(	O
C	B-Language
,	O
C++	B-Language
,	O
POSIX	O
,	O
WIN32	O
)	O
,	O
and	O
volatile	B-Operating_System
variables	I-Operating_System
are	O
not	O
threadsafe	O
in	O
the	O
vast	O
majority	O
of	O
current	O
implementations	O
.	O
</s>
<s>
Thus	O
,	O
the	O
usage	O
of	O
volatile	B-Operating_System
keyword	O
as	O
a	O
portable	O
synchronization	O
mechanism	O
is	O
discouraged	O
by	O
many	O
C/C	O
++	O
groups	O
.	O
</s>
<s>
It	O
then	O
starts	O
to	O
poll	B-General_Concept
that	O
value	O
repeatedly	O
until	O
it	O
changes	O
to	O
255	O
:	O
</s>
<s>
An	O
optimizing	B-Application
compiler	I-Application
will	O
notice	O
that	O
no	O
other	O
code	O
can	O
possibly	O
change	O
the	O
value	O
stored	O
in	O
foo	O
,	O
and	O
will	O
assume	O
that	O
it	O
will	O
remain	O
equal	O
to	O
0	O
at	O
all	O
times	O
.	O
</s>
<s>
The	O
compiler	O
will	O
therefore	O
replace	O
the	O
function	O
body	O
with	O
an	O
infinite	B-Algorithm
loop	I-Algorithm
similar	O
to	O
this	O
:	O
</s>
<s>
However	O
,	O
foo	O
might	O
represent	O
a	O
location	O
that	O
can	O
be	O
changed	O
by	O
other	O
elements	O
of	O
the	O
computer	O
system	O
at	O
any	O
time	O
,	O
such	O
as	O
a	O
hardware	B-General_Concept
register	I-General_Concept
of	O
a	O
device	O
connected	O
to	O
the	O
CPU	B-Device
.	O
</s>
<s>
The	O
above	O
code	O
would	O
never	O
detect	O
such	O
a	O
change	O
;	O
without	O
the	O
volatile	B-Operating_System
keyword	O
,	O
the	O
compiler	O
assumes	O
that	O
the	O
current	O
program	O
is	O
the	O
only	O
part	O
of	O
the	O
system	O
that	O
could	O
change	O
the	O
value	O
(	O
which	O
is	O
by	O
far	O
the	O
most	O
common	O
situation	O
)	O
.	O
</s>
<s>
To	O
prevent	O
the	O
compiler	O
from	O
optimizing	O
code	O
as	O
above	O
,	O
the	O
volatile	B-Operating_System
keyword	O
is	O
used	O
:	O
</s>
<s>
Generally	O
,	O
there	O
are	O
memory	B-General_Concept
barrier	I-General_Concept
operations	O
available	O
on	O
platforms	O
(	O
which	O
are	O
exposed	O
in	O
C++11	B-Language
)	O
that	O
should	O
be	O
preferred	O
instead	O
of	O
volatile	B-Operating_System
as	O
they	O
allow	O
the	O
compiler	O
to	O
perform	O
better	O
optimization	O
and	O
more	O
importantly	O
they	O
guarantee	O
correct	O
behaviour	O
in	O
multi-threaded	B-Operating_System
scenarios	O
;	O
neither	O
the	O
C	B-Language
specification	O
(	O
before	O
C11	O
)	O
nor	O
the	O
C++	B-Language
specification	O
(	O
before	O
C++11	B-Language
)	O
specifies	O
a	O
multi-threaded	B-Operating_System
memory	O
model	O
,	O
so	O
volatile	B-Operating_System
may	O
not	O
behave	O
deterministically	O
across	O
OSes/compilers/CPUs	O
.	O
</s>
<s>
The	O
following	O
C	B-Language
programs	I-Language
,	O
and	O
accompanying	O
assemblies	O
,	O
demonstrate	O
how	O
the	O
volatile	B-Operating_System
keyword	O
affects	O
the	O
compiler	O
's	O
output	O
.	O
</s>
<s>
The	O
compiler	O
in	O
this	O
case	O
was	O
GCC	B-Application
.	O
</s>
<s>
While	O
observing	O
the	O
assembly	O
code	O
,	O
it	O
is	O
clearly	O
visible	O
that	O
the	O
code	O
generated	O
with	O
volatile	B-Operating_System
objects	O
is	O
more	O
verbose	O
,	O
making	O
it	O
longer	O
so	O
the	O
nature	O
of	O
volatile	B-Operating_System
objects	O
can	O
be	O
fulfilled	O
.	O
</s>
<s>
The	O
volatile	B-Operating_System
keyword	O
prevents	O
the	O
compiler	O
from	O
performing	O
optimization	O
on	O
code	O
involving	O
volatile	B-Operating_System
objects	O
,	O
thus	O
ensuring	O
that	O
each	O
volatile	B-Operating_System
variable	I-Operating_System
assignment	O
and	O
read	O
has	O
a	O
corresponding	O
memory	O
access	O
.	O
</s>
<s>
Without	O
the	O
volatile	B-Operating_System
keyword	O
,	O
the	O
compiler	O
knows	O
a	O
variable	O
does	O
not	O
need	O
to	O
be	O
reread	O
from	O
memory	O
at	O
each	O
use	O
,	O
because	O
there	O
should	O
not	O
be	O
any	O
writes	O
to	O
its	O
memory	O
location	O
from	O
any	O
other	O
thread	B-Operating_System
or	O
process	O
.	O
</s>
<s>
int	O
a	O
=	O
10	O
,	O
b	O
=	O
100	O
,	O
c	B-Language
=	O
0	O
,	O
d	B-Application
=	O
0	O
;	O
</s>
<s>
printf( 	O
"	O
%d	O
"	O
,	O
a	O
+	O
b	O
)	O
;	O
</s>
<s>
c	B-Language
=	O
b	O
;	O
</s>
<s>
d	B-Application
=	O
b	O
;	O
</s>
<s>
printf( 	O
"	O
%d	O
"	O
,	O
c	B-Language
+	O
d	B-Application
)	O
;	O
</s>
<s>
volatile	B-Operating_System
int	O
a	O
=	O
10	O
,	O
b	O
=	O
100	O
,	O
c	B-Language
=	O
0	O
,	O
d	B-Application
=	O
0	O
;	O
</s>
<s>
printf( 	O
"	O
%d	O
"	O
,	O
a	O
+	O
b	O
)	O
;	O
</s>
<s>
c	B-Language
=	O
b	O
;	O
</s>
<s>
d	B-Application
=	O
b	O
;	O
</s>
<s>
printf( 	O
"	O
%d	O
"	O
,	O
c	B-Language
+	O
d	B-Application
)	O
;	O
</s>
<s>
According	O
to	O
the	O
C++11	B-Language
ISO	O
Standard	O
,	O
the	O
volatile	B-Operating_System
keyword	O
is	O
only	O
meant	O
for	O
use	O
for	O
hardware	O
access	O
;	O
do	O
not	O
use	O
it	O
for	O
inter-thread	O
communication	O
.	O
</s>
<s>
For	O
inter-thread	O
communication	O
,	O
the	O
standard	O
library	O
provides	O
std::atomicxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx1	O
templates	O
.	O
</s>
<s>
The	O
Java	B-Language
programming	I-Language
language	I-Language
also	O
has	O
the	O
volatile	B-Operating_System
keyword	O
,	O
but	O
it	O
is	O
used	O
for	O
a	O
somewhat	O
different	O
purpose	O
.	O
</s>
<s>
When	O
applied	O
to	O
a	O
field	O
,	O
the	O
Java	B-Language
qualifier	O
volatile	B-Operating_System
provides	O
the	O
following	O
guarantees	O
:	O
</s>
<s>
In	O
all	O
versions	O
of	O
Java	B-Language
,	O
there	O
is	O
a	O
global	O
ordering	O
on	O
reads	O
and	O
writes	O
of	O
all	O
volatile	B-Operating_System
variables	I-Operating_System
(	O
this	O
global	O
ordering	O
on	O
volatiles	B-Operating_System
is	O
a	O
partial	O
order	O
over	O
the	O
larger	O
synchronization	O
order	O
(	O
which	O
is	O
a	O
total	O
order	O
over	O
all	O
synchronization	O
actions	O
)	O
)	O
.	O
</s>
<s>
This	O
implies	O
that	O
every	O
thread	B-Operating_System
accessing	O
a	O
volatile	B-Operating_System
field	O
will	O
read	O
its	O
current	O
value	O
before	O
continuing	O
,	O
instead	O
of	O
(	O
potentially	O
)	O
using	O
a	O
cached	O
value	O
.	O
</s>
<s>
(	O
However	O
,	O
there	O
is	O
no	O
guarantee	O
about	O
the	O
relative	O
ordering	O
of	O
volatile	B-Operating_System
reads	O
and	O
writes	O
with	O
regular	O
reads	O
and	O
writes	O
,	O
meaning	O
that	O
it	O
's	O
generally	O
not	O
a	O
useful	O
threading	B-Operating_System
construct	O
.	O
)	O
</s>
<s>
In	O
Java	B-Language
5	O
or	O
later	O
,	O
volatile	B-Operating_System
reads	O
and	O
writes	O
establish	O
a	O
happens-before	B-Operating_System
relationship	I-Operating_System
,	O
much	O
like	O
acquiring	O
and	O
releasing	O
a	O
mutex	B-Operating_System
.	O
</s>
<s>
Using	O
volatile	B-Operating_System
may	O
be	O
faster	O
than	O
a	O
lock	B-Operating_System
,	O
but	O
it	O
will	O
not	O
work	O
in	O
some	O
situations	O
before	O
Java	B-Language
5	O
.	O
</s>
<s>
The	O
range	O
of	O
situations	O
in	O
which	O
volatile	B-Operating_System
is	O
effective	O
was	O
expanded	O
in	O
Java	B-Language
5	O
;	O
in	O
particular	O
,	O
double-checked	B-Operating_System
locking	I-Operating_System
now	O
works	O
correctly	O
.	O
</s>
<s>
In	O
C#	B-Application
,	O
volatile	B-Operating_System
ensures	O
that	O
code	O
accessing	O
the	O
field	O
is	O
not	O
subject	O
to	O
some	O
thread-unsafe	O
optimizations	O
that	O
may	O
be	O
performed	O
by	O
the	O
compiler	O
,	O
the	O
CLR	O
,	O
or	O
by	O
hardware	O
.	O
</s>
<s>
When	O
a	O
field	O
is	O
marked	O
volatile	B-Operating_System
,	O
the	O
compiler	O
is	O
instructed	O
to	O
generate	O
a	O
"	O
memory	B-General_Concept
barrier	I-General_Concept
"	O
or	O
"	O
fence	O
"	O
around	O
it	O
,	O
which	O
prevents	O
instruction	O
reordering	O
or	O
caching	O
tied	O
to	O
the	O
field	O
.	O
</s>
<s>
When	O
reading	O
a	O
volatile	B-Operating_System
field	O
,	O
the	O
compiler	O
generates	O
an	O
acquire-fence	O
,	O
which	O
prevents	O
other	O
reads	O
and	O
writes	O
to	O
the	O
field	O
,	O
including	O
those	O
in	O
other	O
threads	B-Operating_System
,	O
from	O
being	O
moved	O
before	O
the	O
fence	O
.	O
</s>
<s>
When	O
writing	O
to	O
a	O
volatile	B-Operating_System
field	O
,	O
the	O
compiler	O
generates	O
a	O
release-fence	O
;	O
this	O
fence	O
prevents	O
other	O
reads	O
and	O
writes	O
to	O
the	O
field	O
from	O
being	O
moved	O
after	O
the	O
fence	O
.	O
</s>
<s>
Only	O
the	O
following	O
types	O
can	O
be	O
marked	O
volatile	B-Operating_System
:	O
all	O
reference	O
types	O
,	O
Single	O
,	O
Boolean	O
,	O
Byte	O
,	O
SByte	O
,	O
Int16	O
,	O
UInt16	O
,	O
Int32	O
,	O
UInt32	O
,	O
Char	O
,	O
and	O
all	O
enumerated	O
types	O
with	O
an	O
underlying	O
type	O
of	O
Byte	O
,	O
SByte	O
,	O
Int16	O
,	O
UInt16	O
,	O
Int32	O
,	O
or	O
UInt32	O
.	O
</s>
<s>
(	O
This	O
excludes	O
value	O
structs	B-Language
,	O
as	O
well	O
as	O
the	O
primitive	O
types	O
Double	O
,	O
Int64	O
,	O
UInt64	O
and	O
Decimal	O
.	O
)	O
</s>
<s>
Using	O
the	O
volatile	B-Operating_System
keyword	O
does	O
not	O
support	O
fields	O
that	O
are	O
passed	O
by	O
reference	O
or	O
captured	B-Language
local	I-Language
variables	I-Language
;	O
in	O
these	O
cases	O
,	O
Thread.VolatileRead	O
and	O
Thread.VolatileWrite	O
must	O
be	O
used	O
instead	O
.	O
</s>
<s>
In	O
effect	O
,	O
these	O
methods	O
disable	O
some	O
optimizations	O
usually	O
performed	O
by	O
the	O
C#	B-Application
compiler	O
,	O
the	O
JIT	O
compiler	O
,	O
or	O
the	O
CPU	B-Device
itself	O
.	O
</s>
<s>
The	O
guarantees	O
provided	O
by	O
Thread.VolatileRead	O
and	O
Thread.VolatileWrite	O
are	O
a	O
superset	O
of	O
the	O
guarantees	O
provided	O
by	O
the	O
volatile	B-Operating_System
keyword	O
:	O
instead	O
of	O
generating	O
a	O
"	O
half	O
fence	O
"	O
(	O
ie	O
an	O
acquire-fence	O
only	O
prevents	O
instruction	O
reordering	O
and	O
caching	O
that	O
comes	O
before	O
it	O
)	O
,	O
VolatileRead	O
and	O
VolatileWrite	O
generate	O
a	O
"	O
full	O
fence	O
"	O
which	O
prevent	O
instruction	O
reordering	O
and	O
caching	O
of	O
that	O
field	O
in	O
both	O
directions	O
.	O
</s>
<s>
The	O
Thread.VolatileWrite	O
method	O
forces	O
the	O
value	O
in	O
the	O
field	O
to	O
be	O
written	O
to	O
at	O
the	O
point	O
of	O
the	O
call	O
.	O
</s>
<s>
The	O
Thread.VolatileRead	O
method	O
forces	O
the	O
value	O
in	O
the	O
field	O
to	O
be	O
read	O
from	O
at	O
the	O
point	O
of	O
the	O
call	O
.	O
</s>
<s>
The	O
Thread.VolatileRead	O
and	O
Thread.VolatileWrite	O
methods	O
generate	O
a	O
full	O
fence	O
by	O
calling	O
the	O
Thread.MemoryBarrier	O
method	O
,	O
which	O
constructs	O
a	O
memory	B-General_Concept
barrier	I-General_Concept
that	O
works	O
in	O
both	O
directions	O
.	O
</s>
<s>
In	O
addition	O
to	O
the	O
motivations	O
for	O
using	O
a	O
full	O
fence	O
given	O
above	O
,	O
one	O
potential	O
problem	O
with	O
the	O
volatile	B-Operating_System
keyword	O
that	O
is	O
solved	O
by	O
using	O
a	O
full	O
fence	O
generated	O
by	O
Thread.MemoryBarrier	O
is	O
as	O
follows	O
:	O
due	O
to	O
the	O
asymmetric	O
nature	O
of	O
half	O
fences	O
,	O
a	O
volatile	B-Operating_System
field	O
with	O
a	O
write	O
instruction	O
followed	O
by	O
a	O
read	O
instruction	O
may	O
still	O
have	O
the	O
execution	O
order	O
swapped	O
by	O
the	O
compiler	O
.	O
</s>
<s>
Because	O
full	O
fences	O
are	O
symmetric	O
,	O
this	O
is	O
not	O
a	O
problem	O
when	O
using	O
Thread.MemoryBarrier	O
.	O
</s>
<s>
VOLATILE	B-Operating_System
is	O
part	O
of	O
the	O
Fortran	O
2003	O
standard	O
,	O
although	O
earlier	O
version	O
supported	O
it	O
as	O
an	O
extension	O
.	O
</s>
<s>
Making	O
all	O
variables	O
volatile	B-Operating_System
in	O
a	O
function	O
is	O
also	O
useful	O
finding	O
aliasing	B-Application
related	O
bugs	O
.	O
</s>
<s>
By	O
always	O
"	O
drilling	O
down	O
"	O
to	O
memory	O
of	O
a	O
VOLATILE	B-Operating_System
,	O
the	O
Fortran	O
compiler	O
is	O
precluded	O
from	O
reordering	O
reads	O
or	O
writes	O
to	O
volatiles	B-Operating_System
.	O
</s>
<s>
This	O
makes	O
visible	O
to	O
other	O
threads	B-Operating_System
actions	O
done	O
in	O
this	O
thread	B-Operating_System
,	O
and	O
vice	O
versa	O
.	O
</s>
<s>
Use	O
of	O
VOLATILE	B-Operating_System
reduces	O
and	O
can	O
even	O
prevent	O
optimization	O
.	O
</s>
