繁体   English   中英

无法跨具有 mpsc::Sender 作为字段的线程发送结构

[英]Cannot Send A Struct Across Threads which has mpsc::Sender as field

我有一个结构,它的字段是Sender类型。

pub struct GenericConnectionPool<E>
where
    E: ConnectionConnector,
{
    _sender: Sender<()>,
    _reciever: Arc<Mutex<Receiver<()>>>,
    _num_of_live_connections: AtomicU8,
    _max_connections: u8,
    _min_connections: u8,
    _connections: Arc<Mutex<Vec<<E as ConnectionConnector>::Conn>>>,
    _connector: E,
}

我在多个线程中使用该结构,这就是为什么在 Arc 中使用它并克隆它的原因。

let pool = Arc::new(GenericConnectionPool::new(2, 1, cc));
println!("here");
{
    for _ in 0..3 {
        let pool = Arc::clone(&pool);
        std::thread::spawn(move || {
            pool.get_connection();
            thread::sleep(Duration::from_secs(1));
        });
    }
}

但是我收到了我的结构无法跨线程发送的错误。

`std::sync::mpsc::Sender<()>` cannot be shared between threads safely
within `GenericConnectionPool<tests::connector_works::DummyConnectionConnector>`, the trait `std::marker::Sync` is not implemented for `std::sync::mpsc::Sender<()>`
required because it appears within the type `GenericConnectionPool<tests::connector_works::DummyConnectionConnector>`
required because of the requirements on the impl of `std::marker::Send` for `std::sync::Arc<GenericConnectionPool<tests::connector_works::DummyConnectionConnector>>`
required because it appears within the type `[closure@src/lib.rs:169:36: 172:18 pool:std::sync::Arc<GenericConnectionPool<tests::connector_works::DummyConnectionConnector>>]`

我的理解是Sender类型不能安全地跨线程发送,但是由于它的cloneable ,您可以克隆它然后跨线程发送它。 但在我的情况下,发件人在我的结构中。 我想不出办法来解决这个问题。

我想我可能不得不改变我的设计。

你可以使用横梁。 它的crossbeam::Sender似乎可以在线程之间转移。 据推测,您也需要使用它的crossbeam::Receiver

或者,您可以将 GenericConnectionPool 重构为:

pub struct ExtraData {}

#[derive(Clone)]
pub struct GenericConnectionPool {
    _sender: Sender<()>,
    _extra_data: Arc<ExtraData>,
}

那么您可以直接克隆 GenericConnectionPool 而不是包含它的 Arc 并获得正确的行为:

let pool = GenericConnectionPool{_sender:s, _extra_data:Arc::new(ExtraData{}) };

for _ in 0..3 {
   let pool = pool.clone();
   std::thread::spawn(move || {
       pool.get_connection();
       thread::sleep(Duration::from_secs(1));
    });
}

您可以在此处查看编译版本:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM