<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Lazy init singleton</title><link>http://blogs.msdn.com/davidnotario/archive/2005/07/19/440705.aspx</link><description>I’ve seen some confusion in some MS internal mailing lists about when singletons are instantiated for the pattern described in: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/singletondespatt.asp // .NET Singleton sealed class</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Lazy init singleton</title><link>http://blogs.msdn.com/davidnotario/archive/2005/07/19/440705.aspx#440758</link><pubDate>Wed, 20 Jul 2005 06:48:53 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:440758</guid><dc:creator>Matt Garven</dc:creator><description>This seems like an over-complication to me... what's wrong witht this?&lt;br&gt;&lt;br&gt;public static Singleton Instance&lt;br&gt;{&lt;br&gt;    get&lt;br&gt;    {&lt;br&gt;        if (instance == null)&lt;br&gt;        {&lt;br&gt;            instance = new Singleton();&lt;br&gt;        }&lt;br&gt;        return instance;&lt;br&gt;    }&lt;br&gt;}&lt;br&gt;&lt;br&gt;static Singleton instance;</description></item><item><title>re: Lazy init singleton</title><link>http://blogs.msdn.com/davidnotario/archive/2005/07/19/440705.aspx#440782</link><pubDate>Wed, 20 Jul 2005 07:48:21 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:440782</guid><dc:creator>davidnotario</dc:creator><description>From the msdn article I link:&lt;br&gt;&lt;br&gt;Let&amp;#195;‚&amp;#194;’s examine this code for a moment. This simple class has one member variable and that is a pointer to itself. Notice that the constructor is protected and that the only public method is the Instance method. In the implementation of the Instance method, there is a control block (if) that checks to see if the member variable has been initialized, and if not creates a new instance. This lazy initialization in the control block means that the Singleton instance is initialized, or created, only on the first call to the Instance() method. For many applications, this approach works just fine. But, for multithreaded applications, this approach proves to have a potentially hazardous side effect. If two threads manage to enter the control block at the same time, two instances of the member variable could be created. To solve this, you might be tempted to merely place a critical section around the control block in order to guarantee thread safety. If you do this, then all calls to the Instance method would be serialized and could have a very negative impact on performance, depending on the application. </description></item><item><title>re: Lazy init singleton</title><link>http://blogs.msdn.com/davidnotario/archive/2005/07/19/440705.aspx#440799</link><pubDate>Wed, 20 Jul 2005 08:56:13 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:440799</guid><dc:creator>Oleksii</dc:creator><description>The thread-safe access easily resolved by interlocked double-checking. This is described many times, see C++ example in the 'Modern C++ Design' by A.Alexandrescu.&lt;br&gt;&lt;br&gt;public static Singleton Instance&lt;br&gt;{&lt;br&gt;    get&lt;br&gt;    {&lt;br&gt;        // Prevent unnecessary locking.&lt;br&gt;        if (instance == null)&lt;br&gt;        {&lt;br&gt;            lock (typeof(Singleton))&lt;br&gt;            {&lt;br&gt;                // Check whether this is still true (in a case of multiple treads&lt;br&gt;                // entering the previous if... block simultaneously.&lt;br&gt;                if (instance == null)&lt;br&gt;                {&lt;br&gt;                     instance = new Singleton();&lt;br&gt;                }&lt;br&gt;            }&lt;br&gt;        }&lt;br&gt;        return instance;&lt;br&gt;    }&lt;br&gt;}&lt;br&gt;</description></item><item><title>Building Singletons</title><link>http://blogs.msdn.com/davidnotario/archive/2005/07/19/440705.aspx#440827</link><pubDate>Wed, 20 Jul 2005 10:18:07 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:440827</guid><dc:creator>Erno de Weerd</dc:creator><description /></item><item><title>re: Lazy init singleton</title><link>http://blogs.msdn.com/davidnotario/archive/2005/07/19/440705.aspx#440833</link><pubDate>Wed, 20 Jul 2005 10:56:16 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:440833</guid><dc:creator>Andy Clymer</dc:creator><description>It is highly discouraged to lock a public field, locking based on the type can cause deadlock that is out of the the type authors control. &lt;br&gt;&lt;br&gt;It is always better to create a private static field and lock on that.  &lt;br&gt;</description></item><item><title>Building Singletons</title><link>http://blogs.msdn.com/davidnotario/archive/2005/07/19/440705.aspx#440849</link><pubDate>Wed, 20 Jul 2005 11:35:51 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:440849</guid><dc:creator>Erno de Weerd</dc:creator><description /></item><item><title>Blog link of the week 29</title><link>http://blogs.msdn.com/davidnotario/archive/2005/07/19/440705.aspx#442762</link><pubDate>Mon, 25 Jul 2005 00:28:57 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:442762</guid><dc:creator>Daniel Moth</dc:creator><description>Blog link of the week 29</description></item></channel></rss>