<?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: Question of the day: Threading</title> <atom:link href="http://quickduck.com/blog/2010/06/30/question-of-the-day-threading/feed/" rel="self" type="application/rss+xml" /><link>http://quickduck.com/blog/2010/06/30/question-of-the-day-threading/</link> <description>Straight from the mind of geniuseseses....</description> <lastBuildDate>Thu, 26 Jan 2012 20:48:40 +0000</lastBuildDate> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.1</generator> <item><title>By: Drew</title><link>http://quickduck.com/blog/2010/06/30/question-of-the-day-threading/comment-page-1/#comment-12505</link> <dc:creator>Drew</dc:creator> <pubDate>Mon, 27 Jun 2011 21:04:49 +0000</pubDate> <guid isPermaLink="false">http://quickduck.com/blog/?p=313#comment-12505</guid> <description>Nice one Gerrod!</description> <content:encoded><![CDATA[<p>Nice one Gerrod!</p> ]]></content:encoded> </item> <item><title>By: gerrod</title><link>http://quickduck.com/blog/2010/06/30/question-of-the-day-threading/comment-page-1/#comment-12504</link> <dc:creator>gerrod</dc:creator> <pubDate>Fri, 24 Jun 2011 03:28:57 +0000</pubDate> <guid isPermaLink="false">http://quickduck.com/blog/?p=313#comment-12504</guid> <description>Stu just pointed out that this question was never answered!At an initial glance, it looks like the code is a trick question - the ThreadStatic attribute applies to _instance only, and not to _instanceId - so the code should execute normally and produce:Instance 1 of 2 Instance 2 of 2But if you run the code, you&#039;ll get a null reference exception!The problem lies in the initialisation of the &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/system.threadstaticattribute.aspx&quot; rel=&quot;nofollow&quot;&gt;ThreadStatic&lt;/a&gt; member - it only runs once, on the first thread that runs the initialisation code. So, poor old Thread 2 always has a null _instance!The only way around the problem is to make the field non-readonly, and then modify the Instance property accessor to do the initialisation there:&lt;code&gt; [ThreadStatic] static private Producer _instance;static public Producer Instance { get { return _instance ?? (_instance = new Producer()); } } &lt;/code&gt;Hope that makes sense!</description> <content:encoded><![CDATA[<p>Stu just pointed out that this question was never answered!</p><p>At an initial glance, it looks like the code is a trick question &#8211; the ThreadStatic attribute applies to _instance only, and not to _instanceId &#8211; so the code should execute normally and produce:</p><p> Instance 1 of 2<br /> Instance 2 of 2</p><p>But if you run the code, you&#8217;ll get a null reference exception!</p><p>The problem lies in the initialisation of the <a href="http://msdn.microsoft.com/en-us/library/system.threadstaticattribute.aspx" rel="nofollow">ThreadStatic</a> member &#8211; it only runs once, on the first thread that runs the initialisation code. So, poor old Thread 2 always has a null _instance!</p><p>The only way around the problem is to make the field non-readonly, and then modify the Instance property accessor to do the initialisation there:</p><p><code><br /> [ThreadStatic]<br /> static private Producer _instance;</p><p> static public Producer Instance<br /> {<br /> get { return _instance ?? (_instance = new Producer()); }<br /> }<br /> </code></p><p>Hope that makes sense!</p> ]]></content:encoded> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced

Served from: quickduck.com @ 2012-02-08 15:18:14 -->
