Threading with Phidget 2.2 API

Comments & issues
Fresh meat
Posts: 1
Joined: Tue Jun 23, 2020 11:39 am

Threading with Phidget 2.2 API

Postby somecoder » Tue Jun 23, 2020 12:05 pm

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

User avatar
Site Admin
Posts: 784
Joined: Fri Oct 28, 2011 12:17 pm

Re: Threading with Phidget 2.2 API

Postby mparadis » Tue Jun 23, 2020 1:12 pm

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

User avatar
Lead Developer
Posts: 3215
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada

Re: Threading with Phidget 2.2 API

Postby Patrick » Wed Jun 24, 2020 11:38 am

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.


Return to “General”

Who is online

Users browsing this forum: No registered users and 4 guests