gpt4 book ai didi

multithreading - 如何为 RustBox 实现 Sync?

转载 作者:行者123 更新时间:2023-11-29 08:18:39 26 4
gpt4 key购买 nike

我是 Rust 线​​程的新手。我正在努力通过 RustBox type来自 rustbox线程内的 crate 。

我想按下 q 键并让它在 (1, 1) 处显示一个 + 符号 2 秒,同时按下 w 在 (1, 2) 处显示另一个 + 符号的 2 秒内键入。

我为同样的逻辑写了一些代码:

extern crate rustbox;

use std::thread;
use std::time::Duration;

use rustbox::{Color, RustBox};
use rustbox::Key;


fn mark_at(x: usize, y: usize, rustbox: &RustBox) {
rustbox.print(x, y, rustbox::RB_BOLD, Color::Black, Color::White, "+");
thread::spawn(move || {
let delay = Duration::from_millis(2000);
thread::sleep(delay);
rustbox.print(x, y, rustbox::RB_BOLD, Color::Black, Color::White, " ");
});
}


fn main() {
let rustbox = match RustBox::init(Default::default()) {
Result::Ok(v) => v,
Result::Err(e) => panic!("{}", e),
};
rustbox.print(1, 1, rustbox::RB_BOLD, Color::Black, Color::White, " ");
rustbox.print(1, 2, rustbox::RB_BOLD, Color::Black, Color::White, " ");

loop {
rustbox.present();
match rustbox.poll_event(false) {
Ok(rustbox::Event::KeyEvent(key)) => {
match key {
Key::Char('q') => {
mark_at(1, 1, &rustbox);
}
Key::Char('w') => {
mark_at(1, 2, &rustbox);
}
Key::Esc => { break; }
_ => { }
}
},
Err(e) => panic!("{}", e),
_ => { }
}
}
}

它给了我:

error[E0277]: the trait bound `*mut (): std::marker::Sync` is not satisfied in `rustbox::RustBox`
--> src/main.rs:12:5
|
12 | thread::spawn(move || {
| ^^^^^^^^^^^^^ `*mut ()` cannot be shared between threads safely
|
= help: within `rustbox::RustBox`, the trait `std::marker::Sync` is not implemented for `*mut ()`
= note: required because it appears within the type `std::marker::PhantomData<*mut ()>`
= note: required because it appears within the type `rustbox::RustBox`
= note: required because of the requirements on the impl of `std::marker::Send` for `&rustbox::RustBox`
= note: required because it appears within the type `[closure@src/main.rs:12:19: 16:6 rustbox:&rustbox::RustBox, x:usize, y:usize]`
= note: required by `std::thread::spawn`

error: aborting due to previous error

如何为 RustBox 类型实现 Sync 以便上面的代码可以工作?

最佳答案

RustBox没有实现 Send ,因此没有(安全的)方式在线程之间共享它(似乎您已经找到了尚未合并的拉取请求 https://github.com/gchp/rustbox/pull/65 它将支持 Send)。

如果拉取请求被合并,您可以包装 RustBox Mutex ,即 Mutex<RustBox> ,以及对它的引用可以在线程之间共享。

但随后您会遇到终身问题:您的 rustbox引用没有足够长的时间使用它来生成新线程,因此您必须将它包装在 Arc 中.

use std::sync::{Arc,Mutex};

fn mark_at(x: usize, y: usize, rustbox: &Arc<Mutex<RustBox>>) {
rustbox.lock().unwrap().print(x, y, rustbox::RB_BOLD, Color::Black, Color::White, "+");
let rustbox = rustbox.clone(); // increment reference counter
thread::spawn(move || {
let delay = Duration::from_millis(2000);
thread::sleep(delay);
rustbox.lock().unwrap().print(x, y, rustbox::RB_BOLD, Color::Black, Color::White, " ");
});
}

在你的main函数你必须包装 rustbox :

let rustbox = Arc::new(Mutex::new(rustbox));

lock()每次使用它。

确保你不要把锁放得太久;使用辅助变量可能有助于使其更明确,例如:

let pe = rustbox.lock().unwrap().poll_event(false);
match pe {
// ...
}

关于multithreading - 如何为 RustBox 实现 Sync?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46851258/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com