Juce::Atomic <> alignment

from class Atomic:

Is this necessary even for a 4-byte value?

Possibly not, but it may be needed for 64-bit values, so I erred on the side of caution.

What are your thoughts on

JUCE_ALIGN (sizeof(Type));


I’m doing:

template <class Elem, class Tag>
struct List
  class Node : NonCopyable
    Node() { }

    Atomic <Node*> m_next;

Where list elements derive from List::Node

TBH I’d be surprised if it made much difference in that case, as the object would be allocated on the heap, so almost certainly aligned to at least 8 bytes anyway.

I have objects accessed from multiple threads that can live in several lists at the same time (through the tag parameter). For example:


struct Tag_gui {}; // dummy structure to distinguish Node types

struct Track : List<Track,Tag_gui>::Node,

This is just an example…the real list of base members is actually much larger (lol)

Well, I can’t really think of any objections to aligning it to sizeof(Type)…

This is what I found:

It doesn’t really say definitively. All I can think of is the possibility of a 64-bit system (Itanium perhaps?) that requires even 32-bit atomic operations to be quad-word aligned.

It would be cool if there was some compiler-specific typedef or intrinsic that supplied this information depending on the target system.

BTW, I tried sizeof (Type), but actually it doesn’t work - the pragma can only take a literal constant.

Hmm…I see what you mean. Well if you think it’s worth it, and all Juce platforms support partial class template specialization, this could get some mileage although there is a minor issue with using value in the ctor-initializer list:

template <typename Type, int Bytes>
struct AtomicAlignedValue;

template <typename Type>
struct AtomicAlignedValue <Type, 4>
  volatile Type value;

template <typename Type>
struct AtomicAlignedValue <Type, 8>
  volatile Type value;

template <typename Type>
class Atomic : public AtomicAlignedValue <Type, sizeof (Type)>