signal11 / hidapi

A Simple library for communicating with USB and Bluetooth HID devices on Linux, Mac, and Windows.
http://www.signal11.us/oss/hidapi/
Other
2.45k stars 898 forks source link

thread safe? #45

Open dsula opened 12 years ago

dsula commented 12 years ago

WINDOWS I'm having problems when running hid_enumerate and hid_read/hid_write in different threads. I'm not sure if hidapi is supposed to be thread safe and the problem is in fact in my firmware device code, or if this is hidapi that's the problem.

Sooner or later my host code doesn't work anymore as the hid_read/hid_write fail and this only happens while a 2nd thread using hid_enumerate is active.

signal11 commented 12 years ago

The Windows implementation is not entirely thread safe. There should not be a problem connecting on the main thread and then reading/writing on a separate thread though.

Alan.

dsula commented 12 years ago

Ok, I found the problem to be in fact an issue in the device firmware. However I still wonder, what do you mean by the windows version is not entierly safe? I have 1 thread constantly running hid_enumerate to dynamically manage device connect/disconnect. Device IO is done by other threads. Do you see a potential problem with this?

signal11 commented 12 years ago

I think that should be fine.

michalmmm commented 11 years ago

Windows implementation is not thread-safe at all.

Simply because (but not only) register_error function is not thread safe and that function is actually called from almost everywhere in hid.c.

To resolve it the following block would have be made thread safe: LocalFree(device->last_error_str); device->last_error_str = msg; otherwise LocalFree function could be called more than once and would fail when called for the second time.

signal11 commented 11 years ago

More than that would need to be changed.