【发布时间】:2016-07-11 09:16:50
【问题描述】:
我正在努力通过通道发送自定义结构。
如教程中所述,我将值包装在 Arc 和 Mutex 中,
但它无论如何都不会编译。
extern crate num;
use num::bigint::BigInt;
use std::io::{self, Write};
use std::sync::mpsc;
use std::thread;
use readline::readline as ask;
use std::sync::{Arc, Mutex};
enum Command {
Quit,
Help,
Factorial { n: BigInt },
Error { msg: String },
}
fn main() {
let (input_tx, input_rx) = mpsc::channel();
let input_thread = thread::spawn(|| {
input_tx.send(Arc::new(Mutex::new(Command::Quit)));
});
}
error: the trait bound `std::sync::mpsc::Sender<std::sync::Arc<std::sync::Mutex<Command>>>: std::marker::Sync` is not satisfied [E0277]
let input_thread = thread::spawn(|| {
^~~~~~~~~~~~~
help: run `rustc --explain E0277` to see a detailed explanation
note: `std::sync::mpsc::Sender<std::sync::Arc<std::sync::Mutex<Command>>>` cannot be shared between threads safely
note: required because of the requirements on the impl of `std::marker::Send` for `&std::sync::mpsc::Sender<std::sync::Arc<std::sync::Mutex<Command>>>`
note: required because it appears within the type `[closure@src/main.rs:25:38: 65:6 input_tx:&std::sync::mpsc::Sender<std::sync::Arc<std::sync::Mutex<Command>>>]`
note: required by `std::thread::spawn`
error: aborting due to previous error
我正在使用 Rust 1.10.0 (cfcb716cf 2016-07-03)。
【问题讨论】:
-
注意:您将值包装在通道内,但是这里的编译器抱怨 sender 到通道没有被包装。
标签: multithreading rust