title: What is the recommended way to align memory in C++11
body: <p>I am working on a single producer single consumer ring buffer implementation.I have two requirements:</p>
  <ol>
  <li>Align a single heap allocated instance of a ring buffer to a cache line.</li>
  <li>Align a field within a ring buffer to a cache line (to prevent false sharing).</li>
  </ol>
  <p>My class looks something like:</p>
  <pre><code>#define CACHE_LINE_SIZE 64  // To be used later.
  
  template&lt;typename T, uint64_t num_events&gt;
  class RingBuffer {  // This needs to be aligned to a cache line.
  public:
    ....
  
  private:
    std::atomic&lt;int64_t&gt; publisher_sequence_ ;
    int64_t cached_consumer_sequence_;
    T* events_;
    std::atomic&lt;int64_t&gt; consumer_sequence_;  // This needs to be aligned to a cache line.
  
  };
  </code></pre>
  <p>Let me first tackle point 1 i.e. <strong>aligning a single heap allocated instance</strong> of the class. There are a few ways:</p>
  <ol>
  <li><p>Use the c++ 11 <code>alignas(..)</code> specifier:</p>
  <pre><code>template&lt;typename T, uint64_t num_events&gt;
  class alignas(CACHE_LINE_SIZE) RingBuffer {
  public:
    ....
  
  private:
    // All the private fields.
  
  };
  </code></pre>
  </li>
  <li><p>Use <code>posix_memalign(..)</code> + placement <code>new(..)</code> without altering the class definition. This suffers from not being platform independent:</p>
  <pre><code>void* buffer;
  if (posix_memalign(&amp;buffer, 64, sizeof(processor::RingBuffer&lt;int, kRingBufferSize&gt;)) != 0) {
      perror(&quot;posix_memalign did not work!&quot;);
      abort();
  }
  // Use placement new on a cache aligned buffer.
  auto ring_buffer = new(buffer) processor::RingBuffer&lt;int, kRingBufferSize&gt;();
  </code></pre>
  </li>
  <li><p>Use the GCC/Clang extension <code>__attribute__ ((aligned(#)))</code></p>
  <pre><code>template&lt;typename T, uint64_t num_events&gt;
  class RingBuffer {
  public:
    ....
  
  private:
    // All the private fields.
  
  } __attribute__ ((aligned(CACHE_LINE_SIZE)));
  </code></pre>
  </li>
  <li><p>I tried to use the C++ 11 standardized <code>aligned_alloc(..)</code> function instead of <code>posix_memalign(..)</code> but GCC 4.8.1 on Ubuntu 12.04 could not find the definition in <code>stdlib.h</code></p>
  </li>
  </ol>
  <p>Are all of these guaranteed to do the same thing? My goal is cache-line alignment so any method that has some limits on alignment (say double word) will not do. Platform independence which would point to using the standardized <code>alignas(..)</code> is a secondary goal.</p>
  <p>I am not clear on whether <code>alignas(..)</code> and <code>__attribute__((aligned(#)))</code> have some limit which could be below the cache line on the machine. I can't reproduce this any more but while printing addresses I think I did not always get 64 byte aligned addresses with <code>alignas(..)</code>. On the contrary <code>posix_memalign(..)</code> seemed to always work. Again I cannot reproduce this any more so maybe I was making a mistake.</p>
  <p>The second aim is to <strong>align a field within a class/struct</strong> to a cache line. I am doing this to prevent false sharing. I have tried the following ways:</p>
  <ol>
  <li><p>Use the C++ 11 <code>alignas(..)</code> specifier:</p>
  <pre><code>template&lt;typename T, uint64_t num_events&gt;
  class RingBuffer {  // This needs to be aligned to a cache line.
    public:
    ...
    private:
      std::atomic&lt;int64_t&gt; publisher_sequence_ ;
      int64_t cached_consumer_sequence_;
      T* events_;
      std::atomic&lt;int64_t&gt; consumer_sequence_ alignas(CACHE_LINE_SIZE);
  };
  </code></pre>
  </li>
  <li><p>Use the GCC/Clang extension <code>__attribute__ ((aligned(#)))</code></p>
  <pre><code>template&lt;typename T, uint64_t num_events&gt;
  class RingBuffer {  // This needs to be aligned to a cache line.
    public:
    ...
    private:
      std::atomic&lt;int64_t&gt; publisher_sequence_ ;
      int64_t cached_consumer_sequence_;
      T* events_;
      std::atomic&lt;int64_t&gt; consumer_sequence_ __attribute__ ((aligned (CACHE_LINE_SIZE)));
  };
  </code></pre>
  </li>
  </ol>
  <p>Both these methods seem to align <code>consumer_sequence</code> to an address 64 bytes after the beginning of the object so whether <code>consumer_sequence</code> is cache aligned depends on whether the object itself is cache aligned. Here my question is - are there any better ways to do the same?</p>
  <p><strong>EDIT:</strong></p>
  <p>The reason <code>aligned_alloc</code> did not work on my machine was that I was on eglibc 2.15 (Ubuntu 12.04). It worked on a later version of eglibc.</p>
  <p>From the <a href="http://linux.die.net/man/3/posix_memalign" rel="nofollow noreferrer">man page</a>: <em>The function <code>aligned_alloc()</code> was added to glibc in version 2.16</em>.</p>
  <p>This makes it pretty useless for me since I cannot require such a recent version of eglibc/glibc.</p>