Repository navigation
Multi‐threading Notes
HIDAPI v0.x.x is not thread-safe.
There are a few specific cases that softens the above statement and this document describes those from the most strict to least strict.
Some of the HIDAPI functions may be used from different threads and the below describes in which cases.
hid_init/hid_exit/hid_enumerate/hid_open/hid_open_path/hid_close/hid_error(NULL) shall not be called concurently from different threads. Yes, in general case, it is not even safe to open different devices in different threads concurently.
One of the reasons is that in case of any of those function fails - a global error string will be updated (which can be accessed with hid_error(NULL)).
Call hid_init() before starting threads that use HIDAPI. Implicit initialization
exists, but must not be relied upon when several threads may make the first call.
Call hid_exit() last, after all other calls have returned and all devices are
closed.
macOS: on macOS there is even stricter requirement: hid_init and hid_exit has to be called from the same thread. See #666 for discussion.
And the thread where hid_init has been called should not be exited until all HIDAPI devices are closed and hid_exit is called.
All functions that accept hid_device as a first argument are to be referenced as a "device functions".
Device functions are not individually thread-safe. Different devices may be used from different threads, for example one dedicated thread per device. Concurrent use of the same device is supported only for the dedicated-reader and read-interruption cases described below.
hid_close is the exception here - it should be serialized with initialisation/enumeration functions described in the previous section.
Even though it is perfectly safe to have a separate thread to handle a specific hid_device it is often desirable to have a another dedicated thread to handle hid_read/hid_read_timeout specifically.
Since v0.16.0, hid_read_interrupt() can wake a blocked hid_read() or
hid_read_timeout() call. See the read-interruption rules below.
Before v0.15.0 many projects where successfully using one thread for hid_read and another thread for all other device functions. See previous description for potential problems with this on older versions of HIDAPI.
Since v0.15.0 such scenario/usage is perfectly safe for as long as only hid_read/hid_read_timeout and hid_read_error are the only functions to be used from a dedicated thread. hid_error is no longer affected by hid_read and should not be used concurently to other device functions. hid_read_error on the other hand is specifically designed to be used in the same thread as hid_read.
hid_read_interrupt(dev) sets a sticky interrupted state and wakes a read waiting
for input. An interrupted read returns -1; use hid_read_error(dev) on the reader
thread for its diagnostic. A read already in progress may still return queued
input. Reads entered after hid_read_interrupt() returns fail with -1 until
hid_read_clear_interrupt(dev) clears the state. Queued input remains available
after clearing, subject to the backend's buffer limits.
hid_is_read_interrupted(dev) returns 1 while interrupted and 0 otherwise.
Calls to interrupt, query and clear on the same device must be serialized against
one another, including repeated calls to the same function. They may run alongside
device I/O, but must not overlap hid_close() or hid_exit(). Keep the device
alive until every call using it has returned.
To stop a dedicated reader, interrupt it, join the reader thread, then close the device. To resume reading deterministically, clear only after the active read returns. Interruption affects reads; writes and feature/output report operations continue normally.