title: Alignment of atomic variables
body: <p>Naturally, in order for a typical modern processor architecture (like x86_64) to perform an atomic load or store, the data to be read/written needs to be aligned.</p>
  
  <p>But how is this requirement actually realized/enforced via C++11 <code>&lt;atomic&gt;</code> variables?</p>
  
  <p>Suppose I have an architecture that supports 16-byte compare and swap (double-word CAS), so it can atomically read/write 16-byte values, and I define a 16-byte type:</p>
  
  <pre><code>struct double_word
  {
      std::uint64_t x;
      std::uint64_t y;
  };
  </code></pre>
  
  <p>Now, suppose I include an <code>std::atomic&lt;double_word&gt;</code> as a member field of a class:</p>
  
  <pre><code>class foo
  {
      public:
  
      std::atomic&lt;double_word&gt; dword;
  };
  </code></pre>
  
  <p>How do I know <code>foo::dword</code> is actually aligned on a 16-byte boundary?  How do I know a call to <code>dword.load()</code> would actually <em>be</em> atomic?</p>
  
  <p>Actually, I originally starting asking this question because of an odd thing that happened when I added another data member before <code>foo::dword</code>.  I defined <code>foo</code> as:</p>
  
  <pre><code>class foo
  {
      public:
  
      std::uint64_t x;
      std::atomic&lt;double_word&gt; dword; 
  };
  </code></pre>
  
  <p>When I actually perform an atomic load on <code>foo::dword</code>, and compile and run using GCC 4.7.2 on an x86_64 machine running Debian Linux, it actually gives me a Segmentation Fault!</p>
  
  <p>Full program:</p>
  
  <pre><code>#include &lt;atomic&gt;
  #include &lt;cstdint&gt;
  
      struct double_word
      {
          std::uint64_t x;
          std::uint64_t y;
      };
  
      class foo
      {
          public:
  
          std::uint64_t x;
          std::atomic&lt;double_word&gt; dword; // &lt;-- not aligned on 16-byte boundary
      };
  
      int main()
      {
          foo f;
          double_word d = f.dword.load(); // &lt;-- segfaults with GCC 4.7.2 !!
      }
  </code></pre>
  
  <p>This actually segfaults on <code>f.dword.load()</code>.  At first I didn't understand why, but then I realized that <code>dword</code> is <strong><em>not</em></strong> aligned on a 16-byte boundary.  So, this leads to a lot of questions like: what should the compiler do if an atomic variable is not aligned and we try to atomically load it?  Is it undefined behavior?  Why did the program simply segfault?</p>
  
  <p>Secondly, what does the C++11 standard say about this?  Should the compiler make sure that <code>double_word</code> is automatically aligned on a 16-byte boundary?  If so, does that mean GCC is simply buggy here?  If not - it would seem it is up to the user to ensure alignment, in which case <em>any</em> time we use an <code>std::atomic&lt;T&gt;</code> larger than one byte, it would seem we'd <em>have</em> to use <code>std::aligned_storage</code> to ensure it is properly aligned, which (A) seems cumbersome, and (B) is something I've never actually seen done in practice or in any examples/tutorials.</p>
  
  <p>So, how should a programmer using C++11 <code>&lt;atomic&gt;</code> handle alignment issues like this?</p>