title: Why is std::mutex so slow on OSX?
body: <p>I have the following benchmark: <a href="https://gist.github.com/leifwalsh/10010580" rel="noreferrer">https://gist.github.com/leifwalsh/10010580</a></p>
  
  <p>Essentially it spins up <code>k</code> threads and then each thread does about 16 million / <code>k</code> lock/increment/unlock cycles, using a spinlock and a <code>std::mutex</code>.  On OSX, the <code>std::mutex</code> is devastatingly slower than the spinlock when contended, whereas on Linux it's competitive or a bit faster.</p>
  
  <p>OSX:</p>
  
  <pre><code>spinlock 1:     334ms
  spinlock 2:     3537ms
  spinlock 3:     4815ms
  spinlock 4:     5653ms
  std::mutex 1:   813ms
  std::mutex 2:   38464ms
  std::mutex 3:   44254ms
  std::mutex 4:   47418ms
  </code></pre>
  
  <p>Linux:</p>
  
  <pre><code>spinlock 1:     305ms
  spinlock 2:     1590ms
  spinlock 3:     1820ms
  spinlock 4:     2300ms
  std::mutex 1:   377ms
  std::mutex 2:   1124ms
  std::mutex 3:   1739ms
  std::mutex 4:   2668ms
  </code></pre>
  
  <p>The processors are different, but not <em>that</em> different (OSX is Intel(R) Core(TM) i7-2677M CPU @ 1.80GHz, Linux is Intel(R) Core(TM) i5-2500K CPU @ 3.30GHz), this seems like a library or kernel problem.  Anyone know the source of the slowness?</p>
  
  <p>To clarify my question, I understand that "there are different mutex implementations that optimize for different things and this isn't a problem, it's expected".  This question is: what are the actual differences in implementation that cause this?  Or, if it's a hardware issue (maybe the cache is just a lot slower on the macbook), that's acceptable too.</p>