- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个包含Arc<Receiver<f32>>
的结构,我试图添加一个获取self
所有权的方法,并将所有权移到新线程中并启动它。但是,我得到了错误
error[E0277]: the trait bound `std::sync::mpsc::Receiver<f32>: std::marker::Sync` is not satisfied
--> src/main.rs:19:9
|
19 | thread::spawn(move || {
| ^^^^^^^^^^^^^ `std::sync::mpsc::Receiver<f32>` cannot be shared between threads safely
|
= help: the trait `std::marker::Sync` is not implemented for `std::sync::mpsc::Receiver<f32>`
= note: required because of the requirements on the impl of `std::marker::Send` for `std::sync::Arc<std::sync::mpsc::Receiver<f32>>`
= note: required because it appears within the type `Foo`
= note: required because it appears within the type `[closure@src/main.rs:19:23: 22:10 self:Foo]`
= note: required by `std::thread::spawn`
Arc<i32>
或仅保留
Receiver<f32>
,则它将编译,但不使用
Arc<Receiver<f32>>
。这是如何运作的?该错误对我而言没有任何意义,因为我没有尝试在线程之间共享它(我正在移动它,而不是克隆它)。
use std::sync::mpsc::{channel, Receiver, Sender};
use std::sync::Arc;
use std::thread;
pub struct Foo {
receiver: Arc<Receiver<f32>>,
}
impl Foo {
pub fn new() -> (Foo, Sender<f32>) {
let (sender, receiver) = channel::<f32>();
let sink = Foo {
receiver: Arc::new(receiver),
};
(sink, sender)
}
pub fn run_thread(self) -> thread::JoinHandle<()> {
thread::spawn(move || {
println!("Thread spawned by 'run_thread'");
self.run(); // <- This line gives the error
})
}
fn run(mut self) {
println!("Executing 'run'")
}
}
fn main() {
let (example, sender) = Foo::new();
let handle = example.run_thread();
handle.join();
}
最佳答案
How does this work?
thread::spawn
的要求:
pub fn spawn<F, T>(f: F) -> JoinHandle<T>
where
F: FnOnce() -> T,
F: Send + 'static, // <-- this line is important for us
T: Send + 'static,
Foo
包含
Arc<Receiver<_>>
,因此让我们检查
Arc
implements Send
以及如何:
impl<T> Send for Arc<T>
where
T: Send + Sync + ?Sized,
Arc<T>
实现
Send
和
T
,则
Send
实现
Sync
。而
Receiver
implements Send
,
it does not implement Sync
。
Arc
对
T
有如此强烈的要求?
T
还必须实现
Send
,因为
Arc
可以充当容器;如果您只能在
Send
中隐藏未实现
Arc
的内容,请将其发送到另一个线程并在其中解压缩...可能会发生不好的事情。有趣的部分是看看
T
为什么还必须实现
Sync
,这显然也是您正在努力的部分:
The error doesn't make sense to me as I'm not trying to share it between threads (I'm moving it, not cloning it).
Arc
中的
Foo
实际上并未共享。考虑一下您是否稍后将
#[derive(Clone)]
添加到
Foo
(这可能没有问题):
fn main() {
let (example, sender) = Foo::new();
let clone = example.clone();
let handle = example.run_thread();
clone.run();
// oopsie, now the same `Receiver` is used from two threads!
handle.join();
}
Receiver
。这是不好的,因为
Receiver
没有实现
Sync
!
Arc
?正如您所注意到的,没有
Arc
,它可以正常工作而没有问题:您清楚地声明
Foo
是
Receiver
的唯一所有者。而且,如果您仍然“不尝试共享[接收者]”,那么拥有多个所有者是没有意义的。
关于multithreading - 在将包含Arc的self移到新线程中时,为什么会出现 “Sync is not satisfied”错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63094640/
我有一个 jqgrid,在设置多重检查时,我在第一列上获得复选框,我希望该复选框列成为最后一列。 我没有找到任何选项,因此我正在编写一个自定义 jquery 方法来将 tr 的第一个 td 移动到最后
在编程类(class)中,我被要求创建一个 iCalendar 应用程序的副本。我正在使用 JAVA 对其进行编码,并使用 JFrame 和 JPanel 对其进行绘制。这是我的问题的 SSCCEE:
想把我的 snackbar 移到 bottomnavigationview 上面
我正在用 Objective-C 创建一个聊天应用程序。我的对话面板有两个项目,文本字段和将显示消息的表格。我正在为文本字段及其按钮使用 UIView,此 View 位于屏幕底部。每当我开始打字时,键
该方法用于选择User和TargetUser之间的 Action 数,例如就在两个用户之间。此方法的结果值取决于 GetTotalOfPossibleActions() 动态返回值(每个用户都有自己的
我有一个关于 owl api 用法的快速问题。 假设我有一个名为 Species 的类,它有一个哺乳动物子类,它有一个灵长类动物子类,它有一个人类子类。 物种 -> 哺乳动物 -> 灵长类动物 ->
我需要 eclipse 内容帮助将“java.lang.String”放置在以“string”开头的类型之上(见附图)。那可能吗? 请注意,对于此问题,“string”必须首先输入非大写的“s”。大写
我正在研究 pdf 页面上的收缩和收缩功能。我的捏合和平移(移动)工作正常,但是当用户连续移动缩放 View 时,缩放 View 超出了 super View 边界。是这样的: 我如何限制平移移动,以
一般 typescript 问题说我迭代一个我知道它的内容的数组并应用一个reduce来取回一个我知道类型的对象 例如: interface IMyInterface { a: number;
我有一个仪表板,您可以在其中使用小部件选择多个数据集。我使用 Python 中的 Altair 库从这些数据集中制作经典折线图,x 轴和 y 轴均从零开始。显示的数据是标准化数据,这意味着根据定义,所
首先,我没有这样做的经验。但是就像任何好的程序的开始一样,我有需要解决的问题,所以我愿意学习。 你们中的许多人可能已经熟悉 pdftk,它是处理各种 pdf 相关任务的便捷实用程序。据我所知,这些功能
我是一名优秀的程序员,十分优秀!