Threading with Phidget 2.2 API

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

Threading with Phidget 2.2 API

Post 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.
User avatar
mparadis
Site Admin
Posts: 660
Joined: Fri Oct 28, 2011 12:17 pm
Contact:

Re: Threading with Phidget 2.2 API

Post 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).
User avatar
Patrick
Lead Developer
Posts: 634
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

Re: Threading with Phidget 2.2 API

Post 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
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests