如何在多个线程上使用隐藏对象

How to use hid object on multiple threads

我正在使用 rust 使用 hid (https://crates.io/crates/hid) 库与 StreamDeck 交互。

我想:

由于从设备读取是阻塞的,我想在不同的线程上执行此操作。

为了与 hid 设备交互,它创建了一个 Handle (https://github.com/meh/rust-hid/blob/master/src/handle.rs#L8),它不能在线程之间移动,因为它包含一个 ptr: *mut hid_device.

(the trait `Send` is not implemented for `*mut c_void`)

有什么方法可以从多个线程使用句柄吗?一个我会读,另一个我会写。

可能有多种选择,但我建议使用渠道,例如 tokio's mpsc channels

想法是创建一个专用线程来与设备对话,并且只在该线程中使用 Handle。您想要接收或发送的事件和命令可以发布到相应的频道,并在您的主“控制器”线程中处理。

查看 this tutorial