首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在创建可以在线程之间传递的引用的二叉树时,会出现“生存时间不够长”的情况

在创建可以在线程之间传递的引用的二叉树时,会出现“生存时间不够长”的情况
EN

Stack Overflow用户
提问于 2014-10-04 14:07:45
回答 1查看 586关注 0票数 0

我正在尝试编写一个二叉树,它可以在线程之间传递,而不必每次都被复制。我很难理解如何在Rust的生命周期限制下做到这一点。

代码语言:javascript
复制
use std::thread::spawn;

#[derive(Debug)]
struct Node<'a> {
    left: &'a i32,
    right: &'a i32,
}

fn main() {
    let l = 3;
    let r = 4;
    let n = Node {
        left: &l,
        right: &r,
    };

    spawn(|| {
        println!("{:?}", n);
    });
}
代码语言:javascript
复制
error[E0597]: `l` does not live long enough
  --> src/main.rs:13:15
   |
13 |           left: &l,
   |                 ^^ borrowed value does not live long enough
...
17 | /     spawn(|| {
18 | |         println!("{:?}", n);
19 | |     });
   | |______- argument requires that `l` is borrowed for `'static`
20 |   }
   |   - `l` dropped here while still borrowed

error[E0597]: `r` does not live long enough
  --> src/main.rs:14:16
   |
14 |           right: &r,
   |                  ^^ borrowed value does not live long enough
...
17 | /     spawn(|| {
18 | |         println!("{:?}", n);
19 | |     });
   | |______- argument requires that `r` is borrowed for `'static`
20 |   }
   |   - `r` dropped here while still borrowed

error[E0373]: closure may outlive the current function, but it borrows `n`, which is owned by the current function
  --> src/main.rs:17:11
   |
17 |     spawn(|| {
   |           ^^ may outlive borrowed value `n`
18 |         println!("{:?}", n);
   |                          - `n` is borrowed here
   |
note: function requires argument type to outlive `'static`
  --> src/main.rs:17:5
   |
17 | /     spawn(|| {
18 | |         println!("{:?}", n);
19 | |     });
   | |______^
help: to force the closure to take ownership of `n` (and any other referenced variables), use the `move` keyword
   |
17 |     spawn(move || {
   |           ^^^^^^^

我理解为什么它会认为它们活得不够长,但我应该如何重组它才能让它们活得足够长呢?

EN

回答 1

Stack Overflow用户

发布于 2014-10-04 15:19:14

在您的情况下,使用在main()函数中定义的变量引用创建一个Node对象,并将其传递给一个新线程。问题是你完全不能保证这个线程会在你的main()的线程之前完成,并且你冒着在引用之前原始变量超出作用域的风险,因此出现了错误。

考虑到您希望构建一个树,我认为最简单的解决方法是让您的Node类型拥有它的字段,如下所示:

代码语言:javascript
复制
#[derive(Debug)]
struct Node {
    left: i32,
    right: i32,
}

这样你就不会再有终生的问题了。

要在不复制的情况下在多个线程之间共享它,可以使用std::sync::Arc包装器;它就是为了这个目的。你可以这样做:

代码语言:javascript
复制
use std::sync::Arc;

fn main() {
    let n = Arc::new(Node { left: 3, right: 4 });

    for _ in 0..10 {
        let n_thread = n.clone();
        spawn(move || {
            println!("{:?}", n_thread);
        });
    }
}

如果您还需要授予其他线程对Node的写访问权限,您可能会希望将其包装在MutexRwLock中,将所有内容都保留在Arc中。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26190213

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档