<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Using the Singleton Pattern</title>
	<atom:link href="http://www.jaltiere.com/index.php/2008/03/26/using-the-singleton-pattern/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jaltiere.com/index.php/2008/03/26/using-the-singleton-pattern/</link>
	<description>.NET Software Development</description>
	<lastBuildDate>Mon, 14 Nov 2011 14:58:38 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Jack Altiere</title>
		<link>http://www.jaltiere.com/index.php/2008/03/26/using-the-singleton-pattern/comment-page-1/#comment-24260</link>
		<dc:creator>Jack Altiere</dc:creator>
		<pubDate>Mon, 31 Mar 2008 13:26:19 +0000</pubDate>
		<guid isPermaLink="false">http://www.jaltiere.com/?p=42#comment-24260</guid>
		<description>@Arnon,

You are right that the lazy initialization isn&#039;t necessary, and you&#039;re solution will work.   Lazy initialization is nice if your constructor does anything time consuming.

As for the point on efficiency....I&#039;ll grant you the point that locking is not efficient, but some performance tests were done in the link that Peter pointed out.  The author of that article has a locking solution very similiar to the one I came up with, and in his benchmark he was able to acquire the singleton a billion times in under 40 seconds.  To me, that performance is more than good enough.

I do like your idea with generics, I might have to explore that a little and come back with a follow up post.</description>
		<content:encoded><![CDATA[<p>@Arnon,</p>
<p>You are right that the lazy initialization isn&#8217;t necessary, and you&#8217;re solution will work.   Lazy initialization is nice if your constructor does anything time consuming.</p>
<p>As for the point on efficiency&#8230;.I&#8217;ll grant you the point that locking is not efficient, but some performance tests were done in the link that Peter pointed out.  The author of that article has a locking solution very similiar to the one I came up with, and in his benchmark he was able to acquire the singleton a billion times in under 40 seconds.  To me, that performance is more than good enough.</p>
<p>I do like your idea with generics, I might have to explore that a little and come back with a follow up post.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Arnon</title>
		<link>http://www.jaltiere.com/index.php/2008/03/26/using-the-singleton-pattern/comment-page-1/#comment-24246</link>
		<dc:creator>Arnon</dc:creator>
		<pubDate>Mon, 31 Mar 2008 07:22:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.jaltiere.com/?p=42#comment-24246</guid>
		<description>You implementation with the mutex while thread safe is not very efficient.
Singletons are instantiated once but used many times so it is better to only lock if the instance is null (and then check again since potentially multiple threads can get there.  plus you&#039;d also have to mark the instance volatile in that case)
In any event, if you don&#039;t need the lazy initialization you can just do 
public static readonly MySingleton instance = new MySingleton()
and the CLR will take care of everything for you.

Lastly, if you are going to implement a singleton it is better to implement a &quot;template&quot; one using Generics and then just initialize it with the type you want. This is a better approach from the SRP perspective 

Arnon</description>
		<content:encoded><![CDATA[<p>You implementation with the mutex while thread safe is not very efficient.<br />
Singletons are instantiated once but used many times so it is better to only lock if the instance is null (and then check again since potentially multiple threads can get there.  plus you&#8217;d also have to mark the instance volatile in that case)<br />
In any event, if you don&#8217;t need the lazy initialization you can just do<br />
public static readonly MySingleton instance = new MySingleton()<br />
and the CLR will take care of everything for you.</p>
<p>Lastly, if you are going to implement a singleton it is better to implement a &#8220;template&#8221; one using Generics and then just initialize it with the type you want. This is a better approach from the SRP perspective </p>
<p>Arnon</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jack Altiere</title>
		<link>http://www.jaltiere.com/index.php/2008/03/26/using-the-singleton-pattern/comment-page-1/#comment-24084</link>
		<dc:creator>Jack Altiere</dc:creator>
		<pubDate>Thu, 27 Mar 2008 12:35:57 +0000</pubDate>
		<guid isPermaLink="false">http://www.jaltiere.com/?p=42#comment-24084</guid>
		<description>@Hyle,

That is another example of how to do it.  In that article, they use a method rather than a property to get an instance to the singleton.  It works either way.</description>
		<content:encoded><![CDATA[<p>@Hyle,</p>
<p>That is another example of how to do it.  In that article, they use a method rather than a property to get an instance to the singleton.  It works either way.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Hyle</title>
		<link>http://www.jaltiere.com/index.php/2008/03/26/using-the-singleton-pattern/comment-page-1/#comment-24046</link>
		<dc:creator>Hyle</dc:creator>
		<pubDate>Thu, 27 Mar 2008 06:11:47 +0000</pubDate>
		<guid isPermaLink="false">http://www.jaltiere.com/?p=42#comment-24046</guid>
		<description>Here is another perspective:

http://en.csharp-online.net/Singleton_design_pattern</description>
		<content:encoded><![CDATA[<p>Here is another perspective:</p>
<p><a href="http://en.csharp-online.net/Singleton_design_pattern" rel="nofollow">http://en.csharp-online.net/Singleton_design_pattern</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jack Altiere</title>
		<link>http://www.jaltiere.com/index.php/2008/03/26/using-the-singleton-pattern/comment-page-1/#comment-23985</link>
		<dc:creator>Jack Altiere</dc:creator>
		<pubDate>Wed, 26 Mar 2008 19:02:19 +0000</pubDate>
		<guid isPermaLink="false">http://www.jaltiere.com/?p=42#comment-23985</guid>
		<description>@Peter:

That is a good article, he goes into much more detail than I did with different ways to do it.  I will have to disagree about  it being a &quot;simpler&quot; implementation, especially since my thread-safe version is basically the same thing as his second example. (in fact, they are almost identical)</description>
		<content:encoded><![CDATA[<p>@Peter:</p>
<p>That is a good article, he goes into much more detail than I did with different ways to do it.  I will have to disagree about  it being a &#8220;simpler&#8221; implementation, especially since my thread-safe version is basically the same thing as his second example. (in fact, they are almost identical)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter Ritchie</title>
		<link>http://www.jaltiere.com/index.php/2008/03/26/using-the-singleton-pattern/comment-page-1/#comment-23983</link>
		<dc:creator>Peter Ritchie</dc:creator>
		<pubDate>Wed, 26 Mar 2008 18:51:54 +0000</pubDate>
		<guid isPermaLink="false">http://www.jaltiere.com/?p=42#comment-23983</guid>
		<description>See http://www.yoda.arachsys.com/csharp/singleton.html for much simpler implementations of a thread-safe singleton in C#.</description>
		<content:encoded><![CDATA[<p>See <a href="http://www.yoda.arachsys.com/csharp/singleton.html" rel="nofollow">http://www.yoda.arachsys.com/csharp/singleton.html</a> for much simpler implementations of a thread-safe singleton in C#.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

