title: C++11 when increment the atomic variable, and assign it to other value, is it atomic operation?
body: <p>i'm confused about the atomic operation on c++11,</p>
  
  <p>i know the atomic variable self increment is atomic operation,</p>
  
  <p>but i use the assignment to other value, just doubt it. </p>
  
  <p>the code just like:</p>
  
  <pre><code>//....
  static std::atomic&lt;int&gt; i; // global variable
  //....
  // in the thread
  int id = ++i; 
  </code></pre>
  
  <p>when using the assignment at different threads, is the id unique value? </p>
  
  <p>the test code:</p>
  
  <pre><code>#include &lt;thread&gt;
  #include &lt;mutex&gt;
  #include &lt;atomic&gt;
  #include &lt;iostream&gt;
  
  class A {
  public:
      static int idGenerator;
      static std::mutex m;
      A () {
          // i know this operation will keep the id_ is unique
          std::lock_guard&lt;std::mutex&gt; lock(m);
          id_ = ++idGenerator; 
      }
     void F(std::string name) {
           std::cout &lt;&lt; name &lt;&lt; " " &lt;&lt; id_ &lt;&lt; std::endl;
      }
  private:
      int id_;
  };
  int A::idGenerator = 0;
  std::mutex A::m;
  
  class B {
  public:
      static int idGenerator;
      B () {
          // after self increment, is the assignment atomic? 
          id_ = (++idGenerator);
      }
     void F(std::string name) {
           std::cout &lt;&lt; name &lt;&lt; " " &lt;&lt; id_.load() &lt;&lt; std::endl;
      }
  private:
      std::atomic&lt;int&gt; id_;
  };
  int B::idGenerator = 0;
  
  
  void funcA() {
      A a2;
      a2.F("a2");
  }
  
  void funcB() {
      B b2;
      b2.F("b2");
  }
  
  int main() {
      A a1;
      B b1;
      std::thread t1(&amp;funcA);
      std::thread t2(&amp;funcB);
      a1.F("a1");
      b1.F("b1");
  
      t1.join();
      t2.join();
      return 0;
  }
  </code></pre>
  
  <p>there are three threads,</p>
  
  <p>A class use lock_guard keep unique.</p>
  
  <p>B class just use atomic operation, and assign to the variable</p>