将结构与 Rust 中的缓存行对齐 [英] Align struct to cache lines in Rust
问题描述
假设我想(ab)使用缓存一致性来进行无锁读取,例如 FaRM,将一个带有单个 64 字节数组的结构作为数据是否足以保证在具有 64 字节缓存行的体系结构上每个结构将只占用一个缓存行?>
Assuming I wanted to (ab)use cache coherency to do lock free reads like FaRM, would it be enough to have a struct with a single 64 byte array as data to guarantee that on an architecture with 64 byte cache lines each struct would occupy exactly one cache line?
推荐答案
不,这不能保证对齐是缓存行.
No, that wouldn't guarantee that the alignment was a cache line.
RFC 1358 添加了 的概念#[repr(align)]
,允许程序员指定对齐要求.此属性在 Rust 中稳定1.25.
RFC 1358 added the concept of #[repr(align)]
, allowing the programmer to specify alignment requirements. This attribute was stabilized in Rust 1.25.
对于您的具体情况,您可以像这样使用它:
For your specific case, you'd use it like:
#[repr(align(64))]
struct Foo {
value: u8,
}
这篇关于将结构与 Rust 中的缓存行对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!