title: Do I need to synchronize thread access to an int?
body: <p>I've just written a method that is called by multiple threads simultaneously and I need to keep track of when all the threads have completed. The code uses this pattern:</p>
  
  <pre><code>private void RunReport()
  {
     _reportsRunning++;
  
     try
     {
         //code to run the report
     }
     finally
     {
         _reportsRunning--;
     }
  }
  </code></pre>
  
  <p>This is the only place within the code that <code>_reportsRunning</code>'s value is changed, and the method takes about a second to run.</p>
  
  <p>Occasionally when I have more than six or so threads running reports together the final result for _reportsRunning can get down to -1. If I wrap the calls to <code>_runningReports++</code> and <code>_runningReports--</code> in a lock then the behaviour appears to be correct and consistent.</p>
  
  <p>So, to the question: When I was learning multithreading in C++ I was taught that you didn't need to synchronize calls to increment and decrement operations because they were always one assembly instruction and therefore it was impossible for the thread to be switched out mid-call. Was I taught correctly, and if so, how come that doesn't hold true for C#?</p>