title: InterlockedCompareExchange64 vs std::atomic compare_exchange
body: <p>How is <code>std::atomic&lt;&gt;</code> <a href="http://en.cppreference.com/w/cpp/atomic/atomic/compare_exchange" rel="nofollow noreferrer">compare_exchange</a>() related and translated to/from MS <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms683562(v=vs.85).aspx" rel="nofollow noreferrer">InterlockedCompareExchange64</a> on a non-atomic value? Are there performance or semantics differences?</p>
  
  <p>For example, are these codes equivalent?</p>
  
  <p><strong><em>edit</em></strong> : David Haim pointed out that it should have been <code>int64_t atomic_inc_ms(volatile int64_t&amp; val, int64_t less_than)</code></p>
  
  <pre><code>int64_t atomic_inc_ms(int64_t&amp; val, int64_t less_than) {
   int64_t new_val;
   int64_t old_val = val; 
   while(true)
   {
     if (old_val &gt; less_than) return old_val;
     new_val = old_val + 1;
     int64_t got_val = InterlockedCompareExchange64(&amp;val,new_val,old_val);
     if(got_val == old_val) break;
     old_val = got;
   }
  
   return new_val;
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>int64_t atomic_inc(std::atomic&lt;int64_t&gt;&amp; val, int64_t less_than) {
   int64_t new_val;
   int64_t old_val = val.load();
   do
   {
     if (old_val &gt; less_than) return old_val;
     new_val = old_val + 1;
   } while (!val.compare_exchange_weak(old_val, new_val));
  
   return new_val;
  }
  </code></pre>
  
  <p>A concern is the line <code>int64_t old_val = val;</code> with no explicit atomic load.</p>
  
  <p>The example is that of <a href="https://stackoverflow.com/questions/13778110/is-there-atomic-increment-with-the-check-preconditions-that-the-atomic-value-wa">Is there atomic increment with the check preconditions, that the atomic value was less than the specified value?</a> and is close to the text book example on how to use <code>compare_exchange_weak</code>.</p>
  
  <p>Are <code>compare_exchange_weak</code> and <code>compare_exchange</code>, semantically equivalent to
  <code>InterlockedCompareExchangeAcquire64</code> and <code>InterlockedCompareExchange64</code> ?</p>