title: Porting InterlockedExchange, using GCC intrinsics only
body: <p>Windows API offers <code>InterlockedExchange</code>, which sets a value in memory atomically. Using only GCC intrinsics, I’d like to create an equivalent of that function. Would setting the value and then calling a memory barrier be sufficient (see the code below) ? </p>
  
  <pre><code>template &lt;typename T&gt;
  T InterlockedExchange(volatile T&amp; _data, T _value)
  {
      const T oldValue = _data;
      _data = _value;
      __sync_synchronize();
      return oldValue;
  }
  </code></pre>
  
  <p>Thank you.</p>
  
  <p><strong>EDIT</strong>: The proposed snippet is <em>NOT</em> a correct solution to the problem, as it is clearly not atomic (but, well, I had to give a try at least).</p>