Page 1 of 1

Threading with Phidget 2.2 API

Posted: Tue Jun 23, 2020 12:05 pm
by somecoder
Question: Are The Phidget 2.2 API handles threadsafe? Can phidget calls (other than create/open/close/delete) be made on the same handle from multiple threads at the same time? or does the application itself need to mutex calls for the same handle or does the application need to mutex ALL calls into the API?

With the Phidget 2.1 API, the library worked in multiple threads by having each thread own its own handle to the same channels. e.g. My C++ code under windows could open two different handles to the same Interfacekit. One thread could monitor inputs and set/clear output channels. A user controlled dialog in a main U/I thread could monitor and display current states of input/output channels.

With the 2.2 API, the ability to open a channel multiple times is specifically disallowed (documented here: https://www.phidgets.com/docs/Attaching_a_Channel) Attempting to attach to the same channel a second time seems to wait indefinitely and timeout. Also, opening a channel clears its state back to 0, so it doesn't seem possible to just open an output channel to read its current or last set state anyway as you always get 0 for a newly opened channel.

Re: Threading with Phidget 2.2 API

Posted: Tue Jun 23, 2020 1:12 pm
by mparadis
In order to have a Phidget channel in Phidget22 open in multiple programs or threads, you need to open all connections remotely over the Network Server.

This will also solve the problem of the channel properties resetting on open, as long as one of the connections is constantly open.

Note that certain channel classes do not support multiple connections, even over the network server (Motor Controllers, for example).

Re: Threading with Phidget 2.2 API

Posted: Wed Jun 24, 2020 11:38 am
by Patrick
Phidget22 is thread safe. The correct solution here is to open a single connection to your channel and share this handle/object among your threads. You are expected to keep the channel open - once the channel is closed, it will revert to it's default/reset state, which is a change from phidget21.

-Patrick