Threading in Matlab GUI Callbacks
29 views (last 30 days)
Show older comments
Dear All,
We would like to run two separate threads/tasks/functions on user clicks in GUI(Separate Push Button Callbacks) concurrently. So far we have observed only one callback runs at a given time. So is it possible to create two explicit threads like Posix threads(pthreads) in MATLAB to handle callbacks?
As per the forum, we were able to try a simple multi-threading application using MEX functionality, But as per our requirement, we cannot use MEX.
0 Comments
Answers (2)
Walter Roberson
on 17 Aug 2021
Edited: Walter Roberson
on 18 Aug 2021
No.
You can have the controlling process use parfeval() https://www.mathworks.com/help/parallel-computing/parallel.pool.parfeval.html
With fairly new matlab those can be run in threads instead of a separate process. However the threads version and the separate process version have the same limitations of not being able to access the display or use global variables or change variables in the controlling process: the controlling process at some point needs to gather the finished result and assign from the collected output back into appropriate variables.
The arrangement is not suitable for anything like timing responses (see Psychtoolbox for that). It could potentially be used to collect data from different devices (but synchronization of readings can be a problem).
2 Comments
Walter Roberson
on 18 Aug 2021
Edited: Walter Roberson
on 18 Aug 2021
parfeval() does require Parallel Computing Toolbox.
If you do not have Parallel Computing Toolbox, then your options are:
- start up multiple MATLAB sessions, and use one of several different methods of communicating between them as different processes. For example you could use the tcpudpip contribution from the File Exchange, or there are communications arrangements involving shared files
- If you are using Windows, then you can use .NET System.Diagnostics.Process to create a second MATLAB process, and use I/O redirection to be able to read to/from the process. The process is able to proceed independently
- If you are using Windows then you can use activexserver() to start a second MATLAB using its Automation Server interface. I am not clear whether the invoking program is able to proceed asynchronously
- You can use Java threads from inside MATLAB to start a new Java thread, and you can use Java process communications functions to talk between them. However, only one Java Thread can talk to the execution engine, so this is not very useful for having multiple MATLAB-level functions executing. Sometimes though, if the reason for having multiple functions is that you need to communicate with a device without waiting, then it can turn out that the communication with the device might already happen to go through Java, or it might turn out that the communication with the device could reasonably be rewritten to go through Java, so multiple Java threads can sometimes be useful even if only one can talk to the execution engine
If I understand correctly, one thing you cannot do is run Mex to start multiple pthreads or openMP entitites,. and have the different pthreads / openMP entities call back into MATLAB. It is possible to use Mex to start multiple pthreads or to use openMP primitives, but the mex would have to provide the communications synchronization, so that only the original thread talks to the execution engine. There are uses for such a thing, such as calling into C++ to do work outside of MATLAB.
Your version of MATLAB is not thread safe, and will crash if you have multiple parallel constructs call into the execution engine. R2021a (or perhaps it was R2020b) was the first safer (relatively speaking) release, and future releases are expected to be more thread safe.
Chetan A
on 18 Aug 2021
1 Comment
Walter Roberson
on 19 Aug 2021
parallel futures do not have the ability to change display graphics, or to write to the command window, and any local variables they write will disappear when the future ends. Global variables will not be transferred to the process when it starts, and will not be transferred back from the process when it ends.
To be able to have the worker send data to the controlling process in real-time, have the controlling process first start a parallel data queue https://www.mathworks.com/help/parallel-computing/parallel.pool.dataqueue.html or the pollable version of that, and make sure it gets passed to the worker process. When the worker queues data to the construct, the controlling process can pull it out.
This is a one-way queue. The method of building a reverse queue (to allow the controlling process to send data to the worker) is a bit awkward but can be done.
To end the indefinite loop, you could cancel() the future. I am not certain whether onCleanup() will work in such a case.
If onCleanup() will not work while canceling a future, then possibly the easiest way would be to use a memory map https://www.mathworks.com/help/matlab/ref/memmapfile.html to write a terminate request (the worker would have to be checking the file periodically)
Worker processes do not have write access to the memory of the controlling process.
At least for now, you have to treat MATLAB as being single-threaded in itself, and any threads/processes you create have to work through a fairly narrow interface that handles synchronization by value (not by reference.)
See Also
Categories
Find more on MATLAB Compiler SDK in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!