What is the correct and portable way to ensure true sharing in a struct small enough to fit in a cacheline? Is it enough to just ensure that the struct is small enough? Or does it also have to be aligned on the cache boundary?
For example, assuming the size of a cacheline is 64 bytes, is the following enough?
struct A {
std::uint32_t one;
std::uint32_t two;
};
Or do I have to do this?
struct alignas(std::hardware_constructive_interference_size) A {
std::uint32_t one;
std::uint32_t two;
};
Note: This will always be on the stack, so no over-aligned memory allocations should be required.
Another followup, is this enough to ensure no false-sharing?
struct A {
public:
alignas(hardware_destructive_interference_size) std::uint32_t one;
alignas(hardware_constructive_interference_size) std::uint32_t two;
};
or does one have to do this (in the case where say hardware_constructive_interference_size
< hardware_destructive_interference_size
?)
struct A {
public:
alignas(hardware_destructive_interference_size) std::uint32_t one;
alignas(hardware_destructive_interference_size) std::uint32_t two;
};