问题
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?
回答1:
No, that wouldn't guarantee that the alignment was a cache line.
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,
}
来源:https://stackoverflow.com/questions/41090078/align-struct-to-cache-lines-in-rust