title: Is this the correct way to atomically read and write a bool?
body: <p>A boolean flag is toggled by two threads. Does the following code make sense?</p>
  
  <pre><code>static bool ATOMIC_BOOL_READ( volatile bool&amp; var )
  {
      return __sync_fetch_and_or(&amp;var, 0);
  }
  
  static void ATOMIC_BOOL_WRITE(volatile bool&amp; var, bool newval )
  {
      __sync_bool_compare_and_swap( &amp;var, !newval, newval);
  }
  </code></pre>
  
  <p>Note a few things:</p>
  
  <ul>
  <li><p>I am passing in a bool reference. Make sense?</p></li>
  <li><p>For kick's sake, I am also declaring it volatile. </p></li>
  <li>The functions are static</li>
  </ul>
  
  <p>Update:</p>
  
  <p>The fundamental question I want to ask is:
  Whats the difference between atomicity and memory barrier? If Thread A is executing an <em>atomic builtin</em> on variable foo, then Thread B cannot do ANYTHING on variable foo; hence creating a memory barrier?</p>