我一直在研究一个函数,它将使用 Rust 和线程将一堆文件从源复制到目标。我在让线程共享迭代器时遇到了一些麻烦。我还不习惯借阅系统:
extern crate libc;
extern crate num_cpus;
use libc::{c_char, size_t};
use std::thread;
use std::fs::copy;
fn python_str_array_2_str_vec<T, U, V>(_: T, _: U) -> V {
unimplemented!()
}
#[no_mangle]
pub extern "C" fn copyFiles(
sources: *const *const c_char,
destinies: *const *const c_char,
array_len: size_t,
) {
let src: Vec<&str> = python_str_array_2_str_vec(sources, array_len);
let dst: Vec<&str> = python_str_array_2_str_vec(destinies, array_len);
let mut iter = src.iter().zip(dst);
let num_threads = num_cpus::get();
let threads = (0..num_threads).map(|_| {
thread::spawn(|| while let Some((s, d)) = iter.next() {
copy(s, d);
})
});
for t in threads {
t.join();
}
}
fn main() {}
我收到了我无法解决的编译错误:
error[E0597]: `src` does not live long enough
--> src/main.rs:20:20
|
20 | let mut iter = src.iter().zip(dst);
| ^^^ does not live long enough
...
30 | }
| - borrowed value only lives until here
|
= note: borrowed value must be valid for the static lifetime...
error[E0373]: closure may outlive the current function, but it borrows `**iter`, which is owned by the current function
--> src/main.rs:23:23
|
23 | thread::spawn(|| while let Some((s, d)) = iter.next() {
| ^^ ---- `**iter` is borrowed here
| |
| may outlive borrowed value `**iter`
|
help: to force the closure to take ownership of `**iter` (and any other referenced variables), use the `move` keyword, as shown:
| thread::spawn(move || while let Some((s, d)) = iter.next() {
我已经看到了以下问题:
当使用多个我不使用的线程时,值的寿命不够长chunks
,我想尝试通过线程共享一个迭代器,尽管创建块以将它们传递给线程将是经典的解决方案。
无法在线程之间发送 &str 因为它的寿命不够长 我已经看到了一些使用通道与线程通信的答案,但我不太确定使用它们。应该有一种更简单的方法来通过线程共享一个对象。
为什么局部变量对于 thread::scoped 的寿命不够长
这引起了我的注意,scoped
应该可以解决我的错误,但是由于它位于不稳定的通道中,我想看看是否有另一种方法可以使用spawn
.
有人可以解释我应该如何修复生命周期以便可以从线程访问迭代器吗?