Is there an attribute to enforce padding after a variable?
I have a volatile (non-cached) variable declared as such:
volatile int foo __attribute__((aligned(CACHE_LINE_SIZE));
I would like to prevent other variables from being allocated to the same cache line, to avoid coherency issues.
I can add a padding variable after foo, or set the __attribute__((aligned(CACHE_LINE_SIZE))
to the following variable in the same compilation unit. However I was wondering whether there is a cleaner way to do this, such as adding an attribute to variable foo
itself to enforce padding.
struct { int foo; char padding[CACHE_LINE_SIZE - sizeof(int)]; }
? – Berkleystd::aligned_storage
do what you want ? – Reseunion { int foo; char size[CACHE_LINE_SIZE]; }
looks even better. – Berkleystd::aligned_storage
has been deprecated in C++23. – Sorehead