如何将非静态数据发送到 Rust 中的线程?本示例中是否需要它?

the*_*ian 5 concurrency multithreading thread-safety rust

我正在尝试使用 Rust 中的一些堆数据启动一个新线程,但我收到了一堆错误,这些错误源于数据需要具有'static生命周期。我已经按照我的方式倒退了我的程序,但遇到了问题。

use std::sync::Arc;
use std::thread;

struct ThreadData {
    vector_of_strings: Vec<String>,
    terms: Vec<&'static str>,
    quotient: usize,
}

fn perform_search(slice: &[String], terms: &[&str]) {
    /* ... */
}

fn threaded_search(td_arc: &Arc<ThreadData>) {
    let no_of_lines = td_arc.vector_of_strings.len();
    let new_tda1 = td_arc.clone();

    let strings_as_slice1 = new_tda1.vector_of_strings.as_slice();   

    thread::spawn(move || {
        perform_search(&strings_as_slice1[0..td_arc.quotient], &new_tda1.terms);
    });
}

fn main() {
    let td = ThreadData {
        vector_of_strings: Vec::new(),
        terms: Vec::new(),
        quotient: 0,
    };

    let td_arc = Arc::new(td);
    threaded_search(&td_arc);
}
Run Code Online (Sandbox Code Playgroud)

错误:

use std::sync::Arc;
use std::thread;

struct ThreadData {
    vector_of_strings: Vec<String>,
    terms: Vec<&'static str>,
    quotient: usize,
}

fn perform_search(slice: &[String], terms: &[&str]) {
    /* ... */
}

fn threaded_search(td_arc: &Arc<ThreadData>) {
    let no_of_lines = td_arc.vector_of_strings.len();
    let new_tda1 = td_arc.clone();

    let strings_as_slice1 = new_tda1.vector_of_strings.as_slice();   

    thread::spawn(move || {
        perform_search(&strings_as_slice1[0..td_arc.quotient], &new_tda1.terms);
    });
}

fn main() {
    let td = ThreadData {
        vector_of_strings: Vec::new(),
        terms: Vec::new(),
        quotient: 0,
    };

    let td_arc = Arc::new(td);
    threaded_search(&td_arc);
}
Run Code Online (Sandbox Code Playgroud)

dto*_*nay 4

错误'static是因为在其中创建的新线程thread::spawn可能比最初创建线程时的调用寿命更长threaded_search,这意味着不得允许线程使用threaded_search生命周期短于 的任何局部变量'static

在您的代码中,新线程指的是strings_as_slice1td_arc

通常,thread::spawnArc会希望将一个引用计数的所有权移至线程中,并让线程通过该引用计数指针访问它需要的任何内容,而不是直接从封闭的短期作用域中访问。

fn threaded_search(td_arc: &Arc<ThreadData>) {
    // Increment reference count that we can move into the new thread.
    let td_arc = td_arc.clone();

    thread::spawn(move || {
        perform_search(&td_arc.vector_of_strings[0..td_arc.quotient], &td_arc.terms);
    });
}
Run Code Online (Sandbox Code Playgroud)