Skip to content

Documentation: add multi-threading notes #45

Closed

Description

@Youw

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

Activity

  1. added
    documentation
    enhancement
    on Jul 13, 2019
  2. self-assigned this
    on Jul 13, 2019
  3. pinned this issue on Jan 26, 2021
  4. Youw commented on Jan 26, 2021

    @Youw
    MemberAuthor

    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.

  5. mcuee commented on Jun 15, 2021

    @mcuee
    Member

    Reference: Windows: unable to interrupt blocking hid_read call #133

  6. unpinned this issue on Mar 16, 2022
  7. pinned this issue on Mar 16, 2022
  8. unpinned this issue on May 16, 2022
  9. pinned this issue on May 16, 2022
  10. unpinned this issue on Oct 13, 2022
  11. pinned this issue on Oct 13, 2022
  12. mcuee commented on Oct 30, 2022

    @mcuee
    Member

    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.

  13. mcuee commented on Oct 30, 2022

    @mcuee
    Member

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

  14. Youw commented on Oct 30, 2022

    @Youw
    MemberAuthor

    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

  15. 6 remaining items

  16. mcuee commented on May 14, 2023

    @mcuee
    Member

    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.

  17. k1-801 commented on Mar 11, 2024

    @k1-801
    Contributor

    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

  18. Masterxilo commented on Nov 16, 2024

    @Masterxilo

    I found this old discussion on multithreading in hidapi maybe it helps in writing the documentation signal11/hidapi#72

  19. Masterxilo commented on Nov 16, 2024

    @Masterxilo

    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?

  20. JoergAtGithub commented on Nov 16, 2024

    @JoergAtGithub
    Contributor

    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.

  21. Masterxilo commented on Nov 16, 2024

    @Masterxilo

    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.

  22. Masterxilo commented on Nov 16, 2024

    @Masterxilo

    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...

  23. fricpa commented on Dec 5, 2024

    @fricpa

    I dug out signal11/hidapi#72 maybe it helps.

  24. Youw commented on Jan 23, 2025

    @Youw
    MemberAuthor

    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_close operations should be performed in the same thread (or at least serialized with) as hid_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:

    • enumerate / open all you devices in one thread
    • use each device in its separate thread
    • close all devices in the same thread you've opened them
  25. Youw commented on Jan 23, 2025

    @Youw
    MemberAuthor
  26. JoergAtGithub commented on Jan 23, 2025

    @JoergAtGithub
    Contributor

    Thank you very much for documenting this!

    • close all devices in the same thread you've opened them

    Could you please explain why this is the case? This is not obvious to me.

  27. Youw commented on Jan 23, 2025

    @Youw
    MemberAuthor

    Could you please explain why this is the case? This is not obvious to me.

    See the NOTE about hid_close here.

    close all devices in the same thread you've opened them

    That's one way to enforce it.

  28. unpinned this issue on Mar 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

documentationenhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions