title: Thread synchronization. Why exactly this lock isn't enough to synchronize threads
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/7131979/threads-synchronization-how-exactly-lock-makes-access-to-memory-correct">Threads synchronization. How exactly lock makes access to memory &#39;correct&#39;?</a>  </p>
  </blockquote>
  
  
  
  <p>This question is inspired by <a href="https://stackoverflow.com/questions/7131979/threads-synchronization-how-exactly-lock-makes-access-to-memory-correct">this one.</a></p>
  
  <p>We got a following test class</p>
  
  <pre><code>class Test
  {
      private static object ms_Lock=new object();
      private static int ms_Sum = 0;
  
      public static void Main ()
      {
          Parallel.Invoke(HalfJob, HalfJob);
          Console.WriteLine(ms_Sum);
          Console.ReadLine();
      }
  
      private static void HalfJob()
      {
          for (int i = 0; i &lt; 50000000; i++) {
              lock(ms_Lock) { }// empty lock
  
              ms_Sum += 1;
          }
      }
  }
  </code></pre>
  
  <p>Actual result is very close to expected value 100 000 000 (50 000 000 x 2, since 2 loops are running at the same time), with around 600 - 200 difference (mistake is approx 0.0004% on my machine which is very low). No other way of synchronization can provide such way of approximation (its either a much bigger mistake % or its 100% correct)</p>
  
  <p>We currently understand that such level of preciseness is because of program runs in the following way:</p>
  
  <p><img src="https://i.stack.imgur.com/V1yTq.png" alt="enter image description here"></p>
  
  <p>Time is running left to right, and 2 threads are represented by two rows.</p>
  
  <p>where</p>
  
  <ul>
  <li><p>black box represents process of acquiring, holding and releasing the</p></li>
  <li><p>lock plus represents addition operation ( schema represents scale on 
  my PC, lock takes approximated 20 times longer than add)</p></li>
  <li>white box    represents period that consists of try to acquire lock,
  and further    awaiting for it to become available</li>
  </ul>
  
  <p>Also lock provides full memory fence.</p>
  
  <p>So the question now is: if above schema represents what is going on, what is the cause of such big error (now its big cause schema looks like very strong syncrhonization schema)? We could understand difference between 1-10 on boundaries, but its clearly no the only reason of error? We cannot see when writes to ms_Sum can happen at the same time, to cause the error.</p>
  
  <p><strong>EDIT:</strong> many people like to jump to quick conclusions. I know what synchronization is, and that above construct is not a real or close to good way to synchronize threads if we need correct result. Have some faith in poster or maybe read linked answer first. I don't need a way to synchronize 2 threads to perform additions in parallel, I am exploring this extravagant and yet efficient , compared to <em>any</em> possible and <em>approximate</em> alternative, synchronization construct (it does synchronize to some extent so its <strong>not meaningless</strong> like suggested)  </p>