title: C++ atomics: how to allow only a single thread to access a function?
body: <p>I'd like to write a function that is accessible only by a single thread at a time. I don't need busy waits, a brutal 'rejection' is enough if another thread is already running it. This is what I have come up with so far:</p>
  <pre><code>std::atomic&lt;bool&gt; busy (false);
  
  bool func()
  {
      if (m_busy.exchange(true) == true)
          return false;  
  
      // ... do stuff ...
  
      m_busy.exchange(false);
      return true;
  }
  </code></pre>
  <ol>
  <li>Is the logic for the atomic exchange correct?</li>
  <li>Is it correct to mark the two atomic operations as <code>std::memory_order_acq_rel</code>? As far as I understand a relaxed ordering (<code>std::memory_order_relaxed</code>) wouldn't be enough to prevent reordering.</li>
  </ol>