rust - 如何在rust中的多个线程上传递具有特征的数据结构?
问题描述
我正在尝试存储我的应用程序的状态,其中包括在开始时初始化并在多个线程之间传递状态的特征列表,但我收到错误。
use std::sync::{Arc, RwLock};
use std::thread;
use std::time::Duration;
trait TestTrait {
fn test(&self);
}
struct MyTestStruct {}
impl TestTrait for MyTestStruct {
fn test(&self) {
println!("Test trait called");
}
}
struct DataStore {
t: Box<dyn TestTrait>,
}
fn main() {
let s = Arc::new(RwLock::new(DataStore {
t: Box::new(MyTestStruct {}),
}));
let s_clone = s.clone();
thread::spawn(move || {
for i in 1..10 {
s_clone.read().unwrap().t.test();
println!("hi number {} from the spawned thread!", i);
thread::sleep(Duration::from_millis(1));
}
});
for i in 1..5 {
println!("hi number {} from the main thread!", i);
thread::sleep(Duration::from_millis(1));
}
}
当我运行它时,我收到以下错误:
error[E0277]: `(dyn TestTrait + 'static)` cannot be sent between threads safely
--> src/main.rs:26:5
|
26 | thread::spawn(move || {
| ^^^^^^^^^^^^^ `(dyn TestTrait + 'static)` cannot be sent between threads safely
|
= help: the trait `std::marker::Send` is not implemented for `(dyn TestTrait + 'static)`
= note: required because of the requirements on the impl of `std::marker::Send` for `std::ptr::Unique<(dyn TestTrait + 'static)>`
= note: required because it appears within the type `std::boxed::Box<(dyn TestTrait + 'static)>`
= note: required because it appears within the type `DataStore`
= note: required because of the requirements on the impl of `std::marker::Send` for `std::sync::RwLock<DataStore>`
= note: required because of the requirements on the impl of `std::marker::Send` for `std::sync::Arc<std::sync::RwLock<DataStore>>`
= note: required because it appears within the type `[closure@src/main.rs:26:19: 32:6 s_clone:std::sync::Arc<std::sync::RwLock<DataStore>>]`
error[E0277]: `(dyn TestTrait + 'static)` cannot be shared between threads safely
--> src/main.rs:26:5
|
26 | thread::spawn(move || {
| ^^^^^^^^^^^^^ `(dyn TestTrait + 'static)` cannot be shared between threads safely
|
= help: the trait `std::marker::Sync` is not implemented for `(dyn TestTrait + 'static)`
= note: required because of the requirements on the impl of `std::marker::Sync` for `std::ptr::Unique<(dyn TestTrait + 'static)>`
= note: required because it appears within the type `std::boxed::Box<(dyn TestTrait + 'static)>`
= note: required because it appears within the type `DataStore`
= note: required because of the requirements on the impl of `std::marker::Sync` for `std::sync::RwLock<DataStore>`
= note: required because of the requirements on the impl of `std::marker::Send` for `std::sync::Arc<std::sync::RwLock<DataStore>>`
= note: required because it appears within the type `[closure@src/main.rs:26:19: 32:6 s_clone:std::sync::Arc<std::sync::RwLock<DataStore>>]`
我该如何解决?
解决方案
thread::spawn
需要一个实现Send
. Arc<T>
仅Send
当T
同时实现Send
和时才实现Sync
。
Send
并且Sync
是自动特征。您可以将自动特征添加到dyn Trait
类型以提供有关已擦除类型的更多信息:
struct DataStore {
t: Box<dyn TestTrait + Send + Sync>,
}
如果你要dyn TestTrait + Send + Sync
到处写,那么另一种选择是将Send
和声明Sync
为 的超特征TestTrait
:
trait TestTrait: Send + Sync {
fn test(&self);
}
推荐阅读
- powershell - 如何在 PowerShell 中使用 GUI 使用最大化分组框?
- python - 调用 wx.frame 类方法来处理来自框架面板上的按钮的事件
- html - 在 iPhone8、iPhone11 上显示透明背景的引导模式
- functional-interface - 功能接口在 Eclipse 中给出错误
- angular - 无法预测 Angular 中扫描仪或键盘的输入
- javascript - D3 Charting Tool:如何在柱形图中的目标线(附加水平线)右侧添加标签
- ubuntu - k8 在 bazel info local_cpu_resources 的输出中意味着什么
- java - 如何在 Android 的 TextView 和 EditText 中显示来自 API 服务器的 JSON 数据?
- python - 如何编写for循环来查找Python中每一行的多列中存在或不存在的特定数字?
- php - 从值创建数组键。我已经创建了数组定义到波纹管。我区分了实际结果和预期结果