问题描述
我正在尝试用 Rust 制作一个主要的筛子。
我需要对数组相同元素的多个可变引用,而不是对数组不同部分的可变引用。
就其工作方式而言,我知道数据竞争不是相关问题,因此可以接受多个可变引用,但 Rust 编译器不接受我的不安全代码。
我使用的是横梁 0.8.0。
fn extend_helper(
primes: &Vec<usize>,true_block: &mut Vec<bool>,segment_min: usize,segment_len: usize,) {
crossbeam::scope(|scope| {
for prime in primes {
let prime = prime.clone();
let segment_min = &segment_min;
let segment_len = &segment_len;
let shared = unsafe { &mut *true_block };
scope.spawn(move |_| {
let tmp = smallest_multiple_of_n_geq_m(prime,*segment_min) - *segment_min;
for j in (tmp..*segment_len).step_by(prime) {
shared[j] = false;
}
});
}
})
.unwrap();
}
fn smallest_multiple_of_n_geq_m(n: usize,m: usize) -> usize {
m + ((n - (m % n)) % n)
}
error[E0499]: cannot borrow `*true_block` as mutable more than once at a time
--> src/lib.rs:12:35
|
7 | crossbeam::scope(|scope| {
| ----- has type `&Scope<'1>`
...
12 | let shared = unsafe { &mut *true_block };
| ^^^^^^^^^^^^^^^^ `*true_block` was mutably borrowed here in the prevIoUs iteration of the loop
13 |
14 | / scope.spawn(move |_| {
15 | | let tmp = smallest_multiple_of_n_geq_m(prime,*segment_min) - *segment_min;
16 | | for j in (tmp..*segment_len).step_by(prime) {
17 | | shared[j] = false;
18 | | }
19 | | });
| |______________- argument requires that `*true_block` is borrowed for `'1`
warning: unnecessary `unsafe` block
--> src/lib.rs:12:26
|
12 | let shared = unsafe { &mut *true_block };
| ^^^^^^ unnecessary `unsafe` block
|
= note: `#[warn(unused_unsafe)]` on by default
我应该如何以 Rust 接受的方式编写 unsafe?
解决方法
Rust 在其模型中不允许这样做。您想要 AtomicBool
的轻松排序。
在大多数并发模型中,这是一种比较常见的边缘情况 - 如果您有两个具有相同值的非原子写入 到一个位置,是否定义明确?在 Rust(和 C++)中不是,你需要显式使用 atomic。
On any platform you care about,the atomic bool store with relaxed ordering will have no impact.
出于这个原因,请考虑:
pub fn do_the_thing(x: &mut bool) {
if !*x { return };
// Do some stuff.
*x = true;
}
在将 x
设置为 true 时,编译器可以自由地假设(由于没有共享的可变引用)x
仍然为 false。它可以实现这个赋值,例如,在汇编中使用 inc x
,将它的值从 0 移动到 1。
如果两个线程通过这个并且都达到了 *x = true
,它的值可能会变成 0 或 1 以外的其他值,这可能会违反其他假定的不变量。