gpt4 book ai didi

c++ - std::hardware_destructive_interference_size 的可靠性

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:20:24 26 4
gpt4 key购买 nike

C++17 引入了常量,这似乎对缓存感知编程很有用: https://en.cppreference.com/w/cpp/thread/hardware_destructive_interference_size

inline constexpr std::size_t hardware_destructive_interference_size,
inline constexpr std::size_t hardware_constructive_interference_size

尽管我想知道它们的可靠性如何?
是否保证以后不会有相同 CPU 架构内具有其他缓存线大小的新 CPU 型号?

即x64 缓存行大小为 64 字节。但它会变成例如吗? 128 字节 用于某些 future 的 x64 兼容 CPU 型号?
64 字节 缓存行大小编译的代码最终会变得异常。

缓存行大小的变化当然不会破坏代码的逻辑(如果这可能不是类似心脏出血的算法)。但这仍然会导致一些副作用。例如。虚假共享保护将停止工作。

对这些常量的依赖可能会给人这样的感觉:

My code is guaranteed by the standard to run correctly platform-independent.

这实际上有时可能不是正确的感觉。由于代码编译后硬件发生变化。

或者...每个 CPU 架构或类似的东西可能有任何固定的标准缓存行大小吗?

最佳答案

...有点手摇,但评论太长了:

Though I'm wondered how reliable they could be?

您只引用了声明。对于它们的定义,标准仅指定:/*implementation-defined*/。该标准仅向您保证符合要求的实现必须提供定义。

Is it guaranteed that later there will be no new CPU models with the other cache line sizes within the same CPU architecture?

据我所知,这就是标准中包含这些常量的全部意义所在。如果您为不同的体系结构编译代码,这些值将“自动”成为正确的值。不再需要使用魔数(Magic Number)。不再由您负责处理与缓存访问有关的目标架构,而现在由库实现者负责。

PS:可移植不是“一次编译,到处运行”,而是“一次编写,到处编译”

关于c++ - std::hardware_destructive_interference_size 的可靠性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57875779/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com