Documentation: add multi-threading notes #45
Labels
No labels
3rdparty
Android
API
Author does not respond
bsd
bug
build system/CI
ci-virtual-device
CMake
Contributions Welcome
Core
dependencies
device-specific
documentation
don't_merge
duplicate
enhancement
Example
Haiku
hidraw
hotplug
info required
invalid
libusb
macOS
Misc
Not an issue
question
Release
Test
Test GUI
Windows
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
gh-libusb/hidapi#45
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
need add notes to README regarding multithreading, e.g. regarding Windows issues/non-full support
maybe some more
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.
Reference: Windows: unable to interrupt blocking hid_read call https://github.com/libusb/hidapi/issues/133
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.
@Youw
Just wondering what is the plan for 0.13.0 release, say as a Christmas gift. :-)
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.
@Youw
It is great that you have released hidapi 0.13. Thanks.
Reference by Youw.
Another reference by Youw.
Add a stub FAQ entry here.
https://github.com/libusb/hidapi/wiki#faq
Comments by Youw here.
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-hidis not thread-safe because the underlying C-library it wraps (hidapi) is not thread-safe. However,node-hidis now reporting as minimally Context Aware to allow use in Electron v9+. Untilnode-hid(orhidapi) is rewritten to be thread-safe, please constrain all accesses to it via a single thread.I have a potential solution, described fully in #666
I found this old discussion on multithreading in hidapi maybe it helps in writing the documentation https://github.com/signal11/hidapi/issues/72
In my usecase, I will interact with one device on one thread always, but I want to interact with different devices on different threads simultaneously.
Are all hidapi implementations at least thread-safe enough for this?
I don't need to be able to talk to one device from multiple threads simultaneously and I would think that makes sense only very rarely anyways. I observed that for instance on Windows, multiple threads and programs talking to the same device at the same time are allowed, but the order in which the reports are received in each direction gets garbled up. Even on a conceptual level talking to one device from multiple threads doesn't make much sense.
But talking to different devices on different threads/programs seems natural and should be possible for performance reasons. It should make no difference whether I use program x to talk to device a and program y to talk to device b, or simply two threads 1&2 in a single program to talk to devices a&b.
Do the hidapi implementations guarantee that this works?
You can enumerate and open the devices in a main thread, and once opened successful, you can move the device handle into a communication thread per device, where you do all read/write ops and finally close the device.
Thanks for the feedback @JoergAtGithub. I don't even need or want to open the devices on that main thread, but yes i will need to enumerate them once in a main thread to get the vid/pid/usb serials I am interested in.
So do the hidapi implementations guarantee that then opening the devices in the different communication threads is safe, even if, say, I were to do another enumeration again on the main thread simultaneously?
I have the feeling I saw some caching mechanisms in some of the hidapi implementations (having to do with USB characteristics?) that make me doubt this is thread-safe...
So yeah my requirement would be that there can be one main thread doing enumeration (potentially in a loop, forever), while other threads communicate with the existing devices undisturbed, one thread per device. Well maybe some threads will handle more than one device, but no device will be opened by more than one thread.
I currently make sure to do absolutely all hidapi work in a single thread and that obviously works fine, but I am afraid to break things in moving past that for performance reasons...
I would need pretty strong guarantees to be confident this works in all implementations and will continue to do so, i.e. hidapi would have to commit to and document some level of some notion of this kind of "per device" & single-enumeration thread-safety...
Note I don't need that enumeration itself is thread safe - there's no point in enumerating in more than one thread simultaneously.
Short of that guarantee, i would imagine the only other safe option I would have is to enumerate in a main program, then spawn a separate process per device. I would imagine the operating systems guarantee this works fine when each process talks to another device and I don't imagine any hidapi implementation uses some system wide shared memory in some misguided attempt to share some cached data/state...
I dug out https://github.com/signal11/hidapi/issues/72 maybe it helps.
Sorry for ignoring this for so long.
@Masterxilo, yes it is safe to use different hidapi devices in different threads with one note: in general (croll-platform case) all
hid_open/hid_closeoperations should be performed in the same thread (or at least serialized with) ashid_enumerate. This limitation comes from the least "safe" platform for this case - macOS. HidManager is not thread-safe to be used and it is used by all of hid_init/hid_exit/hid_enumerate/hid_open/hid_close.So you can:
I think I'm finally getting my hands to https://github.com/libusb/hidapi/wiki/Multi%E2%80%90threading-Notes/
Closing in favor of https://github.com/libusb/hidapi/wiki/Multi%E2%80%90threading-Notes
If I've missed something obvious - will update later.
Thank you very much for documenting this!
Could you please explain why this is the case? This is not obvious to me.
See the
NOTEabouthid_closehere.That's one way to enforce it.