如何将非静态数据发送到 Rust 中的线程,在本例中是否需要它? [英] How can I send non-static data to a thread in Rust and is it needed in this example?

查看:10
本文介绍了如何将非静态数据发送到 Rust 中的线程,在本例中是否需要它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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. 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[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

推荐答案

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

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.

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

In your code the new thread is referring to strings_as_slice1 and td_arc.

通常使用 thread::spawnArc 你会想要将一个引用计数的所有权移到线程中,并让线程通过引用计数访问它需要的任何东西指针,而不是直接来自封闭的短期作用域.

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.

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

这篇关于如何将非静态数据发送到 Rust 中的线程,在本例中是否需要它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆