Open and close a channel from different threads

Comments & issues
Supporting Windows 8 or newer
HjP
Fresh meat
Posts: 3
Joined: Sat Jun 14, 2025 12:56 am

Open and close a channel from different threads

Post by HjP »

Is it possible to open a channel from one thread and then close it from a different one?
Sorry for the contextless question. I just want to check, how much effort I shall put into investigating why I have a crash in the "Phidget22 Central Thread" when I do the above. When opening and closing from the same thread, everything works fine.
If you confirm to me that it should be possible in principle, I will of course provide more information.
User avatar
Patrick
Lead Developer
Posts: 674
Joined: Mon Jun 20, 2005 8:46 am
Location: Calgary

Re: Open and close a channel from different threads

Post by Patrick »

The library is thread safe, I would be interested in your set up. You may run into a deadlock if you try to close a channel from one of it's event callbacks, but you should never see a crash.

-Patrick
HjP
Fresh meat
Posts: 3
Joined: Sat Jun 14, 2025 12:56 am

Re: Open and close a channel from different threads

Post by HjP »

Based on a configuration file, a computing cell in my framework loads various modules, and from these modules creates a number of objects which represent the sensors, actuators, algorithms and the user interface(s) of an application. The creation of the object happens in parallel, with the threads coming from a thread pool. They are not permanently attached to the objects they create.
When the computing cell shuts down - which does not mean that the host application is shutting down - the objects are destroyed, again in parallel. During this, I had the crash. I should add that the modules are unloaded as the last action at the end of the cell shutdown. This unloads the implicitly loaded modules, like Phidgets, as well.
After I saw the crash, I performed an experiment in which I closed the Phidgets channels immediately after opening them. This worked well.
If necessary, I will create an example project. How could I submit it?
User avatar
Patrick
Lead Developer
Posts: 674
Joined: Mon Jun 20, 2005 8:46 am
Location: Calgary

Re: Open and close a channel from different threads

Post by Patrick »

Hi,

I sent you an e-mail.

-Patrick
HjP
Fresh meat
Posts: 3
Joined: Sat Jun 14, 2025 12:56 am

Re: Open and close a channel from different threads

Post by HjP »

By creating a minimum reproducible example (MRE), we were able to isolate the problem:
The crash is not caused by multithreading but by loading/unloading plug-ins. After closing the last device, my code immediately unloaded the module which was implicitly using phidget22.dll. This interfered with some cleanup work done in the Phidget22 Central Thread. In the original tests in the context of my complete application, when I closed the device right after having opened it, I was not able to see this because too much time was passing between opening and closing of the device and the final unloading.
Patrick pointed me to the clean solution (adding a Sleep() is not clean :) ):
use Phidget_finalize() to ensure proper cleanup before unloading.