<?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: Volatile keyword in C# – memory model explained</title>
	<atom:link href="http://igoro.com/archive/volatile-keyword-in-c-memory-model-explained/feed/" rel="self" type="application/rss+xml" />
	<link>http://igoro.com/archive/volatile-keyword-in-c-memory-model-explained/</link>
	<description>On programming, technology, and random things of interest</description>
	<lastBuildDate>Thu, 02 Feb 2012 01:36:06 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
	<item>
		<title>By: Jakub</title>
		<link>http://igoro.com/archive/volatile-keyword-in-c-memory-model-explained/comment-page-1/#comment-10091</link>
		<dc:creator>Jakub</dc:creator>
		<pubDate>Thu, 21 Jul 2011 06:38:28 +0000</pubDate>
		<guid isPermaLink="false">http://igoro.com/?p=468#comment-10091</guid>
		<description>Hello sir,

know you answer this my question. I ask you, because it is related to this article. Can you help me here please: 
http://social.msdn.microsoft.com/Forums/en-US/modelingandtools/thread/3e5957f3-a45c-49a8-9de5-01f41a9f84d1</description>
		<content:encoded><![CDATA[<p>Hello sir,</p>
<p>know you answer this my question. I ask you, because it is related to this article. Can you help me here please:<br />
<a href="http://social.msdn.microsoft.com/Forums/en-US/modelingandtools/thread/3e5957f3-a45c-49a8-9de5-01f41a9f84d1" rel="nofollow">http://social.msdn.microsoft.c.....f41a9f84d1</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rob</title>
		<link>http://igoro.com/archive/volatile-keyword-in-c-memory-model-explained/comment-page-1/#comment-8369</link>
		<dc:creator>Rob</dc:creator>
		<pubDate>Fri, 01 Jul 2011 16:47:21 +0000</pubDate>
		<guid isPermaLink="false">http://igoro.com/?p=468#comment-8369</guid>
		<description>Sorry... switched to release build and it doesn&#039;t terminate maybe 1 in 5 tries.</description>
		<content:encoded><![CDATA[<p>Sorry&#8230; switched to release build and it doesn&#8217;t terminate maybe 1 in 5 tries.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rob</title>
		<link>http://igoro.com/archive/volatile-keyword-in-c-memory-model-explained/comment-page-1/#comment-8368</link>
		<dc:creator>Rob</dc:creator>
		<pubDate>Fri, 01 Jul 2011 16:42:13 +0000</pubDate>
		<guid isPermaLink="false">http://igoro.com/?p=468#comment-8368</guid>
		<description>I tried running the example at the top of the article. It terminates immediately. Did something change with the Framework?</description>
		<content:encoded><![CDATA[<p>I tried running the example at the top of the article. It terminates immediately. Did something change with the Framework?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Massimiliano</title>
		<link>http://igoro.com/archive/volatile-keyword-in-c-memory-model-explained/comment-page-1/#comment-2167</link>
		<dc:creator>Massimiliano</dc:creator>
		<pubDate>Tue, 18 Jan 2011 18:45:24 +0000</pubDate>
		<guid isPermaLink="false">http://igoro.com/?p=468#comment-2167</guid>
		<description>Woops links lost...
http://www.albahari.com/threading/part4.aspx#_The_volatile_keyword
http://www.bluebytesoftware.com/blog/2008/06/13/VolatileReadsAndWritesAndTimeliness.aspx</description>
		<content:encoded><![CDATA[<p>Woops links lost&#8230;<br />
<a href="http://www.albahari.com/threading/part4.aspx#_The_volatile_keyword" rel="nofollow">http://www.albahari.com/thread.....le_keyword</a><br />
<a href="http://www.bluebytesoftware.com/blog/2008/06/13/VolatileReadsAndWritesAndTimeliness.aspx" rel="nofollow">http://www.bluebytesoftware.co.....iness.aspx</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Massimiliano</title>
		<link>http://igoro.com/archive/volatile-keyword-in-c-memory-model-explained/comment-page-1/#comment-2166</link>
		<dc:creator>Massimiliano</dc:creator>
		<pubDate>Tue, 18 Jan 2011 18:44:15 +0000</pubDate>
		<guid isPermaLink="false">http://igoro.com/?p=468#comment-2166</guid>
		<description>Speaking of Alahari, here volatile keyword he seems to say the opposite of what you say on the volatile keyword. In his &quot;IfYouThinkYouUnderstandVolatile&quot; example he says that a write and a read could be swapped, because the write can be deferred and the read would then read a &quot;stale&quot; value. The analysis of Thread.VolatileWrite and Thread.VolatileRead seems to confirm this. This makes my head explode :-) (he references this article Volatile reads and writes, and timeliness from Joe Duff) that in the last rows seems to say the same thing.</description>
		<content:encoded><![CDATA[<p>Speaking of Alahari, here volatile keyword he seems to say the opposite of what you say on the volatile keyword. In his &#8220;IfYouThinkYouUnderstandVolatile&#8221; example he says that a write and a read could be swapped, because the write can be deferred and the read would then read a &#8220;stale&#8221; value. The analysis of Thread.VolatileWrite and Thread.VolatileRead seems to confirm this. This makes my head explode <img src='http://igoro.com/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  (he references this article Volatile reads and writes, and timeliness from Joe Duff) that in the last rows seems to say the same thing.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sylwester Zaluga</title>
		<link>http://igoro.com/archive/volatile-keyword-in-c-memory-model-explained/comment-page-1/#comment-1683</link>
		<dc:creator>Sylwester Zaluga</dc:creator>
		<pubDate>Tue, 23 Nov 2010 01:44:41 +0000</pubDate>
		<guid isPermaLink="false">http://igoro.com/?p=468#comment-1683</guid>
		<description>Very accessible, figurative explanation of how the things actually work with a volatile keyword. Thanks!</description>
		<content:encoded><![CDATA[<p>Very accessible, figurative explanation of how the things actually work with a volatile keyword. Thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tim Medora</title>
		<link>http://igoro.com/archive/volatile-keyword-in-c-memory-model-explained/comment-page-1/#comment-1127</link>
		<dc:creator>Tim Medora</dc:creator>
		<pubDate>Wed, 08 Sep 2010 20:01:58 +0000</pubDate>
		<guid isPermaLink="false">http://igoro.com/?p=468#comment-1127</guid>
		<description>Good article.  I appreciate the behind-the-scenes explanation; the more I seek to optimize C# code, the more I find myself digging into how the IL/JIT compilers map back to actual processor instructions.

This topic seems particular useful for out-of-process services that are intended to provide asynchronous handling of multiple tasks (such as for a high-traffic website that relies on a SOA to display aggregated data).</description>
		<content:encoded><![CDATA[<p>Good article.  I appreciate the behind-the-scenes explanation; the more I seek to optimize C# code, the more I find myself digging into how the IL/JIT compilers map back to actual processor instructions.</p>
<p>This topic seems particular useful for out-of-process services that are intended to provide asynchronous handling of multiple tasks (such as for a high-traffic website that relies on a SOA to display aggregated data).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brian Gideon</title>
		<link>http://igoro.com/archive/volatile-keyword-in-c-memory-model-explained/comment-page-1/#comment-1086</link>
		<dc:creator>Brian Gideon</dc:creator>
		<pubDate>Mon, 23 Aug 2010 19:23:03 +0000</pubDate>
		<guid isPermaLink="false">http://igoro.com/?p=468#comment-1086</guid>
		<description>This is one of the better explanations of the .NET memory model I have see. You have a done a great job describing the behavior using the &quot;thread cache&quot; illustration. Joe Albahari has a great &lt;a href=&quot;http://www.albahari.com/threading/part4.aspx&quot; rel=&quot;nofollow&quot;&gt;write up&lt;/a&gt; on the topic as well, but uses the instruction reordering terminology. The astute reader of both can appreciate how they compliment each other.</description>
		<content:encoded><![CDATA[<p>This is one of the better explanations of the .NET memory model I have see. You have a done a great job describing the behavior using the &#8220;thread cache&#8221; illustration. Joe Albahari has a great <a href="http://www.albahari.com/threading/part4.aspx" rel="nofollow">write up</a> on the topic as well, but uses the instruction reordering terminology. The astute reader of both can appreciate how they compliment each other.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Igor Ostrovsky</title>
		<link>http://igoro.com/archive/volatile-keyword-in-c-memory-model-explained/comment-page-1/#comment-1053</link>
		<dc:creator>Igor Ostrovsky</dc:creator>
		<pubDate>Tue, 27 Jul 2010 06:52:42 +0000</pubDate>
		<guid isPermaLink="false">http://igoro.com/?p=468#comment-1053</guid>
		<description>Hi Matt,

The safe thing to do would be to publish the Test object by writing it into a volatile field.

1.    The writer first writes all fields on the Test object, and then writes the reference to the Test object into a volatile field.

2.    The reader first reads the reference from a volatile field, and then reads fields of the Test object.

If the object is published via a volatile field, the reader cannot possibly observe the state of the object before it was initialized by the constructor. An alternative solution is to write the object it into a regular field under a lock, and then read it under the same lock.

HOWEVER, in today&#039;s .NET implementation on today&#039;s hardware, I understand that you may be able to get away with removing the volatile modifier on the publication field, even though I would recommend against doing that. For example, Vance Morrison&#039;s article (http://msdn.microsoft.com/en-us/magazine/cc163715.aspx) on memory models suggests that this lazy initialization pattern is safe:

&lt;code&gt;
public class LazyInitClass { 
&#160;&#160;&#160;&#160;private static LazyInitClass myValue = null;

&#160;&#160;&#160;&#160;public static LazyInitClass GetValue() {
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;if (myValue == null)
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;myValue = new LazyInitClass();
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;return myValue;
&#160;&#160;&#160;&#160;}
};
&lt;/code&gt;

I find this particular example very tricky, even trickier than what Vance&#039;s article admits. I understand that the order of writes to myValue and myValue fields is guaranteed by the strong ordering of writes. But, the order of reads has to be enforced using some rule as well. My rough understanding is that x86/x64 will avoid reordering the reads because there is a data dependency between them.

Unfortunately, in this area, even experts disagree on what precisely is and what isn&#039;t guaranteed (&lt;a href=&quot;http://www.bluebytesoftware.com/blog/PermaLink,guid,3420c247-2da5-411b-8ce7-05082e1aba30.aspx&quot; rel=&quot;nofollow&quot;&gt;example&lt;/a&gt;), and so I am not going to claim to know. 

I hope this helps. The summary is this: publish the object either via a volatile field or a field protected by a lock, and you&#039;ll be fine. In typical cases, you&#039;ll have some sort of a synchronization between the reader and the writer to pass the object anyways, and that synchronization will be sufficient to insert the appropriate barriers.</description>
		<content:encoded><![CDATA[<p>Hi Matt,</p>
<p>The safe thing to do would be to publish the Test object by writing it into a volatile field.</p>
<p>1.    The writer first writes all fields on the Test object, and then writes the reference to the Test object into a volatile field.</p>
<p>2.    The reader first reads the reference from a volatile field, and then reads fields of the Test object.</p>
<p>If the object is published via a volatile field, the reader cannot possibly observe the state of the object before it was initialized by the constructor. An alternative solution is to write the object it into a regular field under a lock, and then read it under the same lock.</p>
<p>HOWEVER, in today&#8217;s .NET implementation on today&#8217;s hardware, I understand that you may be able to get away with removing the volatile modifier on the publication field, even though I would recommend against doing that. For example, Vance Morrison&#8217;s article (<a href="http://msdn.microsoft.com/en-us/magazine/cc163715.aspx" rel="nofollow">http://msdn.microsoft.com/en-u.....63715.aspx</a>) on memory models suggests that this lazy initialization pattern is safe:</p>
<p><code><br />
public class LazyInitClass {<br />
&nbsp;&nbsp;&nbsp;&nbsp;private static LazyInitClass myValue = null;</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;public static LazyInitClass GetValue() {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (myValue == null)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myValue = new LazyInitClass();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return myValue;<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
};<br />
</code></p>
<p>I find this particular example very tricky, even trickier than what Vance&#8217;s article admits. I understand that the order of writes to myValue and myValue fields is guaranteed by the strong ordering of writes. But, the order of reads has to be enforced using some rule as well. My rough understanding is that x86/x64 will avoid reordering the reads because there is a data dependency between them.</p>
<p>Unfortunately, in this area, even experts disagree on what precisely is and what isn&#8217;t guaranteed (<a href="http://www.bluebytesoftware.com/blog/PermaLink,guid,3420c247-2da5-411b-8ce7-05082e1aba30.aspx" rel="nofollow">example</a>), and so I am not going to claim to know. </p>
<p>I hope this helps. The summary is this: publish the object either via a volatile field or a field protected by a lock, and you&#8217;ll be fine. In typical cases, you&#8217;ll have some sort of a synchronization between the reader and the writer to pass the object anyways, and that synchronization will be sufficient to insert the appropriate barriers.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matt</title>
		<link>http://igoro.com/archive/volatile-keyword-in-c-memory-model-explained/comment-page-1/#comment-1050</link>
		<dc:creator>Matt</dc:creator>
		<pubDate>Tue, 27 Jul 2010 01:42:01 +0000</pubDate>
		<guid isPermaLink="false">http://igoro.com/?p=468#comment-1050</guid>
		<description>Let&#039;s say we added a second field to your Test class &quot;private Object _loopLock = new Object();&quot; and used it to protect all reads and writes to _loop.  We know from the language spec that all fields are initialized to their default values (in this case null) (http://msdn.microsoft.com/en-us/library/aa645756%28v=VS.71%29.aspx).  As such, couldn&#039;t the &quot;set thread&quot; have a stale initial version of _loopLock in it&#039;s cache (i.e. &quot;null&quot;) and attempt to lock on it?  This seems to raise a chicken and egg problem of requiring a second lock to protect the first lock. And so on and so forth...</description>
		<content:encoded><![CDATA[<p>Let&#8217;s say we added a second field to your Test class &#8220;private Object _loopLock = new Object();&#8221; and used it to protect all reads and writes to _loop.  We know from the language spec that all fields are initialized to their default values (in this case null) (<a href="http://msdn.microsoft.com/en-us/library/aa645756%28v=VS.71%29.aspx" rel="nofollow">http://msdn.microsoft.com/en-u.....71%29.aspx</a>).  As such, couldn&#8217;t the &#8220;set thread&#8221; have a stale initial version of _loopLock in it&#8217;s cache (i.e. &#8220;null&#8221;) and attempt to lock on it?  This seems to raise a chicken and egg problem of requiring a second lock to protect the first lock. And so on and so forth&#8230;</p>
]]></content:encoded>
	</item>
</channel>
</rss>

