<s>
In	O
software	B-General_Concept
engineering	I-General_Concept
,	O
double-checked	B-Operating_System
locking	I-Operating_System
(	O
also	O
known	O
as	O
"	O
double-checked	B-Operating_System
locking	I-Operating_System
optimization	I-Operating_System
"	O
)	O
is	O
a	O
software	O
design	O
pattern	O
used	O
to	O
reduce	O
the	O
overhead	O
of	O
acquiring	O
a	O
lock	B-Operating_System
by	O
testing	O
the	O
locking	B-Operating_System
criterion	O
(	O
the	O
"	O
lock	B-Operating_System
hint	O
"	O
)	O
before	O
acquiring	O
the	O
lock	B-Operating_System
.	O
</s>
<s>
Locking	B-Operating_System
occurs	O
only	O
if	O
the	O
locking	B-Operating_System
criterion	O
check	O
indicates	O
that	O
locking	B-Operating_System
is	O
required	O
.	O
</s>
<s>
It	O
is	O
typically	O
used	O
to	O
reduce	O
locking	B-Operating_System
overhead	O
when	O
implementing	O
"	O
lazy	O
initialization	O
"	O
in	O
a	O
multi-threaded	O
environment	O
,	O
especially	O
as	O
part	O
of	O
the	O
Singleton	O
pattern	O
.	O
</s>
<s>
For	O
the	O
singleton	O
pattern	O
,	O
double-checked	B-Operating_System
locking	I-Operating_System
is	O
not	O
needed	O
:	O
</s>
<s>
C++11	O
and	O
beyond	O
also	O
provide	O
a	O
built-in	O
double-checked	B-Operating_System
locking	I-Operating_System
pattern	O
in	O
the	O
form	O
of	O
std::once_flag	O
and	O
std::call_once	O
:	O
</s>
<s>
Consider	O
,	O
for	O
example	O
,	O
this	O
code	O
segment	O
in	O
the	O
Java	B-Language
programming	I-Language
language	I-Language
as	O
given	O
by	O
(	O
as	O
well	O
as	O
all	O
other	O
Java	B-Language
code	I-Language
segments	O
)	O
:	O
</s>
<s>
A	O
lock	B-Operating_System
must	O
be	O
obtained	O
in	O
case	O
two	O
threads	O
call	O
getHelper( )	O
simultaneously	O
.	O
</s>
<s>
The	O
lock	B-Operating_System
is	O
obtained	O
by	O
expensive	O
synchronizing	O
,	O
as	O
is	O
shown	O
in	O
the	O
following	O
example	O
.	O
</s>
<s>
Since	O
synchronizing	O
a	O
method	O
could	O
in	O
some	O
extreme	O
cases	O
decrease	O
performance	O
by	O
a	O
factor	O
of	O
100	O
or	O
higher	O
,	O
the	O
overhead	O
of	O
acquiring	O
and	O
releasing	O
a	O
lock	B-Operating_System
every	O
time	O
this	O
method	O
is	O
called	O
seems	O
unnecessary	O
:	O
once	O
the	O
initialization	O
has	O
been	O
completed	O
,	O
acquiring	O
and	O
releasing	O
the	O
locks	O
would	O
appear	O
unnecessary	O
.	O
</s>
<s>
Check	O
that	O
the	O
variable	O
is	O
initialized	O
(	O
without	O
obtaining	O
the	O
lock	B-Operating_System
)	O
.	O
</s>
<s>
Obtain	O
the	O
lock	B-Operating_System
.	O
</s>
<s>
Double-check	O
whether	O
the	O
variable	O
has	O
already	O
been	O
initialized	O
:	O
if	O
another	O
thread	O
acquired	O
the	O
lock	B-Operating_System
first	O
,	O
it	O
may	O
have	O
already	O
done	O
the	O
initialization	O
.	O
</s>
<s>
Thread	O
A	O
notices	O
that	O
the	O
value	O
is	O
not	O
initialized	O
,	O
so	O
it	O
obtains	O
the	O
lock	B-Operating_System
and	O
begins	O
to	O
initialize	O
the	O
value	O
.	O
</s>
<s>
Due	O
to	O
the	O
semantics	O
of	O
some	O
programming	O
languages	O
,	O
the	O
code	O
generated	O
by	O
the	O
compiler	B-Language
is	O
allowed	O
to	O
update	O
the	O
shared	O
variable	O
to	O
point	O
to	O
a	O
partially	O
constructed	O
object	O
before	O
A	O
has	O
finished	O
performing	O
the	O
initialization	O
.	O
</s>
<s>
For	O
example	O
,	O
in	O
Java	B-Language
if	O
a	O
call	O
to	O
a	O
constructor	O
has	O
been	O
inlined	O
then	O
the	O
shared	O
variable	O
may	O
immediately	O
be	O
updated	O
once	O
the	O
storage	O
has	O
been	O
allocated	O
but	O
before	O
the	O
inlined	O
constructor	O
initializes	O
the	O
object	O
.	O
</s>
<s>
Because	O
thread	O
B	O
believes	O
the	O
value	O
is	O
already	O
initialized	O
,	O
it	O
does	O
not	O
acquire	O
the	O
lock	B-Operating_System
.	O
</s>
<s>
If	O
B	O
uses	O
the	O
object	O
before	O
all	O
of	O
the	O
initialization	O
done	O
by	O
A	O
is	O
seen	O
by	O
B	O
(	O
either	O
because	O
A	O
has	O
not	O
finished	O
initializing	O
it	O
or	O
because	O
some	O
of	O
the	O
initialized	O
values	O
in	O
the	O
object	O
have	O
not	O
yet	O
percolated	O
to	O
the	O
memory	O
B	O
uses	O
(	O
cache	B-General_Concept
coherence	I-General_Concept
)	O
)	O
,	O
the	O
program	O
will	O
likely	O
crash	O
.	O
</s>
<s>
One	O
of	O
the	O
dangers	O
of	O
using	O
double-checked	B-Operating_System
locking	I-Operating_System
in	O
J2SE	B-Language
1.4	I-Language
(	O
and	O
earlier	O
versions	O
)	O
is	O
that	O
it	O
will	O
often	O
appear	O
to	O
work	O
:	O
it	O
is	O
not	O
easy	O
to	O
distinguish	O
between	O
a	O
correct	O
implementation	O
of	O
the	O
technique	O
and	O
one	O
that	O
has	O
subtle	O
problems	O
.	O
</s>
<s>
Depending	O
on	O
the	O
compiler	B-Language
,	O
the	O
interleaving	O
of	O
threads	O
by	O
the	O
scheduler	O
and	O
the	O
nature	O
of	O
other	O
concurrent	B-Operating_System
system	I-Operating_System
activity	I-Operating_System
,	O
failures	O
resulting	O
from	O
an	O
incorrect	O
implementation	O
of	O
double-checked	B-Operating_System
locking	I-Operating_System
may	O
only	O
occur	O
intermittently	O
.	O
</s>
<s>
As	O
of	O
J2SE	B-Language
5.0	I-Language
,	O
this	O
problem	O
has	O
been	O
fixed	O
.	O
</s>
<s>
The	O
volatile	B-Operating_System
keyword	O
now	O
ensures	O
that	O
multiple	O
threads	O
handle	O
the	O
singleton	O
instance	O
correctly	O
.	O
</s>
<s>
The	O
effect	O
of	O
this	O
is	O
that	O
in	O
cases	O
where	O
is	O
already	O
initialized	O
(	O
i.e.	O
,	O
most	O
of	O
the	O
time	O
)	O
,	O
the	O
volatile	B-Operating_System
field	O
is	O
only	O
accessed	O
once	O
(	O
due	O
to	O
""	O
instead	O
of	O
""	O
)	O
,	O
which	O
can	O
improve	O
the	O
method	O
's	O
overall	O
performance	O
by	O
as	O
much	O
as	O
40	O
percent	O
.	O
</s>
<s>
Java	B-Language
9	O
introduced	O
the	O
class	O
,	O
which	O
allows	O
use	O
of	O
relaxed	O
atomics	O
to	O
access	O
fields	O
,	O
giving	O
somewhat	O
faster	O
reads	O
on	O
machines	O
with	O
weak	O
memory	O
models	O
,	O
at	O
the	O
cost	O
of	O
more	O
difficult	O
mechanics	O
and	O
loss	O
of	O
sequential	O
consistency	O
(	O
field	O
accesses	O
no	O
longer	O
participate	O
in	O
the	O
synchronization	O
order	O
,	O
the	O
global	O
order	O
of	O
accesses	O
to	O
volatile	B-Operating_System
fields	O
)	O
.	O
</s>
<s>
Semantics	O
of	O
field	O
in	O
Java	B-Language
5	O
can	O
be	O
employed	O
to	O
safely	O
publish	O
the	O
helper	O
object	O
without	O
using	O
:	O
</s>
<s>
The	O
local	O
variable	O
is	O
required	O
for	O
correctness	O
:	O
simply	O
using	O
for	O
both	O
null	O
checks	O
and	O
the	O
return	O
statement	O
could	O
fail	O
due	O
to	O
read	O
reordering	O
allowed	O
under	O
the	O
Java	B-Language
Memory	O
Model	O
.	O
</s>
<s>
Double-checked	B-Operating_System
locking	I-Operating_System
can	O
be	O
implemented	O
efficiently	O
in	O
.NET	O
.	O
</s>
<s>
A	O
common	O
usage	O
pattern	O
is	O
to	O
add	O
double-checked	B-Operating_System
locking	I-Operating_System
to	O
Singleton	O
implementations	O
:	O
</s>
<s>
In	O
this	O
example	O
,	O
the	O
"	O
lock	B-Operating_System
hint	O
"	O
is	O
the	O
_mySingleton	O
object	O
which	O
is	O
no	O
longer	O
null	O
when	O
fully	O
constructed	O
and	O
ready	O
for	O
use	O
.	O
</s>
