<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.0.12-alpha" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: Optimizing Singletons With Double Checks</title>
	<link>http://www.codemaestro.com/reviews/10</link>
	<description>The Coding Experience</description>
	<pubDate>Fri, 05 Dec 2008 13:18:23 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.0.12-alpha</generator>

	<item>
		<title>by: Mahmoud Abdelkader</title>
		<link>http://www.codemaestro.com/reviews/10#comment-20740</link>
		<pubDate>Fri, 10 Oct 2008 17:02:03 +0000</pubDate>
		<guid>http://www.codemaestro.com/reviews/10#comment-20740</guid>
					<description>As someone mentioned earlier, this becomes problematic as you're running into the anti-pattern of double checked locking. This idiom has been tried again and again, only to realize that it does not work.

I know you can get around it by using 'volatile' in Java and possibly the same thing in C++.</description>
		<content:encoded><![CDATA[<p>As someone mentioned earlier, this becomes problematic as you&#8217;re running into the anti-pattern of double checked locking. This idiom has been tried again and again, only to realize that it does not work.</p>
<p>I know you can get around it by using &#8216;volatile&#8217; in Java and possibly the same thing in C++.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Bill Robertson</title>
		<link>http://www.codemaestro.com/reviews/10#comment-20707</link>
		<pubDate>Fri, 10 Oct 2008 00:01:24 +0000</pubDate>
		<guid>http://www.codemaestro.com/reviews/10#comment-20707</guid>
					<description>That is one of the things that led to the revisions in the Java memory model a few years ago.  The new C++ standard has the notion to address this sort of thing.

Here's what a quick google search turns up on the problem (in C++)

http://www.devarticles.com/c/a/Cplusplus/C-plus-in-Theory-Why-the-Double-Check-Lock-Pattern-Isnt-100-ThreadSafe/

http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html</description>
		<content:encoded><![CDATA[<p>That is one of the things that led to the revisions in the Java memory model a few years ago.  The new C++ standard has the notion to address this sort of thing.</p>
<p>Here&#8217;s what a quick google search turns up on the problem (in C++)</p>
<p><a href="http://www.devarticles.com/c/a/Cplusplus/C-plus-in-Theory-Why-the-Double-Check-Lock-Pattern-Isnt-100-ThreadSafe/" rel="nofollow">http://www.devarticles.com/c/a/Cplusplus/C-plus-in-Theory-Why-the-Double-Check-Lock-Pattern-Isnt-100-ThreadSafe/</a></p>
<p><a href="http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html" rel="nofollow">http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html</a>
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Al</title>
		<link>http://www.codemaestro.com/reviews/10#comment-20521</link>
		<pubDate>Mon, 06 Oct 2008 09:50:35 +0000</pubDate>
		<guid>http://www.codemaestro.com/reviews/10#comment-20521</guid>
					<description>Not positively sure about C++, but Java allows something really simpler:

public class MyClass
{
    private final static instance = new MyClass();

    public static MyClass getInstance()
    {
        return instance;
    }
}

This ensures the static field is initialized only once. An alternative, if startup code is more complex, is to used a static constructor :

public class MyClass
{
    private final static instance;

    static {
        // some global initialization stuff...
        instance = new MyClass();
    }

    public static MyClass getInstance()
    {
        return instance;
    }
}

This is not to start a C++/Java war ; I've made more Java than C++, and want to learn ;)</description>
		<content:encoded><![CDATA[<p>Not positively sure about C++, but Java allows something really simpler:</p>
<p>public class MyClass<br />
{<br />
    private final static instance = new MyClass();</p>
<p>    public static MyClass getInstance()<br />
    {<br />
        return instance;<br />
    }<br />
}</p>
<p>This ensures the static field is initialized only once. An alternative, if startup code is more complex, is to used a static constructor :</p>
<p>public class MyClass<br />
{<br />
    private final static instance;</p>
<p>    static {<br />
        // some global initialization stuff&#8230;<br />
        instance = new MyClass();<br />
    }</p>
<p>    public static MyClass getInstance()<br />
    {<br />
        return instance;<br />
    }<br />
}</p>
<p>This is not to start a C++/Java war ; I&#8217;ve made more Java than C++, and want to learn ;)
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Scottt</title>
		<link>http://www.codemaestro.com/reviews/10#comment-18945</link>
		<pubDate>Wed, 27 Aug 2008 04:37:20 +0000</pubDate>
		<guid>http://www.codemaestro.com/reviews/10#comment-18945</guid>
					<description>A simple alternative is to use a function such as pthread_once on POSIX or the InitOnce methods in Vista which will run a method a single time regardless of how many threads call it.

As EJ notes above, double checked locking will fail on some compilers even if volatile is used. You need to lock for all access to the pointer.</description>
		<content:encoded><![CDATA[<p>A simple alternative is to use a function such as pthread_once on POSIX or the InitOnce methods in Vista which will run a method a single time regardless of how many threads call it.</p>
<p>As EJ notes above, double checked locking will fail on some compilers even if volatile is used. You need to lock for all access to the pointer.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: EJ</title>
		<link>http://www.codemaestro.com/reviews/10#comment-136</link>
		<pubDate>Wed, 21 Nov 2007 03:13:57 +0000</pubDate>
		<guid>http://www.codemaestro.com/reviews/10#comment-136</guid>
					<description>There are issues with this:
  http://en.wikipedia.org/wiki/Double-checked_locking

Specifically, compilers may optimize the pInstance in such a way that pInstance will point to a partially constructed object.</description>
		<content:encoded><![CDATA[<p>There are issues with this:<br />
  <a href="http://en.wikipedia.org/wiki/Double-checked_locking" rel="nofollow">http://en.wikipedia.org/wiki/Double-checked_locking</a></p>
<p>Specifically, compilers may optimize the pInstance in such a way that pInstance will point to a partially constructed object.
</p>
]]></content:encoded>
				</item>
</channel>
</rss>
