我尝试在向量的块上执行函数,然后使用消息传递库将结果发回。
然而,我得到一个奇怪的错误,关于矢量的生命周期甚至没有参与线程操作:
src/lib.rs:153:27: 154:25 error: borrowed value does not live long enough
src/lib.rs:153 let extended_segments = (segment_size..max_val)
error: src/lib.rs:154 .collect::<Vec<_>>()borrowed value does not live long enough
note: reference must be valid for the static lifetime...:153
let extended_segments = (segment_size..max_val)
src/lib.rs:153:3: 155:27: 154 .collect::<Vec<_>>()
note: but borrowed value is only valid for the statement at 153:2:
reference must be valid for the static lifetime...
src/lib.rs:
let extended_segments = (segment_size..max_val)
consider using a `let` binding to increase its lifetime
我尝试在迭代器周围移动并将生命周期添加到不同的地方,但我无法让检查器通过并仍保持类型。
根据concurrency chapter in the Rust book,违规代码如下。 (完整代码位于github。)
use std::sync::mpsc;
use std::thread;
fn sieve_segment(a: &[usize], b: &[usize]) -> Vec<usize> {
vec![]
}
fn eratosthenes_sieve(val: usize) -> Vec<usize> {
vec![]
}
pub fn segmented_sieve_parallel(max_val: usize, mut segment_size: usize) -> Vec<usize> {
if max_val <= ((2 as i64).pow(16) as usize) {
// early return if the highest value is small enough (empirical)
return eratosthenes_sieve(max_val);
}
if segment_size > ((max_val as f64).sqrt() as usize) {
segment_size = (max_val as f64).sqrt() as usize;
println!("Segment size is larger than √{}. Reducing to {} to keep resource use down.",
max_val,
segment_size);
}
let small_primes = eratosthenes_sieve((max_val as f64).sqrt() as usize);
let mut big_primes = small_primes.clone();
let (tx, rx): (mpsc::Sender<Vec<usize>>, mpsc::Receiver<Vec<usize>>) = mpsc::channel();
let extended_segments = (segment_size..max_val)
.collect::<Vec<_>>()
.chunks(segment_size);
for this_segment in extended_segments.clone() {
let small_primes = small_primes.clone();
let tx = tx.clone();
thread::spawn(move || {
let sieved_segment = sieve_segment(&small_primes, this_segment);
tx.send(sieved_segment).unwrap();
});
}
for _ in 1..extended_segments.count() {
big_primes.extend(&rx.recv().unwrap());
}
big_primes
}
fn main() {}
我如何理解并避免此错误?我不确定如何使线程闭包static as in this question的生命周期仍然具有可重用的功能(即,不是main()
)。我不确定如何消费所有进入[关闭]的东西&#34;如this question中所述。而且我不确定insert .map(|s| s.into())
在哪里确保所有引用都成为移动,我也不确定。
答案 0 :(得分:4)
尝试重现问题时,我建议您删除所有不相关的代码来创建MCVE。在这种情况下,这样的事情似乎产生了同样的错误:
fn segmented_sieve_parallel(max_val: usize, segment_size: usize) {
let foo = (segment_size..max_val)
.collect::<Vec<_>>()
.chunks(segment_size);
}
fn main() {}
让我们打破这一点:
Vec<usize>
。查看slice::chunks
的定义:
fn chunks(&self, size: usize) -> Chunks<T>
pub struct Chunks<'a, T> where T: 'a {
// some fields omitted
}
生命周期标记'a
让您知道迭代器包含对某事物的引用。 Lifetime elision 已从函数中移除了'a
,看起来像这样,展开了:
fn chunks<'a>(&'a self, size: usize) -> Chunks<'a, T>
查看错误消息的这一行:
帮助:考虑使用
let
绑定来增加其生命周期
你可以这样说:
fn segmented_sieve_parallel(max_val: usize, segment_size: usize) {
let foo = (segment_size..max_val)
.collect::<Vec<_>>();
let bar = foo.chunks(segment_size);
}
fn main() {}
虽然我把它写成
fn segmented_sieve_parallel(max_val: usize, segment_size: usize) {
let foo: Vec<_> = (segment_size..max_val).collect();
let bar = foo.chunks(segment_size);
}
fn main() {}
将此代码重新插入原始问题并不能解决问题,但会更容易理解。那是因为你试图传递对thread::spawn
的引用,这可能比当前线程更长。因此,传递给thread::spawn
的所有内容都必须具有'static
生命周期。有很多问题详细说明了为什么必须加以防范以及一系列解决方案,包括范围线程和克隆载体。
克隆载体是最简单的,但可能效率低下:
for this_segment in extended_segments.clone() {
let this_segment = this_segment.to_vec();
// ...
}