Date: Wed, 1 Apr 2026 12:48:45 +0100
I wasn't sure if this would be something for Boost, but then I was thinking
that the compiler writers would know more about the target architecture. So
maybe it belongs in the C++ standard library rather than Boost.
If we had a wrapper class to force at most one object to a cache line, the
most naive implementation could be something like:
template<typename T>
class alignas(std::hardware_destructive_interference_size) cache_isolated :
public T {
static constexpr std::size_t len = std::hardware_destructive_
interference_size;
std::array<char unsigned, (len - (sizeof(T) % len)) % len>
cache_line_padding;
public:
template<typename... Params>
cache_isolated(Params&&... args) : T{ std::forward<Params>(args)... } {}
};
Of course if 'T' is a class marked 'final' then inheritance wouldn't work
here, and it would need to be a sub-object instead.
And then individual compilers could provide more specific optimised
implementations.
that the compiler writers would know more about the target architecture. So
maybe it belongs in the C++ standard library rather than Boost.
If we had a wrapper class to force at most one object to a cache line, the
most naive implementation could be something like:
template<typename T>
class alignas(std::hardware_destructive_interference_size) cache_isolated :
public T {
static constexpr std::size_t len = std::hardware_destructive_
interference_size;
std::array<char unsigned, (len - (sizeof(T) % len)) % len>
cache_line_padding;
public:
template<typename... Params>
cache_isolated(Params&&... args) : T{ std::forward<Params>(args)... } {}
};
Of course if 'T' is a class marked 'final' then inheritance wouldn't work
here, and it would need to be a sub-object instead.
And then individual compilers could provide more specific optimised
implementations.
Received on 2026-04-01 11:48:48
