如何使用 Arc 在线程之间共享可变对象? [英] How do I share a mutable object between threads using Arc?

查看:24
本文介绍了如何使用 Arc 在线程之间共享可变对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Arc 在 Rust 中的线程之间共享一个可变对象,但出现此错误:

I'm trying to share a mutable object between threads in Rust using Arc, but I get this error:

error[E0596]: cannot borrow data in a `&` reference as mutable
  --> src/main.rs:11:13
   |
11 |             shared_stats_clone.add_stats();
   |             ^^^^^^^^^^^^^^^^^^ cannot borrow as mutable

这是示例代码:

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

fn main() {
    let total_stats = Stats::new();
    let shared_stats = Arc::new(total_stats);

    let threads = 5;
    for _ in 0..threads {
        let mut shared_stats_clone = shared_stats.clone();
        thread::spawn(move || {
            shared_stats_clone.add_stats();
        });
    }
}

struct Stats {
    hello: u32,
}

impl Stats {
    pub fn new() -> Stats {
        Stats { hello: 0 }
    }

    pub fn add_stats(&mut self) {
        self.hello += 1;
    }
}

我能做什么?

推荐答案

Arc 的文档说:

Rust 中的共享引用默认不允许突变,Arc 也不例外:您通常无法获得对 Arc 内某些内容的可变引用.如果您需要通过 Arc 进行变异,请使用 Mutex, RwLockAtomic 之一 类型.

Shared references in Rust disallow mutation by default, and Arc is no exception: you cannot generally obtain a mutable reference to something inside an Arc. If you need to mutate through an Arc, use Mutex, RwLock, or one of the Atomic types.

您可能希望将 MutexArc 结合使用:

You will likely want a Mutex combined with an Arc:

use std::{
    sync::{Arc, Mutex},
    thread,
};

struct Stats;

impl Stats {
    fn add_stats(&mut self, _other: &Stats) {}
}

fn main() {
    let shared_stats = Arc::new(Mutex::new(Stats));

    let threads = 5;
    for _ in 0..threads {
        let my_stats = shared_stats.clone();
        thread::spawn(move || {
            let mut shared = my_stats.lock().unwrap();
            shared.add_stats(&Stats);
        });
        // Note: Immediately joining, no multithreading happening!
        // THIS WAS A LIE, see below
    }
}

这主要来自 Mutex 文档.

如何在 for 之后使用 shared_stats?(我说的是 Stats 对象).似乎 shared_stats 不能轻易转换为 Stats.

How can I use shared_stats after the for? (I'm talking about the Stats object). It seems that the shared_stats cannot be easily converted to Stats.

从 Rust 1.15 开始,可以取回值.另请参阅我的其他解决方案的其他答案.

As of Rust 1.15, it's possible to get the value back. See my additional answer for another solution as well.

[示例中的注释] 表示没有多线程.为什么?

[A comment in the example] says that there is no multithreading. Why?

因为我糊涂了!:-)

在示例代码中,thread::spawn 的结果(a JoinHandle) 被立即删除,因为它没有存储在任何地方.当手柄掉落时,线程分离并且可能永远也可能永远不会完成.我把它和 JoinGuard 混淆了,一个旧的、被移除的 API,在它被删除时加入.抱歉造成混乱!

In the example code, the result of thread::spawn (a JoinHandle) is immediately dropped because it's not stored anywhere. When the handle is dropped, the thread is detached and may or may not ever finish. I was confusing it with JoinGuard, a old, removed API that joined when it is dropped. Sorry for the confusion!

对于一些社论,我建议完全避免可变性:

For a bit of editorial, I suggest avoiding mutability completely:

use std::{ops::Add, thread};

#[derive(Debug)]
struct Stats(u64);

// Implement addition on our type
impl Add for Stats {
    type Output = Stats;
    fn add(self, other: Stats) -> Stats {
        Stats(self.0 + other.0)
    }
}

fn main() {
    let threads = 5;

    // Start threads to do computation
    let threads: Vec<_> = (0..threads).map(|_| thread::spawn(|| Stats(4))).collect();

    // Join all the threads, fail if any of them failed
    let result: Result<Vec<_>, _> = threads.into_iter().map(|t| t.join()).collect();
    let result = result.unwrap();

    // Add up all the results
    let sum = result.into_iter().fold(Stats(0), |i, sum| sum + i);
    println!("{:?}", sum);
}

在这里,我们保留对 JoinHandle 的引用,然后等待所有线程完成.然后我们收集结果并将它们全部加起来.这是常见的map-reduce 模式.请注意,没有任何线程需要任何可变性,这一切都发生在主线程中.

Here, we keep a reference to the JoinHandle and then wait for all the threads to finish. We then collect the results and add them all up. This is the common map-reduce pattern. Note that no thread needs any mutability, it all happens in the master thread.

这篇关于如何使用 Arc 在线程之间共享可变对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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