title: Relative performance of swap vs compare-and-swap locks on x86
body: <p>Two common locking idioms are:</p>
  
  <pre><code>if (!atomic_swap(lockaddr, 1)) /* got the lock */
  </code></pre>
  
  <p>and:</p>
  
  <pre><code>if (!atomic_compare_and_swap(lockaddr, 0, val)) /* got the lock */
  </code></pre>
  
  <p>where <code>val</code> could simply be a constant or an identifier for the new prospective owner of the lock.</p>
  
  <p>What I'd like to know is whether there tends to be any significant performance difference between the two on x86 (and x86_64) machines. I know this is a fairly broad question since the answer might vary a lot between individual cpu models, but that's part of the reason I'm asking SO rather than just doing benchmarks on a few cpus I have access to.</p>