title: atomic increment and return counter
body: <p>Trying to make a unique id generating function, and came up with this:</p>
  
  <pre><code>std::atomic&lt;int&gt; id{0};
  int create_id() {
      id++;
      return id.load();
  }
  </code></pre>
  
  <p>But I assume it's possible for that function to return the same value twice, right? For example, thread A calls the function, increments the value, but then halts while thread B comes in and also increments the value, finally A and B both return the same value.</p>
  
  <p>So using mutexes, the function might look like this:</p>
  
  <pre><code>std::mutex mx;
  int id = 0;
  int create_id() {
      std::lock_guard&lt;std::mutex&gt; lock{mx};
      return id++;
  }
  </code></pre>
  
  <p>My question: Is it possible to create the behavior of spawning unique int values from a counter using only atomics? The reason I'm asking is because I need to spawn a lot of id's, but read that mutex is slow.</p>