Align struct to cache lines in Rust

廉价感情. 提交于 2020-05-14 20:00:51

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!