如何从 Arc<Mutex<T> 取得 T 的所有权? [英] How to take ownership of T from Arc<Mutex<T>>?

查看:23
本文介绍了如何从 Arc<Mutex<T> 取得 T 的所有权?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个受 Mutex 保护的函数中返回一个值,但无法理解如何正确执行此操作.此代码不起作用:

I want to return a value from a function which is protected by a Mutex, but cannot understand how to do it properly. This code does not work:

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

fn func() -> Result<(), String> {
    let result_my = Arc::new(Mutex::new(Ok(())));
    let result_his = result_my.clone();

    let t = std::thread::spawn(move || {
        let mut result = result_his.lock().unwrap();
        *result = Err("something failed".to_string());
    });

    t.join().expect("Unable to join thread");

    let guard = result_my.lock().unwrap();
    *guard
}

fn main() {
    println!("func() -> {:?}", func());
}

游乐场

编译器抱怨:

error[E0507]: cannot move out of borrowed content
  --> src/main.rs:16:5
   |
16 |     *guard
   |     ^^^^^^ cannot move out of borrowed content

推荐答案

目前我发现的最佳解决方案是将结果包装到一个 Option 中,然后将其取出:

The best solution I found so far is to wrap the result into an Option and then take it out:

fn func() -> Result<(), String> {
    let result_my = Arc::new(Mutex::new(Some(Ok(()))));
    let result_his = result_my.clone();

    let t = std::thread::spawn(move || {
        let mut result = result_his.lock().unwrap();
        *result = Some(Err("something failed".to_string()));
    });

    t.join().expect("Unable to join thread");

    let mut guard = result_my.lock().unwrap();
    guard.take().unwrap()
}

这似乎比@SBSTP 提出的 mem::replace 解决方案更好,因为不需要构造一个空的 T 进行交换,它可以防止多次提取.

It seems better than the mem::replace solution proposed by @SBSTP because there is no need to construct an empty T for swapping, and it prevents multiple extractions.

这篇关于如何从 Arc&lt;Mutex&lt;T&gt; 取得 T 的所有权?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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