title: cost of atomic<int>::fetch_add verses __sync_fetch_and_add
body: <p>I was doing some research on g++ 4.4.6 on linux related to atomics. I had a simple loop that I was using to estimate the time it took to do a fetch_add(1) on an atomic.</p>
  
  <pre><code>atomic&lt;int&gt; ia;
  ia.store(0);
  timespec start,stop;
  clock_gettime(CLOCK_REALTIME, &amp;start);
  while (ia &lt; THE_MAX)
  {
      //++ia;
      ia.fetch_add(1);
  }
  clock_gettime(CLOCK_REALTIME, &amp;stop);
  </code></pre>
  
  <p>I was surprised to find that the following ran in about half the time:</p>
  
  <pre><code>volatile int ia=0;
  timespec start,stop;
  clock_gettime(CLOCK_REALTIME, &amp;start);
  while (ia &lt; THE_MAX)
  {
      __sync_fetch_and_add( &amp;ia, 1 );
  }
  clock_gettime(CLOCK_REALTIME, &amp;stop);
  </code></pre>
  
  <p>I disassembled it - not that I'm very good on x86 assembler - and I see this main difference. The C++11 atomics call generated </p>
  
  <pre><code>call    _ZNVSt9__atomic213__atomic_baseIiE9fetch_addEiSt12memory_order
  </code></pre>
  
  <p>whereas the gcc atomic gave</p>
  
  <pre><code>lock addl   $1, (%eax)
  </code></pre>
  
  <p>I would expect g++ to give me the best option, so I'm thinking there's some serious dropout in my understanding of what is going on. Is it clear to anyone out there why the C++ call didn't generate as good as the gcc atomic call? (Maybe it is just an issue of g++ 4.4 not being very mature...).
  Thanks.</p>