-
Notifications
You must be signed in to change notification settings - Fork 398
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Documentation: add multi-threading notes #45
Comments
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 #133 |
@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. |
It is great that you have released hidapi 0.13. Thanks. |
Reference by Youw.
|
Another reference by Youw.
|
Add a stub FAQ entry here. |
Comments by Youw here.
|
Reference from node-hid. Thread safety, Worker threads, Context-aware modules |
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 signal11/hidapi#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... |
need add notes to README regarding multithreading, e.g. regarding Windows issues/non-full support
maybe some more
The text was updated successfully, but these errors were encountered: