#memory #lockless

consume

当编译器无法省略依赖时,提供内存顺序消耗

2 个稳定版本

使用旧的 Rust 2015

1.0.1 2017 年 2 月 26 日

并发 中排名 907

MIT 许可证

3KB

当编译器无法省略依赖时,提供消耗顺序

示例

extern crate consume;
use std::sync::atomic::{AtomicPtr, AtomicUsize};

fn consume_load(pt: &AtomicPtr<*const usize>) -> usize {
    // There's a data dependency on the loaded value, 
    // meaning that the compiler can't od silly things to
    // eliminate the dependency
    unsafe { *pt.load(Consume) }
}

fn incorrect_consume_load(ind: &AtomicUsize, vals: &Vec<usize>) -> usize {
    // There is no data dependency here
    // since the compiler can eliminate the loaded value
    // from the pointer index. Ensure that you don't have this.
    // Use consume at your own risk.
    let i = ind.load(consume::Consume);
    vals[ind - ind]
    // vals[ind] would probably be correct, unless some shenanigans with the
    // bounds checking occured. I only use vec for convenience, only
    // use consume if you understand all possible data paths taken by the compiler
}

无运行时依赖