multithreading - 如何为 RustBox 实现 Sync?

标签 multithreading rust thread-safety

我是 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/

相关文章:

c++ - 随机文件写入

c - 在 OpenMP 中将结构体指针定义为 threadprivate

iterator - 如何向 Iterator 添加新方法?

java - 在构造函数中设置父子关系而不泄漏 "this"变量

Java线程: synchronized blocks

java - 如何检查invokelater是否完成

java - 在同一对象上使用不同的操作(方法)创建死锁情况?

rust - 如何安全地使用由唯一 ID 表示的外部数据?

closures - 从函数返回一个闭包

delphi - 我可以在非 GUI 线程中读取 VCL 控件的属性吗?