title: Interlocked.Increment and return of incremented value
body: <p>We have a method which maintains global sequence index of all events in our application. As it is website, it is expected to have such method thread safe. Thread-safe implementation was following:</p>
  
  <pre><code>private static long lastUsedIndex = -1;
  
  public static long GetNextIndex()
  {
      Interlocked.Increment(ref lastUsedIndex);
      return lastUsedIndex;
  }
  </code></pre>
  
  <p>However we noticed that under some not heavy load duplicated indexes appeared in system. Simple test shown that there are about of 1500 duplicates for 100000 iterations.</p>
  
  <pre><code>internal class Program
  {
      private static void Main(string[] args)
      {
          TestInterlockedIncrement.Run();
      }
  }
  
  internal class TestInterlockedIncrement
  {
      private static long lastUsedIndex = -1;
  
      public static long GetNextIndex()
      {
          Interlocked.Increment(ref lastUsedIndex);
          return lastUsedIndex;
      }
  
      public static void Run()
      {
          var indexes = Enumerable
              .Range(0, 100000)
              .AsParallel()
              .WithDegreeOfParallelism(32)
              .WithExecutionMode(ParallelExecutionMode.ForceParallelism)
              .Select(_ =&gt; GetNextIndex())
              .ToList();
  
          Console.WriteLine($"Total values: {indexes.Count}");
          Console.WriteLine($"Duplicate values: {indexes.GroupBy(i =&gt; i).Count(g =&gt; g.Count() &gt; 1)}");
      }
  }
  </code></pre>
  
  <p>This can be fixed with following implementation:</p>
  
  <pre><code>public static long GetNextIndex()
  {
      return Interlocked.Increment(ref lastUsedIndex);
  }
  </code></pre>
  
  <p>However, I do not clearly understand, why first implementation did not work. Can anybody help me describing what is happening in that case?</p>