<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.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>Multithreading: using the volatile in your C# code</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/07/05/multithreading-using-the-volatile-in-your-c-code.aspx</link><description>Today we’re only going to talk about the volatile keyword. The volatile keyword can be used on the declaration of a field, transforming it into a volatile field. Currently, you can only annotate a field with this keyword if it is: a reference type; a</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>re: Multithreading: using the volatile in your C# code</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/07/05/multithreading-using-the-volatile-in-your-c-code.aspx#1698454</link><pubDate>Mon, 06 Jul 2009 18:15:45 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1698454</guid><dc:creator>Luis Abreu</dc:creator><description>&lt;p&gt;Yep, according to my studies (and I&amp;#39;m no expert), IA64 have pretty weak memory models, allowing several &amp;quot;problematic&amp;quot; reorderings. and you&amp;#39;re right regarding the problem and its solution.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1698454" width="1" height="1"&gt;</description></item><item><title>re: Multithreading: using the volatile in your C# code</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/07/05/multithreading-using-the-volatile-in-your-c-code.aspx#1698424</link><pubDate>Mon, 06 Jul 2009 16:02:44 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1698424</guid><dc:creator>Ivan Kotev</dc:creator><description>&lt;p&gt;Well, there is one subtle problem :) The problem is with the if-statement &amp;quot;if (_object == null)&amp;quot;. As we know for performance reasons CPUs can store frequently accessible objects in the CPU cache. If we run on a multiprocessor machine and the OS decides to execute our threads on different processors we might come into a situation where Thread1 writes are not visible by Thread2. Maybe the value is still in the CPU1 cache or CPU2 hits the cache (old value) (As far as I know this is true only for the IA64 which has a weaker memory model and allows load semantics to appear out of order. x86 and x64 have CPU cache coherency which means that any change in the CPU1 cache will be synchronized to CPU2 cache??).&lt;/p&gt;
&lt;p&gt;To solve the problem we need to read from the virtual memory and not from the cache, so we might use:&lt;/p&gt;
&lt;p&gt;1) private volatile SomeObject _object; &lt;/p&gt;
&lt;p&gt;2)Thread.VolatileRead(ref _object)	 &amp;nbsp; &lt;/p&gt;
&lt;p&gt;3)Thread.MemoryBarrier before &amp;quot;if (_object == null)&amp;quot;&lt;/p&gt;
&lt;p&gt;Also when _object is null we create new SomeObject and synchronize access to it with a lock (Monitor). When Monitor enters it uses a Read memory barrier and when it exits - Write memory barrier, so no cache hit =&amp;gt; no problem.&lt;/p&gt;
&lt;p&gt;What do you think? &lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://www.bluebytesoftware.com/blog/PermaLink,guid,543d89ad-8d57-4a51-b7c9-a821e3992bf6.aspx"&gt;www.bluebytesoftware.com/.../PermaLink,guid,543d89ad-8d57-4a51-b7c9-a821e3992bf6.aspx&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://www.yoda.arachsys.com/csharp/singleton.html"&gt;www.yoda.arachsys.com/.../singleton.html&lt;/a&gt;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1698424" width="1" height="1"&gt;</description></item><item><title>Multithreading: the volatile keyword – part II</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/07/05/multithreading-using-the-volatile-in-your-c-code.aspx#1698383</link><pubDate>Mon, 06 Jul 2009 10:12:16 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1698383</guid><dc:creator>ASPInsiders</dc:creator><description>&lt;p&gt;In the last post , I’ve showed you some code I’ve written in&amp;#160; the past and asked if there was anything&lt;/p&gt;
&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1698383" width="1" height="1"&gt;</description></item><item><title>Multithreading: the volatile keyword – part II</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/07/05/multithreading-using-the-volatile-in-your-c-code.aspx#1698372</link><pubDate>Mon, 06 Jul 2009 08:53:18 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1698372</guid><dc:creator>LA.NET [EN]</dc:creator><description>&lt;p&gt;In the last post , I’ve showed you some code I’ve written in&amp;#160; the past and asked if there was anything&lt;/p&gt;
&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1698372" width="1" height="1"&gt;</description></item></channel></rss>