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

Multithreading 无法跨具有mpsc::Sender as字段的线程发送结构,multithreading,rust,Multithreading,Rust,我有一个结构,它的字段是Sendertype pub-struct-GenericConnectionPool 哪里 E:连接连接器, { _发送者:发送者, _接受者:Arc, _实时连接的数量:AtomicU8, _最大连接数:u8, _最小连接数:u8, _连接:Arc, _连接器:E, } 我在多个线程中使用这个结构,这就是为什么在Arc内部使用它并克隆它 let pool=Arc::new(GenericConnectionPool::new(2,1,cc)); 普林顿!(“此处”)

我有一个结构,它的字段是
Sender
type

pub-struct-GenericConnectionPool
哪里
E:连接连接器,
{
_发送者:发送者,
_接受者:Arc,
_实时连接的数量:AtomicU8,
_最大连接数:u8,
_最小连接数:u8,
_连接:Arc,
_连接器:E,
}
我在多个线程中使用这个结构,这就是为什么在Arc内部使用它并克隆它

let pool=Arc::new(GenericConnectionPool::new(2,1,cc));
普林顿!(“此处”);
{
对于0..3中的uu{
让pool=Arc::克隆(&pool);
std::thread::spawn(移动| |){
pool.get_connection();
线程::睡眠(持续时间::从秒(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>>]`
`std::sync::mpsc::Sender`无法在线程之间安全共享
在“GenericConnectionPool”中,“std::Sync::mpsc::Sender”未实现特性“std::marker::Sync”`
必需,因为它出现在“GenericConnectionPool”类型中`
由于对`std::sync::Arc`的`std::marker::Send` impl的要求,因此需要此选项`
必需,因为它出现在类型中`[closure@src/库:169:36:172:18池:标准::同步::弧]`
我的理解是,
发送方
类型不能跨线程安全发送,但由于它的
可克隆性
,您可以克隆它,然后跨线程发送。但在我的情况下,发送方在我的结构中。我想不出一个办法来解决这个问题


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

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

或者,您可以重构GenericConnectionPool,如下所示:

pub struct ExtraData {}

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

您可以看到一个编译版本:

这正是我决定使用的。但我也会看看你提到的横梁库。非常感谢。这回答了你的问题吗?
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));
    });
}