小编Tim*_*onk的帖子

移动接收器线程抱怨同步,但预期发送

我试图通过Arc一个接收器引用一个线程,所以我可以通过调度程序集中发布pub-sub.但是,我收到以下错误:

src/dispatcher.rs:58:11: 58:24 error: the trait `core::marker::Sync` is not implemented for the type `core::cell::UnsafeCell<std::sync::mpsc::Flavor<dispatcher::DispatchMessage>>` [E0277]
src/dispatcher.rs:58           thread::spawn(move || {
                               ^~~~~~~~~~~~~
src/dispatcher.rs:58:11: 58:24 note: `core::cell::UnsafeCell<std::sync::mpsc::Flavor<dispatcher::DispatchMessage>>` cannot be shared between threads safely
src/dispatcher.rs:58           thread::spawn(move || {
Run Code Online (Sandbox Code Playgroud)

笏!我认为只有Send跨越频道才需要?代码DispatchMessage是:

#[derive(PartialEq, Debug, Clone)]
enum DispatchType {
    ChangeCurrentChannel,
    OutgoingMessage,
    IncomingMessage
}

#[derive(Clone)]
struct DispatchMessage {
   dispatch_type: DispatchType,
   payload: String
}
Run Code Online (Sandbox Code Playgroud)

两者String肯定Enum都是Send,对吧?为什么抱怨Sync

调度员的相关部分:

pub fn start(&self) {
   let shared_subscribers = Arc::new(self.subscribers);
   for ref …
Run Code Online (Sandbox Code Playgroud)

multithreading rust

4
推荐指数
2
解决办法
1223
查看次数

标签 统计

multithreading ×1

rust ×1