hidapi icon indicating copy to clipboard operation
hidapi copied to clipboard

Documentation: add multi-threading notes

Open Youw opened this issue 6 years ago • 13 comments

need add notes to README regarding multithreading, e.g. regarding Windows issues/non-full support maybe some more

Youw avatar Jul 13 '19 19:07 Youw

NOTE for everyone: hidapi not thread-safe in general, with a few platform-specific/usage-spacific exceptions.

Damn, I need to get my hands to describe everything in details.

Youw avatar Jan 26 '21 11:01 Youw

Reference: Windows: unable to interrupt blocking hid_read call https://github.com/libusb/hidapi/issues/133

mcuee avatar Jun 15 '21 14:06 mcuee

I see this and #133 are tied to 0.13.0 milestone. Supposedly this one will "help" #133, right?

Any activities on this one?

#146 is supposed to fix #133, but I see no activities there.

mcuee avatar Oct 30 '22 05:10 mcuee

@Youw Just wondering what is the plan for 0.13.0 release, say as a Christmas gift. :-)

mcuee avatar Oct 30 '22 05:10 mcuee

0.13 already includes a lot of good improvements, so I guess making a release (before Christmas) would be a good thing regardless if it is going to include the multithreaded documentation/fixes or not

Youw avatar Oct 30 '22 12:10 Youw

0.13 already includes a lot of good improvements, so I guess making a release (before Christmas) would be a good thing regardless if it is going to include the multithreaded documentation/fixes or not

I agree. Thanks.

mcuee avatar Oct 30 '22 12:10 mcuee

@Youw

It is great that you have released hidapi 0.13. Thanks.

mcuee avatar Jan 06 '23 08:01 mcuee

Reference by Youw.

  • https://github.com/libusb/hidapi/issues/202#issuecomment-716061756

As for multithreaded usage of hidapi - many projects successfully using a separate thread for hid_read/_timeout, and another thread for read/everything else.

mcuee avatar May 10 '23 11:05 mcuee

Another reference by Youw.

  • https://github.com/libusb/hidapi/issues/133#issuecomment-567060164

client code should first stop any read/write attempts before trying to close the device as hid_close cannot not be thread-safe by design, even it it is sometime, for some implementations.

mcuee avatar May 10 '23 12:05 mcuee

Add a stub FAQ entry here. https://github.com/libusb/hidapi/wiki#faq

mcuee avatar May 10 '23 12:05 mcuee

Comments by Youw here.

  • https://github.com/trezor/cython-hidapi/issues/148#issuecomment-1542302679

Oh yes, HIDAPI on macOS has an additional issue regarding thread-safety. I did encountered it in my project(s) but didn't have enough time to gather info/find a root cause. Looks like on macOS hid_init/hid_exit needs to be called in the same thread, which is an additional restriction compared to other platforms.

mcuee avatar May 11 '23 07:05 mcuee

Reference from node-hid. https://github.com/node-hid/node-hid#thread-safety-worker-threads-context-aware-modules

Thread safety, Worker threads, Context-aware modules In general node-hid is not thread-safe because the underlying C-library it wraps (hidapi) is not thread-safe. However, node-hid is now reporting as minimally Context Aware to allow use in Electron v9+. Until node-hid (or hidapi) is rewritten to be thread-safe, please constrain all accesses to it via a single thread.

mcuee avatar May 14 '23 04:05 mcuee

Oh yes, HIDAPI on macOS has an additional issue regarding thread-safety. I did encountered it in my project(s) but didn't have enough time to gather info/find a root cause. Looks like on macOS hid_init/hid_exit needs to be called in the same thread, which is an additional restriction compared to other platforms.

I have a potential solution, described fully in #666

k1-801 avatar Mar 11 '24 20:03 k1-801