title: Atomicity in C++ : Myth or Reality
body: <p>I have been reading an article about <a href="http://msdn.microsoft.com/en-us/library/ee418650%28v=vs.85%29.aspx">Lockless Programming</a> in MSDN. It says :  </p>
  
  <blockquote>
    <p>On all modern processors, you can
    assume that reads and writes of
    <strong>naturally aligned native types are atomic</strong>. As long as the memory bus is
    at least as wide as the type being
    read or written, the CPU reads and
    writes these types in a single bus
    transaction, making it impossible for
    other threads to see them in a
    half-completed state.</p>
  </blockquote>
  
  <p>And it gives some examples: </p>
  
  <pre><code>// This write is not atomic because it is not natively aligned.
  DWORD* pData = (DWORD*)(pChar + 1);
  *pData = 0;
  
  // This is not atomic because it is three separate operations.
  ++g_globalCounter;
  
  // This write is atomic.
  g_alignedGlobal = 0;
  
  // This read is atomic.
  DWORD local = g_alignedGlobal;
  </code></pre>
  
  <p>I read lots of answers and comments saying, nothing is guaranteed to be atomic in C++ and it is not even mentioned in standarts, in SO and now I am a bit confused. Am I misinterpreting the article? Or does the article writer talk about things that are non-standart and specific to MSVC++ compiler?</p>
  
  <p>So according to the article the below assignments must be atomic, right?</p>
  
  <pre><code>struct Data
  {
      char ID;
      char pad1[3];
      short Number;
      char pad2[2];
      char Name[5];
      char pad3[3];
      int Number2;
      double Value;
  } DataVal;
  
  DataVal.ID = 0;
  DataVal.Number = 1000;
  DataVal.Number2 = 0xFFFFFF;
  DataVal.Value = 1.2;
  </code></pre>
  
  <p>If it is true, does replacing <code>Name[5]</code> and <code>pad3[3]</code> with <code>std::string Name;</code> make any difference in memory-alignment ? Will the assignments to <code>Number2</code> and <code>Value</code> variables be still atomic?</p>
  
  <p>Can someone please explain?</p>