title: How to use gcc atomic builtins?
body: <p>In my program I was trying to update a value from multiple threads. I know how to use mutex (<code>pthread_mutex_lock(), pthread_mutex_unlock()</code>) to do this, but I just learned about gcc atomic builtins so I would like to have a try.</p>
  
  <pre><code>shared value A;
  void each_working_thread() {
      thread local variable B;
      if (is_valid(A,B))
          __sync_sub_and_fetch(A,B);
      else 
          throw error;
  }
  </code></pre>
  
  <p>where <code>is_valid()</code> is a const function that return boolean. </p>
  
  <p>Is this correct, or the value of A could be updated by another thread during <code>is_valid()</code> ? </p>