【发布时间】:2018-10-18 03:11:20
【问题描述】:
我使用的特性不是围绕多线程(草书)设计的。
现在,虽然它使用多线程,但它会在互斥体后面,所以它不能同时在两个线程上使用。
生锈是为了保护我免受什么侵害,我能做些什么吗?
示例参考,我的示例代码是:
extern crate cursive;
use cursive::Cursive;
use std::thread;
use std::sync::{Mutex,Arc};
fn main() {
let mut siv = Arc::new(Mutex::new(Cursive::default()));
let copy_siv = siv.clone();
thread::spawn(move || {
let mut new_siv = copy_siv.lock().unwrap();
});
(*(siv.lock().unwrap())).run();
}
编译器在thread::spawn抱怨:
Error[E0277]: `(dyn cursive::traits::View + 'static)` cannot be sent between threads safely
--> src/main.rs:16:5
|
16 | thread::spawn(move || {
| ^^^^^^^^^^^^^ `(dyn cursive::traits::View + 'static)` cannot be sent between threads safely
|
= help: the trait `std::marker::Send` is not implemented for `(dyn cursive::traits::View + 'static)`
【问题讨论】:
-
好吧,由于其他原因(siv 主循环永远不会返回锁),它不会工作,但主要问题仍然存在。
-
请包含更多代码以查看。对您编写的类型的天真假设有效。见play.rust-lang.org/… 基本上,你的错误信息说
Cursive类型不是Sync,所以Arc<Mutex<Cursive>>不是Send。 -
@EarthEngine 为什么 Mutex 不能让它同步?这不是 Mutex 的重点吗?
-
这就是为什么我需要看看事物是如何定义的......猜测没有意义。
-
@EarthEngine 这够了吗?
标签: multithreading rust mutex