title: std::atomic | compare_exchange_weak vs. compare_exchange_strong
body: <p>I'm unsure if it's me not understanding or the documentation isn't clearly formulated.</p>
  
  <p>The following excerpt has been taken from the newest draft (N3126, section 29.6):</p>
  
  <pre><code>bool atomic_compare_exchange_weak(volatile A* object, C * expected, C desired);
  bool atomic_compare_exchange_weak(A* object, C * expected, C desired);
  bool atomic_compare_exchange_strong(volatile A* object, C * expected, C desired);
  bool atomic_compare_exchange_strong(A* object, C * expected, C desired);
  bool atomic_compare_exchange_weak_explicit(volatile A* object, C * expected, C desired, memory_order success, memory_order failure);
  bool atomic_compare_exchange_weak_explicit(A* object, C * expected, C desired, memory_order success, memory_order failure);
  bool atomic_compare_exchange_strong_explicit(volatile A* object, C * expected, C desired, memory_order success, memory_order failure);
  bool atomic_compare_exchange_strong_explicit(A* object, C * expected, C desired, memory_order success, memory_order failure);
  bool A::compare_exchange_weak(C &amp; expected, C desired, memory_order success, memory_order failure) volatile;
  bool A::compare_exchange_weak(C &amp; expected, C desired, memory_order success, memory_order failure);
  bool A::compare_exchange_strong(C &amp; expected, C desired, memory_order success, memory_order failure) volatile;
  bool A::compare_exchange_strong(C &amp; expected, C desired, memory_order success, memory_order failure);
  bool A::compare_exchange_weak(C &amp; expected, C desired, memory_order order = memory_order_seq_cst) volatile;
  bool A::compare_exchange_weak(C &amp; expected, C desired, memory_order order = memory_order_seq_cst);
  bool A::compare_exchange_strong(C &amp; expected, C desired, memory_order order = memory_order_seq_cst) volatile;
  bool A::compare_exchange_strong(C &amp; expected, C desired, memory_order order = memory_order_seq_cst);
  </code></pre>
  
  <blockquote>
    <p>Remark: The weak compare-and-exchange
    operations may fail spuriously, that
    is, return false while leaving the
    contents of memory pointed to by
    expected before the operation is the
    same that same as that of the object
    and the same as that of expected after
    the operation. [ Note: This  spurious
    failure enables implementation of
    compare-and-exchange on a broader
    class of machines,  e.g., loadlocked
    store-conditional machines. A
    consequence of spurious failure is
    that nearly all uses of weak
    compare-and-exchange will be in a
    loop.</p>
  </blockquote>
  
  <p>So, what does this mean?</p>
  
  <p>Firstly, it 'may' fail spuriously?! Why would it fail? And how do they define 'may'?</p>
  
  <p>Secondly, I still have no idea what's the difference between the functions with "_strong" and "_weak" suffix. Could someone explain the difference?</p>
  
  <p><strong>EDIT:</strong>
  That's what I've found in libstdc++-implementation (atomic_0.h):</p>
  
  <pre><code>bool compare_exchange_weak(
      __integral_type&amp; __i1,
      __integral_type __i2,
      memory_order __m1,
      memory_order __m2
  )
  {
      __glibcxx_assert(__m2 != memory_order_release);
      __glibcxx_assert(__m2 != memory_order_acq_rel);
      __glibcxx_assert(__m2 &lt;= __m1);
      return _ATOMIC_CMPEXCHNG_(this, &amp;__i1, __i2, __m1);
  }
  
  bool compare_exchange_strong(
      __integral_type&amp; __i1,
      __integral_type __i2,
      memory_order __m1,
      memory_order __m2
  )
  {
      __glibcxx_assert(__m2 != memory_order_release);
      __glibcxx_assert(__m2 != memory_order_acq_rel);
      __glibcxx_assert(__m2 &lt;= __m1);
      return _ATOMIC_CMPEXCHNG_(this, &amp;__i1, __i2, __m1);
  }
  </code></pre>