在Rust中,可以在多个线程之间共享数据而不需要静态生命周期吗?

时间:2018-05-27 20:39:25

标签: parallel-processing rust

我正在尝试获取大字符串或数据并将其分解为切片以跨多个线程进行处理。我想利用多个线程来帮助处理数据,但又想避免复制数据。

以下是问题的一个示例。 Segment0将起作用,因为它通过副本拥有数据。 Segment1只是借用了数据,即使连接保证了生命周期的正确性,也会引起问题。

use std::thread;
use std::fs;
use std::sync::{Arc};

fn main() {
    let data = fs::read("./test.txt").unwrap();

    let segment_size = data.len()/2;
    let segment0 = &data[0..segment_size]; //Borrow data no copy
    let segment1 = &data[segment_size..]; //Borrow data no copy

    let segment0 = Vec::from(segment0); //has ownership of data through copy

    let thread0 = thread::spawn(move || println!("Thread 0 {:?}",std::str::from_utf8(&segment0)));
    let thread1 = thread::spawn(move || println!("Thread 1 {:?}",std::str::from_utf8(segment1)));

    thread0.join(); //thread 0 terminated segment0 dropped
    thread1.join(); //thread 1 terminated segment1 dropped

    //data released  segment0 and segment1 needed to be dropped before
}

错误返回

error[E0597]: `data` does not live long enough
--> src/main.rs:10:25
|
10 |         let segment1 = &data[segment_size..]; //Borrow data no copy
|                         ^^^^ borrowed value does not live long enough
...
21 |     }
|     - borrowed value only lives until here
|
= note: borrowed value must be valid for the static lifetime...

1 个答案:

答案 0 :(得分:0)

谢谢scoped_threadpool解决了我的问题,这是我的解决方案

extern crate scoped_threadpool;
use std::thread;
use std::fs;
use std::sync::{Arc};

use scoped_threadpool::Pool;


fn main() {
    let data = fs::read("./test.txt").unwrap();

    let segment_size = data.len() / 2;
    let segment0 = &data[0..segment_size]; //Borrow data no copy
    let segment1 = &data[segment_size..]; //Borrow data no copy

    let mut pool = Pool::new(2);

    pool.scoped(|scoped| {
        scoped.execute(move || println!("Thread 0 {:?}", std::str::from_utf8(segment0)));
        scoped.execute(move || println!("Thread 1 {:?}", std::str::from_utf8(segment1)));
    });
}