Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Multithreading 通过将FnMut包裹到弱线程中,将其发送到其他线程<;互斥锁<;FnMut>&燃气轮机;投诉丢失发送_Multithreading_Rust - Fatal编程技术网

Multithreading 通过将FnMut包裹到弱线程中,将其发送到其他线程<;互斥锁<;FnMut>&燃气轮机;投诉丢失发送

Multithreading 通过将FnMut包裹到弱线程中,将其发送到其他线程<;互斥锁<;FnMut>&燃气轮机;投诉丢失发送,multithreading,rust,Multithreading,Rust,我试图用事件和回调编写一个简单的观察者模式。其思想是,当拥有变异函数的对象死亡(struct Obj{cb:Arc,…})时,它会丢弃Arc。在此之后,任何弱引用都将(有效地)失效,并且可以在访问时检测到。我还希望这能将FnMut与线程的静态生命周期解耦。我可能需要用另一个Arc来包装整个过程,以防止弱引用执行线程不安全的操作,但这是另一个问题 我得到的最接近的解决方案是: 然而,我似乎有弧,添加生存期(尽管我可能做得不对)似乎没有帮助。我有点不知道到底是什么错了 我写了一个简单的例子: use

我试图用事件和回调编写一个简单的观察者模式。其思想是,当拥有变异函数的对象死亡(
struct Obj{cb:Arc,…}
)时,它会丢弃
Arc
。在此之后,任何
引用都将(有效地)失效,并且可以在访问时检测到。我还希望这能将
FnMut
线程的
静态生命周期解耦。我可能需要用另一个
Arc
来包装整个过程,以防止
引用执行线程不安全的操作,但这是另一个问题

我得到的最接近的解决方案是:

然而,我似乎有
,添加生存期(尽管我可能做得不对)似乎没有帮助。我有点不知道到底是什么错了

我写了一个简单的例子:

use std::{
    collections::HashMap,
    sync::{
        mpsc::{self, Receiver},
        Arc, Mutex, Weak,
    },
    thread::{self, JoinHandle},
};

struct Callback {
    cb_w: Weak<Mutex<FnMut()>>,
}

type FnMapping = Arc<HashMap<String, Callback>>;

fn start_thread<'a>(rx: Receiver<String>, mapping: FnMapping) -> JoinHandle<()> {
    thread::spawn(move || {
        match rx.recv() {
            Ok(v) => {
                if let Some(o) = mapping.get(&v) {
                    match o.cb_w.upgrade() {
                        Some(cb_m) => match cb_m.lock() {
                            Ok(cb_lg) => (*cb_lg)(),
                            Err(e) => (),
                        },
                        None => { /* TODO owner is gone, mark for delete */ }
                    }
                }
            }
            Err(e) => (),
        }
    })
}

fn main() {
    let mapping: FnMapping = Arc::new(HashMap::new());
    let (tx, rx) = mpsc::channel();
    drop(tx);
    start_thread(rx, mapping)
        .join()
        .expect("Could not join thread -- failed to terminate?");
    println!("Leaving the test bay.");
}
使用std::{
集合::HashMap,
同步::{
mpsc::{self,Receiver},
弧、互斥、弱、,
},
线程::{self,JoinHandle},
};
结构回调{
cb_w:弱,
}
类型=圆弧;
fn开始线程src/main.rs:17:5
|
17 |线程::繁殖(移动| |){
|^^^^^^^^`(dyn std::ops::FnMut()+'static)`无法在线程之间安全发送
|
=help:trait`std::marker::Send`未为`(dyn std::ops::FnMut()+'静态)实现`
=注意:由于'std::marker::Send'的impl上对'std::sync::Mutex'的要求,因此需要此选项>`
=注意:必需,因为它出现在“回调”类型中`
=注意:必需,因为它出现在类型`(std::string::string,Callback)中`
=注意:必需,因为它出现在类型“std::marker::PhantomData”中`
=注意:必需,因为它出现在类型“std::collections::hash::table::RawTable”中`
=注意:必需,因为它出现在类型“std::collections::HashMap”中`
=注意:由于'std::marker::Send'的impl中对'std::sync::Arc'的要求,因此需要`
=注意:必需,因为它出现在类型中`[closure@src/main.rs:17:19:32:6 rx:std::sync::mpsc::Receiver,映射:std::sync::Arc]`
=注意:`std::thread::spawn'需要`

<代码>太长了,读不下去了。我使用了,因为缺少导入。我在所有的类型中使用了完全限定的名称@ ShepmasterBut,为什么要主动地让我们更难阅读?TL;DRID:重复代码:<代码> CBW:弱。k(mut cb_lg)=>(&mut*cb_lg)()<代码>太长了,读不下去了。我使用了,因为缺少导入。我在所有的类型中使用了完全限定的名称@ ShepmasterBut,为什么要主动地让我们更难阅读?TL;DRID:重复代码:<代码> CBW:弱。k(mut cb_lg)=>(&mut*cb_lg)(