简体   繁体   English

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

[英]How can I send non-static data to a thread in Rust and is it needed in this example?

I am trying to fire up a new thread using some heap data in Rust and I am getting a bunch of errors that stem from the need of the data to have 'static lifetime. 我正在尝试使用Rust中的一些堆数据来启动一个新线程,并且我收到一堆错误,这些错误源于对数据具有'static生存期”的需求。 I've worked my way backwards up my program but hit a problem. 我已经尽力将程序倒退,但是遇到了问题。

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);
}

Error: 错误:

error[E0621]: explicit lifetime required in the type of `td_arc`               
  --> src/main.rs:20:5                                                         
   |                                                                           
14 | fn threaded_search(td_arc: &Arc<ThreadData>) {                            
   |                            ---------------- help: add explicit lifetime `'static` to the type of `td_arc`: `&'static std::sync::Arc<ThreadData>`
...                                                                            
20 |     thread::spawn(move || {                                               
   |     ^^^^^^^^^^^^^ lifetime `'static` required

The error about 'static is because the new thread created within thread::spawn may outlive the invocation of threaded_search during which the thread is initially created, which means the thread must not be permitted to use any local variables from threaded_search with a lifetime shorter than 'static . 关于'static ”的错误是因为在thread::spawn内创建的新线程可能会超过最初创建线程的对threaded_search的调用,这意味着不允许该线程使用threaded_search任何本地变量,且其生存期短于'static

In your code the new thread is referring to strings_as_slice1 and td_arc . 在您的代码中,新线程引用了strings_as_slice1td_arc

Generally with thread::spawn and Arc you will want to move ownership of one reference count into the thread and have the thread access whatever it needs through that reference counted pointer rather than from the enclosing short-lived scope directly. 通常,使用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);
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM