title: Is comparing 2 int's an atomic operation?
body: <p>I'm writing a very simple atomic counter, like</p>
  
  <pre><code>class counter
  {
      /* ... */
  
      int increment()
      {
          return __sync_add_and_fetch( &amp;counter_, 1 );
      }
      int decrement()
      {
          return __sync_sub_and_fetch( &amp;counter_, 1 );
      }
      bool operator==( int val ) // const
      {
          return __sync_bool_compare_and_swap( &amp;counter_, val, counter_ );
      }
  
  private:
      volatile int counter_;
  };
  </code></pre>
  
  <p>Can I use <code>return counter_ == val;</code> inside my <code>operator==</code> (shorter and will be able to uncomment the <code>const</code> qualifier)? Or I should use <code>__sync_bool_compare_and_swap</code> instead?</p>
  
  <hr>
  
  <p><strong>Edit:</strong> So, I'm more confused than in the beginning. After reading the comments and answers, I think leaving the "compare-and-swap" is more adequate. Is it?</p>
  
  <p><strong>Edit 2:</strong> I get rid of this <code>operator==</code>, but I'm still curious. I'll wait and hope for a bit more activity on this question. Thanks.</p>