title: Unexpected result when two threads accessing a global variable
body: <p>I have two threads, t1 and t2.<br>
  They both do add operation on a global variable named 'count', which was initialized with 0.<br>
  t1 does <code>count++</code> and t2 does <code>count+=100</code>. I start t1 first then t2, but the output result doesn't meet my expectation.
  Is there I misunderstand something?</p>
  
  <p>It won't help even though I used lock() in two threads. </p>
  
  <p>Here's the C# code:</p>
  
  <pre><code>private int count = 0;
  private object locker = new object();
  
  void run()
  {
      var t1 = new Thread(Add_1);
      var t2 = new Thread(Add_2);
      t1.Start();
      t2.Start();
  }
  </code></pre>
  
  <pre><code>void Add_1()
  {
      lock(locker)
      {
          count++;
          Console.WriteLine(count);
      }
  }
  
  void Add_2()
  {
      lock(locker)
      {
          count += 100;
          Console.WriteLine(count);
      }
  }
  </code></pre>
  
  <p>It sometimes prints</p>
  
  <blockquote>
    <p>1<br>
    101</p>
  </blockquote>
  
  <p>or</p>
  
  <blockquote>
    <p>100<br>
    101</p>
  </blockquote>
  
  <p>I don't have any idea of that. In my opinion, t1 should have the locker until it finished its job. But it seems that t2 has chance to add count earlier than t1.</p>
  
  <p>Hope someone can give me a hand, thanks.</p>