title: Understanding std::atomic::compare_exchange_weak() in C++11
body: <pre><code>bool compare_exchange_weak (T&amp; expected, T val, ..);
  </code></pre>
  <p><code>compare_exchange_weak()</code> is one of compare-exchange primitives provided in C++11. It's <em><strong>weak</strong></em> in the sense that it returns false even if the value of the object is equal to <code>expected</code>. This is due to <em><strong>spurious failure</strong></em> on some platforms where a sequence of instructions (instead of one as on x86) are used to implement it. On such platforms, context switch, reloading of the same address (or cache line) by another thread, etc can fail the primitive. It's <code>spurious</code> as it's not the value of the object (not equal to <code>expected</code>) that fails the operation. Instead, it's kind of timing issues.</p>
  <p>But what puzzles me is what's said in C++11 Standard (ISO/IEC 14882),</p>
  <blockquote>
  <p>29.6.5
  ..
  A consequence of spurious failure is that nearly all uses of weak
  compare-and-exchange will be in a loop.</p>
  </blockquote>
  <p>Why does it have to be in a loop in <em><strong>nearly all uses</strong></em> ? Does that mean we shall loop when it fails because of spurious failures? If that's the case, why do we bother use <code>compare_exchange_weak()</code> and write the loop ourselves? We can just use <code>compare_exchange_strong()</code> which I think should get rid of spurious failures for us. What are the common use cases of <code>compare_exchange_weak()</code>?</p>
  <p>Another question related. In his book &quot;C++ Concurrency In Action&quot; Anthony says,</p>
  <pre><code>//Because compare_exchange_weak() can fail spuriously, it must typically
  //be used in a loop:
  
  bool expected=false;
  extern atomic&lt;bool&gt; b; // set somewhere else
  while(!b.compare_exchange_weak(expected,true) &amp;&amp; !expected);
  
  //In this case, you keep looping as long as expected is still false,
  //indicating that the compare_exchange_weak() call failed spuriously.
  </code></pre>
  <p>Why is <code>!expected</code> there in the loop condition? Does it there to prevent that all threads may starve and make no progress for some time?</p>
  <p><strong>One last question</strong></p>
  <p>On platforms that no single hardware CAS instruction exist, both the weak and strong version are implemented using LL/SC (like ARM, PowerPC, etc). So is there any difference between the following two loops? Why, if any? (To me, they should have similar performance.)</p>
  <pre><code>// use LL/SC (or CAS on x86) and ignore/loop on spurious failures
  while (!compare_exchange_weak(..))
  { .. }
  
  // use LL/SC (or CAS on x86) and ignore/loop on spurious failures
  while (!compare_exchange_strong(..)) 
  { .. }
  </code></pre>
  <p>I come up w/ this last question you guys all mention that there maybe a performance difference inside a loop. It's also mentioned by the C++11 Standard (ISO/IEC 14882):</p>
  <blockquote>
  <p>When a compare-and-exchange is in a loop, the weak version will yield
  better performance on some platforms.</p>
  </blockquote>
  <p>But as analyzed above, two versions in a loop should give the same/similar performance. What's the thing I miss?</p>