<?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>Search results for 'app:weblogs' matching tag 'Multithreading'</title><link>http://msmvps.com/search/SearchResults.aspx?q=app:weblogs&amp;tag=Multithreading&amp;orTags=0&amp;o=DateDescending</link><description>Search results for 'app:weblogs' matching tag 'Multithreading'</description><dc:language>en-US</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>Transforming loops in methods which invoke delegates</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/10/08/transforming-loops-in-methods-which-invoke-delegates.aspx</link><pubDate>Thu, 08 Oct 2009 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1730850</guid><dc:creator>luisabreu</dc:creator><description>&lt;p&gt;In a previous &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/10/04/improving-performance-with-data-parallelism.aspx"&gt;post&lt;/a&gt;, we’ve started talking about data parallelism. Before getting started with the “real stuff”, I thought it would be a good idea to write a small post that explains how to break our loops into units of work. For instance, consider the following snippet:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;int &lt;/span&gt;[] arr = {1, 2, 3};
&lt;span style="color:blue;"&gt;for&lt;/span&gt;(&lt;span style="color:blue;"&gt;var &lt;/span&gt;i = 0; i &amp;lt; arr.Length; i++){
    &lt;span style="color:green;"&gt;//do some work here
&lt;/span&gt;}&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;Before thinking about executing the loop in parallel, we need to identify the main units. It probably won’t take much time until you see that the work you’re doing will need to be wrapped inside an action. For instance, here’s a possible solution for creating a general for method which is capable of doing the same thing as the previous example:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;void &lt;/span&gt;DoLoop(&lt;span style="color:blue;"&gt;int &lt;/span&gt;startIndex, &lt;span style="color:blue;"&gt;int &lt;/span&gt;endIndex, &lt;span style="color:#2b91af;"&gt;Action&lt;/span&gt;&amp;lt;&lt;span style="color:blue;"&gt;int&lt;/span&gt;&amp;gt; loopBody)
{
    &lt;span style="color:blue;"&gt;for &lt;/span&gt;(&lt;span style="color:blue;"&gt;var &lt;/span&gt;i = startIndex; i &amp;lt; endIndex; i++)
    {
        loopBody(i);
    }
}&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;As you can see, we’ve introduced an Action delegate which “moved” the work you’re doing in the loop into a an Action which you can define. That means that now you’d use the following method for interacting over the elements in the array:&lt;/p&gt;

&lt;pre class="code"&gt;DoLoop(0, arr.Length, i =&amp;gt; { &lt;span style="color:green;"&gt;/* do some work */&lt;/span&gt;});&lt;/pre&gt;

&lt;p&gt;In practice, this means that we can concentrate our multithreading efforts in the DoLoop method. The consumer of the method stays “ignorant” and will only have to worry with defining the Action that is executed (don’t forget to respect the assumptions presented in the previous &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/10/04/improving-performance-with-data-parallelism.aspx"&gt;post&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;Things aren’t as straightforward when you don’t know the size of the collection. For instance, when you’re working with an IEnumerator, there’s no way for you to get the number of items without going through all the elements in the collection (and this is something which you might not really want to do in order to improve speed). One of the best approaches here is to build buffers of x items and handle them until you reach the end of the collection (we’ll leave this code for a future post).&lt;/p&gt;

&lt;p&gt;Anyway, I digress. The main purpose of this post is completed! Now that we’ve identified the units of our loop, we can easily update the code of the DoLoop method so that it uses threads for doing its work. In the next post, we’ll start with the simplest approach there is: using the ThreadPool to add parallelism to our DoLoop method. Stay tuned!&lt;/p&gt;</description></item><item><title>Improving performance with data parallelism</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/10/04/improving-performance-with-data-parallelism.aspx</link><pubDate>Sun, 04 Oct 2009 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1729704</guid><dc:creator>luisabreu</dc:creator><description>&lt;p&gt;I know that it’s been a long time since I’ve written on multithreading (I’ve been really busy in JavaScript land!), but that is still an area which interests me and that’s why I’ll be writing on it from time to time.&lt;/p&gt;  &lt;p&gt;As we’ve seen along this &lt;a href="http://msmvps.com/blogs/luisabreu/archive/tags/Multithreading/default.aspx"&gt;series&lt;/a&gt;, there are several things you might do to improve the performance of your apps. One of the most used techniques for improving performance is relying on something called data parallelism. This technique says that you should divide the received data into smaller chunks and then give each chunk to a different processor in order to maximize the throughput of your algorithm. Generally, this is a good approach for those cases where you’ve several items and need to perform some sort of operation over each one of them. In other words, this might be a good option when you need to parallelize a loop.&lt;/p&gt;  &lt;p&gt;Notice that there are several things you should keep in mind before starting to apply this technique. The first thing you need to understand is that you should only apply this when you have a big enough collection of items (big enough depends on the work you’ll be doing with each item and you should always check your “gut feeling” with appropriate measurements). Don’t forget that data parallelism adds overhead to the main operation you’re performing: you’ll be splitting data and then you’ll probably need to get it all back again before returning the processed data back to the user.&lt;/p&gt;  &lt;p&gt;If you’ve already decided that this is the way to go, then you need to make sure that the body of the for loop is thread safe. It goes without saying that thread safe shouldn’t be achieved through the use of locks. Doing that would completely defeat the purpose of executing several operations in parallel and that would mean that the total time of execution wouldn’t really be improved (in many scenarios, you’d probably end up degrading your algorithms).&amp;#160; &lt;/p&gt;  &lt;p&gt;There’s still one last thing you should keep in mind: you should only use this technique when the order of execution for each item in the loop doesn’t matter. If you look at existing literature, you’ll see that some authors tend to call this as non-associative loops.&lt;/p&gt;  &lt;p&gt;And that’s it for this short introduction. In the next couple of posts we’ll be seeing some code for&amp;#160; improving the performance of your loops (always keep in mind the observations I’ve made in this post). Keep tuned for more on multithreading.&lt;/p&gt;</description></item><item><title>Multithreading: the volatile keyword – part II</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/07/06/multithreading-the-volatile-keyword-part-ii.aspx</link><pubDate>Mon, 06 Jul 2009 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1698370</guid><dc:creator>luisabreu</dc:creator><description>&lt;p&gt;In the last &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/07/05/multithreading-using-the-volatile-in-your-c-code.aspx"&gt;post&lt;/a&gt;, I’ve showed you some code I’ve written in&amp;#160; the past and asked if there was anything wrong with it. Here’s the code again:&lt;/p&gt;  &lt;pre style="background-color:black;" class="code"&gt;&lt;span style="color:#ff8000;"&gt;class &lt;/span&gt;&lt;span style="color:yellow;"&gt;Lazy &lt;/span&gt;&lt;span style="color:white;"&gt;{
  &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;private &lt;/span&gt;&lt;span style="color:yellow;"&gt;SomeObject &lt;/span&gt;&lt;span style="color:white;"&gt;_object;
  &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;private &lt;/span&gt;&lt;span style="color:yellow;"&gt;Object &lt;/span&gt;&lt;span style="color:white;"&gt;_locker = &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;new &lt;/span&gt;&lt;span style="color:yellow;"&gt;Object&lt;/span&gt;&lt;span style="color:white;"&gt;();
  &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;public &lt;/span&gt;&lt;span style="color:yellow;"&gt;SomeObject &lt;/span&gt;&lt;span style="color:white;"&gt;SomeObject {
    &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;get &lt;/span&gt;&lt;span style="color:white;"&gt;{
      &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;if &lt;/span&gt;&lt;span style="color:white;"&gt;(_object == &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;null&lt;/span&gt;&lt;span style="color:white;"&gt;) {
        &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;lock &lt;/span&gt;&lt;span style="color:white;"&gt;(_locker) {
          &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;if &lt;/span&gt;&lt;span style="color:white;"&gt;(_object == &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;null&lt;/span&gt;&lt;span style="color:white;"&gt;) {
            _object = &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;new &lt;/span&gt;&lt;span style="color:yellow;"&gt;SomeObject&lt;/span&gt;&lt;span style="color:white;"&gt;();
          }
        }
      }
      &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;return &lt;/span&gt;&lt;span style="color:white;"&gt;_object;
    }
  }
}&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Here’s main idea (for the double-checked locked pattern): you start by checking the instance against null. If it is null, then you acquire the lock and test again. We’re testing it again because someone might have already initialized the instance in the time that passed since the initial test and the lock acquisition. Ok, so is there anything wrong with this?&lt;/p&gt;

&lt;p&gt;To answer this question correctly, we need to go back to the &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/07/03/multithreading-introducing-memory-fences.aspx"&gt;memory model&lt;/a&gt; supported by the CLR. As we’ve seen, the CLR won’t allow store-store reorderings (meaning that all the other types are allowed). If we assume that SomeObject has fields (and this is really a valid assumption), then they will be initialized during construction. So, if we’re using the CLR, everything should be ok because store-store aren’t allowed.&lt;/p&gt;

&lt;p&gt;However, the CLR allows load-load reorderings, meaning that the load of the instance can be moved after the load of its fields, meaning that we could get into trouble. And what happens if we’re writing code that should be run against another ECMA CLI implementation? For instance, say we want to write code that will also run in Mono (I don’t really know Mono, so I don’t know if it follows the CLR 2.0 tighter rules). In this case, store-store reordering are possible and our code might break if the store of the _object instance occurs before the store of its fields. In this case, the testing condition will be null (_object won’t be null) but its fields are because they haven’t been written to yet. Solving this is as simple as adding volatile (recall that volatile allows only store-load reordering!). Here’s the code again:&lt;/p&gt;

&lt;pre style="background-color:black;" class="code"&gt;&lt;span style="color:#ff8000;"&gt;class &lt;/span&gt;&lt;span style="color:yellow;"&gt;Lazy &lt;/span&gt;&lt;span style="color:white;"&gt;{
  &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;private &lt;strong&gt;volatile&lt;/strong&gt; &lt;/span&gt;&lt;span style="color:yellow;"&gt;SomeObject &lt;/span&gt;&lt;span style="color:white;"&gt;_object;
  &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;private &lt;/span&gt;&lt;span style="color:yellow;"&gt;Object &lt;/span&gt;&lt;span style="color:white;"&gt;_locker = &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;new &lt;/span&gt;&lt;span style="color:yellow;"&gt;Object&lt;/span&gt;&lt;span style="color:white;"&gt;();
  &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;public &lt;/span&gt;&lt;span style="color:yellow;"&gt;SomeObject &lt;/span&gt;&lt;span style="color:white;"&gt;SomeObject {
    &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;get &lt;/span&gt;&lt;span style="color:white;"&gt;{
      &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;if &lt;/span&gt;&lt;span style="color:white;"&gt;(_object == &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;null&lt;/span&gt;&lt;span style="color:white;"&gt;) {
        &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;lock &lt;/span&gt;&lt;span style="color:white;"&gt;(_locker) {
          &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;if &lt;/span&gt;&lt;span style="color:white;"&gt;(_object == &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;null&lt;/span&gt;&lt;span style="color:white;"&gt;) {
            _object = &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;new &lt;/span&gt;&lt;span style="color:yellow;"&gt;SomeObject&lt;/span&gt;&lt;span style="color:white;"&gt;();
          }
        }
      }
      &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;return &lt;/span&gt;&lt;span style="color:white;"&gt;_object;
    }
  }
}&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;And that’s it. Adding the volatile keyword solves all the problems mentioned before. Keep tuned for more on multithreading.&lt;/p&gt;</description></item><item><title>Multithreading: using VolatileXXX instead of the volatile keyword</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/07/06/multithreading-using-volatilexxx-instead-of-the-volatile-keyword.aspx</link><pubDate>Mon, 06 Jul 2009 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1698477</guid><dc:creator>luisabreu</dc:creator><description>&lt;p&gt;[Update: Brad detected a flaw in the code: I had forgotten to initialize the _initialized field. Thanks!]&lt;/p&gt;  &lt;p&gt;[Update2: Brad detected another flaw in the code: return _instance must be outside the if. Thanks!]&lt;/p&gt;  &lt;p&gt;In the previous &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/07/06/multithreading-the-volatile-keyword-part-ii.aspx"&gt;post&lt;/a&gt; we’ve seen how we can use the C# &lt;a href="http://msdn.microsoft.com/en-us/library/x13ttww7(VS.71).aspx"&gt;volatile&lt;/a&gt; keyword to guarantee that those nasty load-load reordering stay away from our code. As I’ve said before, we can also use the static &lt;a href="http://msdn.microsoft.com/en-us/library/system.threading.thread.volatileread(loband).aspx"&gt;Thread.VolatileRead&lt;/a&gt; or &lt;a href="http://msdn.microsoft.com/en-us/library/system.threading.thread.volatilewrite.aspx"&gt;Thread.VolatileWrite&lt;/a&gt; for having more control over the way fences are applied to our code. Going back to our previous volatile example, the question is: do we really need a fence whenever we access our instance variable? &lt;/p&gt;  &lt;p&gt;Looking at the code, I guess that we can get&amp;#160; away by just using an acquire fence on the initialization of the instance. Recall that an acquire fence is an optimization of the full fence and ensures that no load or store that comes after the fence can be moved before the fence (it’s just what we need to ensure proper initialization and eliminate the possible load/load reorderings allowed by the CLR). &lt;/p&gt;  &lt;p&gt;With this in mind, lets update our sample, ok? Btw, we’ll be using another variable for controlling initialization (we’re picking an integer). This is your best option for initializing value types since you can’t control it size or check it for null (don’t forget our previous &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/06/29/multithreading-hardware-atomicity.aspx"&gt;discussion&lt;/a&gt; on word size, alignment and .NET). Here’s the final code:&lt;/p&gt;  &lt;pre style="background-color:black;" class="code"&gt;&lt;span style="color:#ff8000;"&gt;class &lt;/span&gt;&lt;span style="color:yellow;"&gt;Lazy&lt;/span&gt;&lt;span style="color:white;"&gt;{
  &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;private &lt;/span&gt;&lt;span style="color:yellow;"&gt;Object &lt;/span&gt;&lt;span style="color:white;"&gt;_locker = &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;new &lt;/span&gt;&lt;span style="color:yellow;"&gt;Object&lt;/span&gt;&lt;span style="color:white;"&gt;();
  &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;private &lt;/span&gt;&lt;span style="color:yellow;"&gt;SomeObject &lt;/span&gt;&lt;span style="color:white;"&gt;_instance = &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;null&lt;/span&gt;&lt;span style="color:white;"&gt;;
  &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;private &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;Int32 &lt;/span&gt;&lt;span style="color:white;"&gt;_initialized = 0;
  &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;public &lt;/span&gt;&lt;span style="color:yellow;"&gt;SomeObject &lt;/span&gt;&lt;span style="color:white;"&gt;SomeObject {
    &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;get &lt;/span&gt;&lt;span style="color:white;"&gt;{
      &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;if &lt;/span&gt;&lt;span style="color:white;"&gt;(&lt;/span&gt;&lt;span style="color:yellow;"&gt;Thread&lt;/span&gt;&lt;span style="color:white;"&gt;.VolatileRead(&lt;/span&gt;&lt;span style="color:#ff8000;"&gt;ref &lt;/span&gt;&lt;span style="color:white;"&gt;_initialized) == 0) {
        &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;lock &lt;/span&gt;&lt;span style="color:white;"&gt;(_locker) {
          &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;if &lt;/span&gt;&lt;span style="color:white;"&gt;(_initialized == 0) {
            _instance = &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;new &lt;/span&gt;&lt;span style="color:yellow;"&gt;SomeObject&lt;/span&gt;&lt;span style="color:white;"&gt;();&lt;br /&gt;            _initialized = 1;
          }
        }&lt;br /&gt;      }
      &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;return &lt;/span&gt;&lt;span style="color:white;"&gt;_instance;
    }
  }
}&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This code is also correct and will behave properly in all the current architectures that run Windows and the CLR. There’s no need for running another VolatileRead on the inner comparison due to a thing called control dependency (check this &lt;a href="http://www.bluebytesoftware.com/blog/PermaLink,guid,543d89ad-8d57-4a51-b7c9-a821e3992bf6.aspx"&gt;post&lt;/a&gt; by &lt;a href="http://www.bluebytesoftware.com/blog/"&gt;Joe Duffy&lt;/a&gt; for more info). Notice that in these posts our main objective is ensuring that you end up getting only one instance of a specific type. As I’ve said, if you don’t care about creating multiple instances and only need to ensure that you’ll have only one active instance, you can only use the Interlocked.CompareExchange method for that. We’ll see how in the next post. Keep tuned!&lt;/p&gt;</description></item><item><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><pubDate>Sun, 05 Jul 2009 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1698282</guid><dc:creator>luisabreu</dc:creator><description>&lt;p&gt;Today we’re only going to talk about the &lt;a href="http://msdn.microsoft.com/en-us/library/x13ttww7(VS.71).aspx"&gt;volatile&lt;/a&gt; keyword. The &lt;a href="http://msdn.microsoft.com/en-us/library/x13ttww7(VS.71).aspx"&gt;volatile&lt;/a&gt; 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:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;a reference type; &lt;/li&gt;    &lt;li&gt;a pointer type (unsafe code); &lt;/li&gt;    &lt;li&gt;one of the following types: sbyte, byte, short, ushort, int, uint, char, float or bool; &lt;/li&gt;    &lt;li&gt;an enum with a base type of byte, sbyte, short, ushort, int or uint. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;As we’ve &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/07/03/multithreading-using-fences-from-net-code.aspx"&gt;seen&lt;/a&gt;, volatiles ensures that proper fences are applied when someone access that field (ie, reading means having an acquire fence and writing ends up injecting a release fence). As you know by now, load and store reordering can happen at several levels and you might be wondering if using the volatile is enough for ensuring that fences are applied on all levels. Fortunately, the answer is yes, and the volatile keyword&amp;#160; is respected by the compiler and by the processor.&lt;/p&gt;  &lt;p&gt;Ok, so when should you use this keyword? Well, probably an example is in order, right? Lets take a look at the following code which shows the code I’ve written in the past for lazy loading:&lt;/p&gt;  &lt;pre style="background-color:black;" class="code"&gt;&lt;span style="color:#ff8000;"&gt;class &lt;/span&gt;&lt;span style="color:yellow;"&gt;Lazy &lt;/span&gt;&lt;span style="color:white;"&gt;{
  &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;private &lt;/span&gt;&lt;span style="color:yellow;"&gt;SomeObject &lt;/span&gt;&lt;span style="color:white;"&gt;_object;
  &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;private &lt;/span&gt;&lt;span style="color:yellow;"&gt;Object &lt;/span&gt;&lt;span style="color:white;"&gt;_locker = &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;new &lt;/span&gt;&lt;span style="color:yellow;"&gt;Object&lt;/span&gt;&lt;span style="color:white;"&gt;();
  &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;public &lt;/span&gt;&lt;span style="color:yellow;"&gt;SomeObject &lt;/span&gt;&lt;span style="color:white;"&gt;SomeObject {
    &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;get &lt;/span&gt;&lt;span style="color:white;"&gt;{
      &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;if &lt;/span&gt;&lt;span style="color:white;"&gt;(_object == &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;null&lt;/span&gt;&lt;span style="color:white;"&gt;) {
        &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;lock &lt;/span&gt;&lt;span style="color:white;"&gt;(_locker) {
          &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;if &lt;/span&gt;&lt;span style="color:white;"&gt;(_object == &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;null&lt;/span&gt;&lt;span style="color:white;"&gt;) {
            _object = &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;new &lt;/span&gt;&lt;span style="color:yellow;"&gt;SomeObject&lt;/span&gt;&lt;span style="color:white;"&gt;();
          }
        }
      }
      &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;return &lt;/span&gt;&lt;span style="color:white;"&gt;_object;
    }
  }
}&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;What is your opinion? Do you see anything wrong (btw, suppose SomeObject is a reference type with some properties). I’ll return tomorrow and we’ll come back to this discussion. Keep tuned!&lt;/p&gt;</description></item><item><title>Multithreading: a final example on how CompareExchange might help you</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/07/04/multithreading-a-final-example-on-how-compareexchange-might-help-you.aspx</link><pubDate>Sat, 04 Jul 2009 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1697975</guid><dc:creator>luisabreu</dc:creator><description>&lt;p&gt;In the last posts we’ve been poking around memory models, &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/07/03/multithreading-introducing-memory-fences.aspx"&gt;memory&lt;/a&gt; &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/07/03/multithreading-using-fences-from-net-code.aspx"&gt;fences&lt;/a&gt; and other interesting things that might lead to the so called lock free programming. Before keep looking at how we can use those features from our C# code, I’d like to add one more example that shows how the interlocked operations might help you in the real world. Do you recall our &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/06/15/multithreading-implementing-the-iasyncresult-interface.aspx"&gt;implementation&lt;/a&gt; of the IAsyncResult interface? &lt;/p&gt;  &lt;p&gt;At the time, we’ve used a lock for ensuring proper access and initialization of our manual reset event used for signaling the end of the operation. Now that we’ve met interlocked operations, we can improve that code and get rid of the lock. Let’s focus on the private GetEvtHandle method:&lt;/p&gt;  &lt;pre style="background-color:black;" class="code"&gt;&lt;span style="color:#ff8000;"&gt;private &lt;/span&gt;&lt;span style="color:yellow;"&gt;ManualResetEvent &lt;/span&gt;&lt;span style="color:white;"&gt;GetEvtHandle() {
  &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;var &lt;/span&gt;&lt;span style="color:white;"&gt;newEvt = &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;new &lt;/span&gt;&lt;span style="color:yellow;"&gt;ManualResetEvent&lt;/span&gt;&lt;span style="color:white;"&gt;(&lt;/span&gt;&lt;span style="color:#ff8000;"&gt;false&lt;/span&gt;&lt;span style="color:white;"&gt;);
  &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;if &lt;/span&gt;&lt;span style="color:white;"&gt;(&lt;/span&gt;&lt;span style="color:yellow;"&gt;Interlocked&lt;/span&gt;&lt;span style="color:white;"&gt;.CompareExchange(&lt;/span&gt;&lt;span style="color:#ff8000;"&gt;ref &lt;/span&gt;&lt;span style="color:white;"&gt;_evt, newEvt, &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;null&lt;/span&gt;&lt;span style="color:white;"&gt;) != &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;null&lt;/span&gt;&lt;span style="color:white;"&gt;) {
    newEvt.Close();
  }      
  &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;if &lt;/span&gt;&lt;span style="color:white;"&gt;(_isCompleted) {
    _evt.Set();
  }      
  &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;return &lt;/span&gt;&lt;span style="color:white;"&gt;_evt;
}&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;As you can see, we’ve replaced the lock with a CompareExchange method call for ensuring proper atomic update: if the _evt field is null, then set it to newEvt (which, if you recall our previous post on interlocked operations, will only happen when that value is null!). Since the method returns the old value, if we get anything different from null, then it means that some other thread already set the field to a valid value. When that happens, we need to clean up and close the manual reset event we’ve just created. And there you go: the lock is gone.&lt;/p&gt;

&lt;p&gt;As you’ve probably guessed, This strategy can be adjusted to objects that implement the IDisposable interface and you can use it when the creation of new objects isn’t too expensive and you need to have only a single instance of an item (in other words, it might be a valid option for implementing singletons in multithreading scenarios).&lt;/p&gt;

&lt;p&gt;And that’s it. Keep tuned for more on multithreading.&lt;/p&gt;</description></item><item><title>Multithreading: introducing memory fences</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/07/03/multithreading-introducing-memory-fences.aspx</link><pubDate>Fri, 03 Jul 2009 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1697685</guid><dc:creator>luisabreu</dc:creator><description>&lt;p&gt;A few posts back, we’ve introduced the &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/06/29/multithreading-load-and-store-reordering.aspx"&gt;concept&lt;/a&gt; of load and store reordering. As we’ve seen, reordering operations exist as a way of improving performance and can be introduced on several levels (starting at compilation time and ending at runtime when the processor executes the instructions). We &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/06/29/multithreading-load-and-store-reordering.aspx"&gt;saw&lt;/a&gt; that even though things can get chaotic quickly, there are some guarantees we can hold on to when writing multithreaded code. One of those guarantees is that all platforms respect a specific memory model and that’s what we’ll be talking about in this post.&lt;/p&gt;  &lt;p&gt;A memory model defines which kinds of moves may occur&amp;#160; (ie, which loads and stores can be moved). If you’ve got a weak memory model, then you’ll get plenty of allowable moves and this will lead to a superior performance. However, you’ll also need to pay lots of attention to the code you write. Allowing less moves will, of course, lead to less complexity but won’t give you (hum…I mean, the compiler and processor) that many chances for updating your code.&lt;/p&gt;  &lt;p&gt;Since we’re talking about .NET here, we’ll focus the rest of the post on the valid assumptions for the CLR. The CLR has a strong memory model. In practice, this means that several compiler optimizations are forbidden&amp;#160; and that it should be fairly easy (sort of…) to write code that is portable across several architectures where the code might run. Before going on, it’s important to notice that the CLR memory model is tighter than the one you get in the ECMA spec.&lt;/p&gt;  &lt;p&gt;In the CLR, you can get reordering for load/load, load/store and store/load. The only one which isn’t permitted is store/store reordering (meaning that a store can never move after another store). Volatile loads and stores are different and only allow store/load reordering (we’ll be talking about volatiles in future posts). Btw, the ECMA specification allows all these move types.&lt;/p&gt;  &lt;p&gt;Ok, so if those store and load reordering are allowed, how can we stopped them from happening? ah, glad you asked! We can use fences (or barriers) to ensure that they don’t occur at specific times.&lt;/p&gt;  &lt;p&gt;A fence (aka barrier) prevents memory loads and stores reordering from happening. There are several types of fences. Full fences are probably the most known and used type. A full fence ensures that no load or stores moves across the fence (ie, no load or store before the fence can move after it nor any load or store placed after the fence may move before it).&lt;/p&gt;  &lt;p&gt;Besides the ubiquitous full fence (which is available everywhere), there are other variations. With Store fences, no store can move over the fence (it’s ok for reorders to happen with loads). Load fences are similar, but in this case, only loads are “fixed”.&lt;/p&gt;  &lt;p&gt;Finally, there’s also a couple of “one way” fences: acquire and release fences. Acquire fences ensures that no memory operation that happens after the fence can be moved before the fence. Release fences work the other way around: instructions defined after the fence may happen before the fence but no “pre-fence” instruction may happen after the fence. &lt;/p&gt;  &lt;p&gt;As you might have guessed by now, fences load to a more sequential model which will, without any doubt, lead to a degradation of your application. This means that you should apply them carefully. Yes, we do need fences, but do keep in mind that using them will reduce the ability for reordering and improving the code you write.&lt;/p&gt;  &lt;p&gt;By now, I guess that we’ve covered most of the theory around fences. You might be asking: how do I use fences in my .NET code. Good question, but we’ll leave the answer for the next post. Keep tuned for more on multithreading.&lt;/p&gt;</description></item><item><title>Multithreading: using fences from .NET code</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/07/03/multithreading-using-fences-from-net-code.aspx</link><pubDate>Fri, 03 Jul 2009 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1697699</guid><dc:creator>luisabreu</dc:creator><description>&lt;p&gt;In the last &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/07/03/multithreading-introducing-memory-fences.aspx"&gt;post&lt;/a&gt;, we’ve talked about memory fences. Today we’re going to keep looking at this topic, but we’re turning our attention to coding, ie, we’re going to talk about the options we have to add fences to our classes. In .NET, things are relatively straightforward. &lt;/p&gt;  &lt;p&gt;Whenever we use one of the interlocked methods we’ve &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/07/02/multithreading-introducing-the-interlocked-operations.aspx"&gt;met&lt;/a&gt; in the past, we’re adding a full fence to our code. Locking will also end up using full fences. As you can see,&amp;#160; you’re already using full fences in several places. The good news is that you can also be specific about them, ie, there’s a method you can call if you want to add a fence to your code in a specific place: I’m talking about the &lt;a href="http://msdn.microsoft.com/en-us/library/system.threading.thread.memorybarrier.aspx"&gt;Thread.MemoryBarrier&lt;/a&gt; static method (btw, this method will also add a&amp;#160; full fence).&lt;/p&gt;  &lt;p&gt;Volatile reads or writes generate fences too! In C#, you can use the &lt;a href="http://msdn.microsoft.com/en-us/library/x13ttww7(VS.71).aspx"&gt;volatile&lt;/a&gt; keyword to mark a field as volatile. Reading a &lt;a href="http://msdn.microsoft.com/en-us/library/x13ttww7(VS.71).aspx"&gt;volatile&lt;/a&gt; field is the “equivalent” of having an acquiring fence. Writing to a &lt;a href="http://msdn.microsoft.com/en-us/library/x13ttww7(VS.71).aspx"&gt;volatile&lt;/a&gt; field can be seen as “adding” a release fence. Now, it’s important to notice that you’ll always get these read and write behaviors whenever you use the volatile field. If you’re looking for more control (ex.: you’ll only interested in having an acquire fence for a read), then you should rely on the &lt;a href="http://msdn.microsoft.com/en-us/library/system.threading.thread.volatileread.aspx"&gt;Thread.VolatileRead&lt;/a&gt; or &lt;a href="http://msdn.microsoft.com/en-us/library/system.threading.thread.volatilewrite.aspx"&gt;Thread.VolatileWrite&lt;/a&gt; methods for ensuring proper the desired behavior.&lt;/p&gt;  &lt;p&gt;And I guess that’s all there’s time for today. In the next post, we’ll keep looking at multithreading and see how we can use these features on a C# program. Keep tuned!&lt;/p&gt;</description></item><item><title>Multithreading: introducing the interlocked operations</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/07/02/multithreading-introducing-the-interlocked-operations.aspx</link><pubDate>Thu, 02 Jul 2009 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1697571</guid><dc:creator>luisabreu</dc:creator><description>&lt;p&gt;As we’ve seen in the previous &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/06/29/multithreading-hardware-atomicity.aspx"&gt;post&lt;/a&gt;, most processors give us important insurances regarding memory loads and stores. However, even though those insurances are important and can be used in several scenarios, the truth is that they aren’t enough for all real world tasks.&lt;/p&gt;  &lt;p&gt;Fortunately, most processors also offer a group of interlocked operations which enable atomic compare and swap scenarios. These operations rely on hardware and interprocess synchronization. Notice that these operations aren’t as simply as they might seem at first sight. For instance, it’s important to recall that in today’s architectures, these kind of operations need to play well with caches. My point is that event though these operations tend to be cheaper that the traditional locks, they’re still not cheap (for instance, there are cases where an interlocked operation ends up locking the bus and that is not a good thing).&lt;/p&gt;  &lt;p&gt;Currently, there are several kinds of interlocked operations. In .NET, all interlocked operations are exposed as members of the &lt;a href="http://msdn.microsoft.com/en-us/library/system.threading.interlocked.aspx"&gt;Interlocked&lt;/a&gt; class:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Add: adds two integers (int or long) and replaces the first with the value of the sum; &lt;/li&gt;    &lt;li&gt;CompareAndExchange: compares two values and if they’re equal, replaces one of them with the other (notice that this method receives three parameters). This is probably the most important method of this class (it supports compares and exchanges on reference types two!); &lt;/li&gt;    &lt;li&gt;Decrement: Similar to add, but in this case, it performs a subtraction; &lt;/li&gt;    &lt;li&gt;Exchange: updates a variable with another value; &lt;/li&gt;    &lt;li&gt;Increment: adds one to an existing variable; &lt;/li&gt;    &lt;li&gt;Read: reads a value from a variable; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;As I’ve said before, the advantage of using these methods is that all the operations are performed atomically. The Read method might not seen necessary at first until you look at its signature:&lt;/p&gt;  &lt;pre style="background-color:black;" class="code"&gt;&lt;span style="color:#ff8000;"&gt;public static long &lt;/span&gt;&lt;span style="color:white;"&gt;Read(&lt;/span&gt;&lt;span style="color:#ff8000;"&gt;ref long &lt;/span&gt;&lt;span style="color:white;"&gt;location);&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;As you can see, it should only be used in 32 bits system when you need to access 64 bits integers. Atomically setting a value on 64 bits can be done through the Exchange method:&lt;/p&gt;

&lt;pre style="background-color:black;" class="code"&gt;&lt;span style="color:#ff8000;"&gt;public static extern long &lt;/span&gt;&lt;span style="color:white;"&gt;Exchange(&lt;/span&gt;&lt;span style="color:#ff8000;"&gt;ref long &lt;/span&gt;&lt;span style="color:white;"&gt;location1, &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;long &lt;/span&gt;&lt;span style="color:white;"&gt;value);&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This means that you can easily build a generic write or read routine and reuse them across your programs:&lt;/p&gt;

&lt;pre style="background-color:black;" class="code"&gt;&lt;span style="color:#ff8000;"&gt;public static class &lt;/span&gt;&lt;span style="color:yellow;"&gt;Generic64Helper &lt;/span&gt;&lt;span style="color:white;"&gt;{
  &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;private const int &lt;/span&gt;&lt;span style="color:white;"&gt;WordSize32 = 4;
  &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;public static void &lt;/span&gt;&lt;span style="color:white;"&gt;WriteLong(&lt;/span&gt;&lt;span style="color:#ff8000;"&gt;ref long &lt;/span&gt;&lt;span style="color:white;"&gt;location, &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;long &lt;/span&gt;&lt;span style="color:white;"&gt;value) {
    &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;if &lt;/span&gt;&lt;span style="color:white;"&gt;(&lt;/span&gt;&lt;span style="color:#2b91af;"&gt;IntPtr&lt;/span&gt;&lt;span style="color:white;"&gt;.Size == WordSize32) {
        &lt;/span&gt;&lt;span style="color:yellow;"&gt;Interlocked&lt;/span&gt;&lt;span style="color:white;"&gt;.Exchange(&lt;/span&gt;&lt;span style="color:#ff8000;"&gt;ref &lt;/span&gt;&lt;span style="color:white;"&gt;location, value);
    } &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;else &lt;/span&gt;&lt;span style="color:white;"&gt;{
        location = value;
    }
  }
  &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;public static long &lt;/span&gt;&lt;span style="color:white;"&gt;ReadLong(&lt;/span&gt;&lt;span style="color:#ff8000;"&gt;ref long &lt;/span&gt;&lt;span style="color:white;"&gt;location) {
    &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;if &lt;/span&gt;&lt;span style="color:white;"&gt;(&lt;/span&gt;&lt;span style="color:#2b91af;"&gt;IntPtr&lt;/span&gt;&lt;span style="color:white;"&gt;.Size == WordSize32) {
        &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;return &lt;/span&gt;&lt;span style="color:yellow;"&gt;Interlocked&lt;/span&gt;&lt;span style="color:white;"&gt;.Read(&lt;/span&gt;&lt;span style="color:#ff8000;"&gt;ref &lt;/span&gt;&lt;span style="color:white;"&gt;location);
    }
    &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;return &lt;/span&gt;&lt;span style="color:white;"&gt;location;
  }
}&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;There a couple of interesting things going on here. First, we use the &lt;a href="http://msdn.microsoft.com/en-us/library/system.intptr.size.aspx"&gt;IntPtr.Size&lt;/a&gt; property to get the size of the “current” word. In 64 bits, we really don’t want to pay the price of the Interlocked operation and will simply delegate to a hardware atomic write or read. However, in 32 bits system (where the word size is 4), we really need to use those methods to have atomicity. Reusing this class is rather simple, as you can see from the next snippet (t is initialized with 10 and y is initialized with the value of t):&lt;/p&gt;

&lt;pre style="background-color:black;" class="code"&gt;&lt;span style="color:yellow;"&gt;Generic64Helper&lt;/span&gt;&lt;span style="color:white;"&gt;.WriteLong(&lt;/span&gt;&lt;span style="color:#ff8000;"&gt;ref &lt;/span&gt;&lt;span style="color:white;"&gt;t, 10);
&lt;/span&gt;&lt;span style="color:#ff8000;"&gt;long &lt;/span&gt;&lt;span style="color:white;"&gt;y = &lt;/span&gt;&lt;span style="color:yellow;"&gt;Generic64Helper&lt;/span&gt;&lt;span style="color:white;"&gt;.ReadLong(&lt;/span&gt;&lt;span style="color:#ff8000;"&gt;ref &lt;/span&gt;&lt;span style="color:white;"&gt;t);&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;There a couple of gotchas with the previous approach (notice that only the read and write are atomics), but we’ll leave that for a future post. Now, the important thing is to concentrate on introducing most of these methods. If you use reflector, you should see that Interlocked.Read depends directly on the CompareExchange method:&lt;/p&gt;

&lt;pre style="background-color:black;" class="code"&gt;&lt;span style="color:#ff8000;"&gt;public static long &lt;/span&gt;&lt;span style="color:white;"&gt;Read(&lt;/span&gt;&lt;span style="color:#ff8000;"&gt;ref long &lt;/span&gt;&lt;span style="color:white;"&gt;location) {
  &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;return &lt;/span&gt;&lt;span style="color:white;"&gt;CompareExchange(&lt;/span&gt;&lt;span style="color:#ff8000;"&gt;ref &lt;/span&gt;&lt;span style="color:white;"&gt;location, 0L, 0L);
}
&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This is really a cool trick!&lt;/p&gt;

&lt;p&gt;As I’ve said before, the CompareExchange method is one of the most important methods exposed by the class. The CompareExchange method receives three parameters: if the first and third parameters are equal, then it updates the first with the value of the second parameter. The method will always return the initial value of the first parameter (even when there’s no update).&lt;/p&gt;

&lt;p&gt;So, the code used on the Read method will only end up doing a write (ie, a store) if the current stored value is 0. And even when this happens, there really isn’t any update in the value (notice that if its value is 0 we’re writing 0 again!).&lt;/p&gt;

&lt;p&gt;The remaining methods exposed by the class are fairly obvious, so I won’t really go into examples here (the docs do a good job on showing how to use these methods). The important thing to take from this post is that these operations are atomic and are performed at the hardware level. On the next post, we’ll keep talking about this class and on how you can reuse them to add some free lock programming to your apps. Keep tuned!&lt;/p&gt;</description></item><item><title>Multithreading: when interlocked operations aren’t enough</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/07/02/multithreading-when-interlocked-operations-aren-t-enough.aspx</link><pubDate>Thu, 02 Jul 2009 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1697578</guid><dc:creator>luisabreu</dc:creator><description>&lt;p&gt;In the previous &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/07/02/multithreading-introducing-the-interlocked-operations.aspx"&gt;post&lt;/a&gt;, we’ve started looking at interlocked operations. As we’ve seen, interlocked operations are great at what they do but they won’t be usable in all scenarios (ie, don’t think that they’ll solve all your locks problems). To show how things might go awry when using interlocks, I’ll reuse a great &lt;a href="http://blogs.msdn.com/oldnewthing/archive/2004/09/15/229915.aspx"&gt;example&lt;/a&gt; written by &lt;a href="http://blogs.msdn.com/oldnewthing/default.aspx"&gt;Raymond Chen&lt;/a&gt; a few years ago (I’m updating it to C#):&lt;/p&gt;  &lt;pre style="background-color:black;" class="code"&gt;&lt;span style="color:#ff8000;"&gt;class &lt;/span&gt;&lt;span style="color:yellow;"&gt;Program 
&lt;/span&gt;&lt;span style="color:white;"&gt;{
  &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;private static &lt;/span&gt;&lt;span style="color:yellow;"&gt;Object &lt;/span&gt;&lt;span style="color:white;"&gt;_lock = &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;new &lt;/span&gt;&lt;span style="color:yellow;"&gt;Object&lt;/span&gt;&lt;span style="color:white;"&gt;();
  &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;public static &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;Int64 &lt;/span&gt;&lt;span style="color:white;"&gt;InterlockedMultiply(&lt;br /&gt;          &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;ref &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;Int64 &lt;/span&gt;&lt;span style="color:white;"&gt;multiplicand, &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;Int64 &lt;/span&gt;&lt;span style="color:white;"&gt;multiplier) {
    &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;Int64 &lt;/span&gt;&lt;span style="color:white;"&gt;result = 0;
    &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;lock &lt;/span&gt;&lt;span style="color:white;"&gt;(_lock) {
      &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;var &lt;/span&gt;&lt;span style="color:white;"&gt;aux = multiplicand;
      &lt;/span&gt;&lt;span style="color:yellow;"&gt;Thread&lt;/span&gt;&lt;span style="color:white;"&gt;.Sleep(100);//oops!!!
      result = multiplicand = aux * multiplier;          
    }
    &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;return &lt;/span&gt;&lt;span style="color:white;"&gt;result;
  }      
  &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;static void &lt;/span&gt;&lt;span style="color:white;"&gt;Main(&lt;/span&gt;&lt;span style="color:#ff8000;"&gt;string&lt;/span&gt;&lt;span style="color:white;"&gt;[] args) {
      &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;Int64 &lt;/span&gt;&lt;span style="color:white;"&gt;a = 5;
      &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;new &lt;/span&gt;&lt;span style="color:yellow;"&gt;Thread&lt;/span&gt;&lt;span style="color:white;"&gt;(&lt;br /&gt;         () =&amp;gt; InterlockedMultiply(&lt;/span&gt;&lt;span style="color:#ff8000;"&gt;ref &lt;/span&gt;&lt;span style="color:white;"&gt;a, 5)).Start();
      &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;new &lt;/span&gt;&lt;span style="color:yellow;"&gt;Thread&lt;/span&gt;&lt;span style="color:white;"&gt;(() =&amp;gt; { &lt;br /&gt;              &lt;/span&gt;&lt;span style="color:yellow;"&gt;Thread&lt;/span&gt;&lt;span style="color:white;"&gt;.Sleep(50); &lt;br /&gt;              &lt;/span&gt;&lt;span style="color:yellow;"&gt;Interlocked&lt;/span&gt;&lt;span style="color:white;"&gt;.Increment(&lt;/span&gt;&lt;span style="color:#ff8000;"&gt;ref &lt;/span&gt;&lt;span style="color:white;"&gt;a); }).Start();
      &lt;/span&gt;&lt;span style="color:yellow;"&gt;Thread&lt;/span&gt;&lt;span style="color:white;"&gt;.Sleep(2000);
      &lt;/span&gt;&lt;span style="color:yellow;"&gt;Console&lt;/span&gt;&lt;span style="color:white;"&gt;.WriteLine(a);
  }
}&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;The idea is to add a safe multiplier method. As we’ve seen in the previous &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/07/02/multithreading-introducing-the-interlocked-operations.aspx"&gt;post&lt;/a&gt;, interlocked increments are atomic. That means that they’re executed as a “single” operation by the processor. Since we didn’t had a method that performs the same operation for multiplication, we’ve decided to mimic that behavior by adding a new method which uses a lock to ensure proper multiplication.&lt;/p&gt;

&lt;p&gt;If I asked you what Console.WriteLine(a) would print, what would you say? For now, forget those nasty Sleep invocations (they’re there to force the wrong behavior)… I’m guessing that you’d probably say that Console.WriteLine will only write 26 or 30. It will write 26 if InterlockedMultiply “beats” Increment or 30 if Increment is run before InterlockedMultiply. Ah, well, with those nasty sleep instructions, I’ve managed to get 25 here on my machine. Wtf? How? Why?&lt;/p&gt;

&lt;p&gt;Well, what happened is logical…Interlocked.Increment will always update the value in a single atomic operation (this means it will load, update and then store the value in a “single” step). However, InterlockedMultiply only ensures that the code wrapped by the lock will only be executed by a thread at a time. Look at that method carefully…can you see a load followed by a store? Those two operations aren’t performed atomically like the one you get through the Interlocked.Increment method!&lt;/p&gt;

&lt;p&gt;There is a solution to this problem, but it involves looping until you get a valid result. Take a look at the method updated to work correctly:&lt;/p&gt;

&lt;pre style="background-color:black;" class="code"&gt;&lt;span style="color:#ff8000;"&gt;public static &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;Int64 &lt;/span&gt;&lt;span style="color:white;"&gt;InterlockedMultiply(&lt;br /&gt;       &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;ref &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;Int64 &lt;/span&gt;&lt;span style="color:white;"&gt;multiplicand, &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;Int64 &lt;/span&gt;&lt;span style="color:white;"&gt;multiplier) {
  &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;Int64 &lt;/span&gt;&lt;span style="color:white;"&gt;result = 0;
  &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;Int64 &lt;/span&gt;&lt;span style="color:white;"&gt;aux = 0;
  &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;do &lt;/span&gt;&lt;span style="color:white;"&gt;{
    aux = multiplicand;
    result = aux * multiplier;
  } &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;while &lt;/span&gt;&lt;span style="color:white;"&gt;( &lt;/span&gt;&lt;span style="color:yellow;"&gt;Interlocked&lt;/span&gt;&lt;span style="color:white;"&gt;.CompareExchange(&lt;br /&gt;                &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;ref &lt;/span&gt;&lt;span style="color:white;"&gt;multiplicand, result, aux) != aux);
  &lt;/span&gt;&lt;span style="color:#ff8000;"&gt;return &lt;/span&gt;&lt;span style="color:white;"&gt;result;
}    &lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;As you can see, we’re using the &lt;a href="http://msdn.microsoft.com/en-us/library/yss0xc74.aspx"&gt;Interlocked.CompareExchange&lt;/a&gt; method to ensure that multiplicand will only be updated if it hasn’t changed during the&amp;#160; execution of that loop. That happens because the &lt;a href="http://msdn.microsoft.com/en-us/library/yss0xc74.aspx"&gt;Interlocked.CompareExchange&lt;/a&gt; method will always return the value that was stored in multiplicand at the time of the call (recall that &lt;a href="http://msdn.microsoft.com/en-us/library/yss0xc74.aspx"&gt;CompareExchange&lt;/a&gt; always returns the original value of the 1st parameter passed to the method at the time of the call).&lt;/p&gt;

&lt;p&gt;As you can see, interlocked operations don’t ensure proper serialization of your code. They only guarantee that the interlocked operation is done atomically. And I guess that’s all for now. Keep tuned for more on multithreading.&lt;/p&gt;</description></item></channel></rss>