相关疑难解决方法(0)

Rust mpsc :: Sender不能在线程之间共享?

我认为渠道的全部目的是在线程之间共享数据.我有这个代码,基于这个例子:

let tx_thread = tx.clone();
let ctx = self;
thread::spawn(|| {
    ...
    let result = ctx.method()
    tx_thread.send((String::from(result), someOtherString)).unwrap();
})
Run Code Online (Sandbox Code Playgroud)

哪里txmpsc::Sender<(String, String)>

error[E0277]: the trait bound `std::sync::mpsc::Sender<(std::string::String, std::string::String)>: std::marker::Sync` is not satisfied
   --> src/my_module/my_file.rs:137:9
    |
137 |         thread::spawn(|| {
    |         ^^^^^^^^^^^^^
    |
    = note: `std::sync::mpsc::Sender<(std::string::String, std::string::String)>` 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::string::String, std::string::String)>`
    = note: required because it appears within the type …
Run Code Online (Sandbox Code Playgroud)

multithreading rust

10
推荐指数
2
解决办法
4866
查看次数

标签 统计

multithreading ×1

rust ×1