SDL2、FnMut 和 mpsc,发送者不能在线程间安全共享
SDL2, FnMut and mpsc, sender can not be shared safely between threads
我想用 sdl2-rs
板条箱启动一个计时器来执行绘图调用。我想通过做这样的事情来开始它:
extern crate sdl2;
use std::sync::mpsc;
enum Event {
Draw,
}
fn main() {
let sdl_context = sdl2::init().unwrap();
let video_subsystem = sdl_context.video().unwrap();
video_subsystem.gl_attr().set_context_version(4, 5);
println!(
"Current gl version: {:?}",
video_subsystem.gl_attr().context_version()
);
let timer_subsystem = sdl_context.timer().unwrap();
let window = video_subsystem
.window("rust-sdl2 demo: Video", 800, 600)
.position_centered()
.opengl()
.build()
.unwrap();
let context = window.gl_create_context().unwrap();
let (tx, rx) = mpsc::channel();
{
let timer_tx = tx.clone();
timer_subsystem.add_timer(
1000u32 / 120u32,
Box::new(move || {
timer_tx.send(Event::Draw);
1000u32 / 120u32
}),
);
}
}
但是,我得到这个错误:
error[E0277]: the trait bound `std::sync::mpsc::Sender<Event>: std::marker::Sync` is not satisfied in `[closure@src/main.rs:33:22: 36:14 timer_tx:std::sync::mpsc::Sender<Event>]`
--> src/main.rs:33:13
|
33 | / Box::new(move || {
34 | | timer_tx.send(Event::Draw);
35 | | 1000u32 / 120u32
36 | | }),
| |______________^ `std::sync::mpsc::Sender<Event>` cannot be shared between threads safely
|
= help: within `[closure@src/main.rs:33:22: 36:14 timer_tx:std::sync::mpsc::Sender<Event>]`, the trait `std::marker::Sync` is not implemented for `std::sync::mpsc::Sender<Event>`
= note: required because it appears within the type `[closure@src/main.rs:33:22: 36:14 timer_tx:std::sync::mpsc::Sender<Event>]`
= note: required for the cast to the object type `std::ops::FnMut() -> u32 + std::marker::Sync`
我知道发件人不是 Sync
所以我克隆它并将克隆的对象移动到 FnMut
闭包中,但无论如何它都不起作用。我怎样才能做到这一点?根据我的理解,通过将对象移动到闭包中,我们 不 共享它,因此它必须以这种方式工作。此外,文档中的示例也是如此。
克隆的发件人与原始发件人的类型相同,因此仍然不是Sync
。 add_timer
函数需要一个 Sync
的闭包,因此您需要将 sender 包装在 Mutex
中,这使您的 sender 可以安全地在线程之间共享。
let timer_tx = Mutex::new(tx.clone());
timer_subsystem.add_timer(
1000u32 / 120u32,
Box::new(move || {
timer_tx.lock().unwrap().send(Event::Draw);
1000u32 / 120u32
}),
);
我想用 sdl2-rs
板条箱启动一个计时器来执行绘图调用。我想通过做这样的事情来开始它:
extern crate sdl2;
use std::sync::mpsc;
enum Event {
Draw,
}
fn main() {
let sdl_context = sdl2::init().unwrap();
let video_subsystem = sdl_context.video().unwrap();
video_subsystem.gl_attr().set_context_version(4, 5);
println!(
"Current gl version: {:?}",
video_subsystem.gl_attr().context_version()
);
let timer_subsystem = sdl_context.timer().unwrap();
let window = video_subsystem
.window("rust-sdl2 demo: Video", 800, 600)
.position_centered()
.opengl()
.build()
.unwrap();
let context = window.gl_create_context().unwrap();
let (tx, rx) = mpsc::channel();
{
let timer_tx = tx.clone();
timer_subsystem.add_timer(
1000u32 / 120u32,
Box::new(move || {
timer_tx.send(Event::Draw);
1000u32 / 120u32
}),
);
}
}
但是,我得到这个错误:
error[E0277]: the trait bound `std::sync::mpsc::Sender<Event>: std::marker::Sync` is not satisfied in `[closure@src/main.rs:33:22: 36:14 timer_tx:std::sync::mpsc::Sender<Event>]`
--> src/main.rs:33:13
|
33 | / Box::new(move || {
34 | | timer_tx.send(Event::Draw);
35 | | 1000u32 / 120u32
36 | | }),
| |______________^ `std::sync::mpsc::Sender<Event>` cannot be shared between threads safely
|
= help: within `[closure@src/main.rs:33:22: 36:14 timer_tx:std::sync::mpsc::Sender<Event>]`, the trait `std::marker::Sync` is not implemented for `std::sync::mpsc::Sender<Event>`
= note: required because it appears within the type `[closure@src/main.rs:33:22: 36:14 timer_tx:std::sync::mpsc::Sender<Event>]`
= note: required for the cast to the object type `std::ops::FnMut() -> u32 + std::marker::Sync`
我知道发件人不是 Sync
所以我克隆它并将克隆的对象移动到 FnMut
闭包中,但无论如何它都不起作用。我怎样才能做到这一点?根据我的理解,通过将对象移动到闭包中,我们 不 共享它,因此它必须以这种方式工作。此外,文档中的示例也是如此。
克隆的发件人与原始发件人的类型相同,因此仍然不是Sync
。 add_timer
函数需要一个 Sync
的闭包,因此您需要将 sender 包装在 Mutex
中,这使您的 sender 可以安全地在线程之间共享。
let timer_tx = Mutex::new(tx.clone());
timer_subsystem.add_timer(
1000u32 / 120u32,
Box::new(move || {
timer_tx.lock().unwrap().send(Event::Draw);
1000u32 / 120u32
}),
);