I am working on a MATLAB project where I would like to have two instances of MATLAB running in parallel and sharing data. I will call these instances MAT_1 and MAT_2. More specifically, the architecture of the system is:
MAT_1processes images sequentially, reading them one by one usingimread, and outputs the result for each image usingimwrite.MAT_2reads the images output byMAT_1usingimreadand outputs its result somewhere else.
One of the problems I think I need to address is to guarantee that MAT_2 reads an image output by MAT_1 once MAT_1 has fully finished writing to it.
My questions are:
- How would you approach this problem? Do I need to use semaphores or locks to prevent race conditions?
- Does MATLAB provide any mechanism to lock files? (i.e. something similar to
flock, but provided by MATLAB directly, and that works on multiple platforms, e.g. Windows & Linux). If not, do you know of any third-party library that I can use to build this mechanism in MATLAB?
EDIT :
- As @yoda points out below, the Parallel Computing Toolbox (PCT) allows for blocking calls between MATLAB workers, which is great. That said, I am particularly interested in solutions that do not require the PCT.
-
Why do I require
MAT_1andMAT_2to run in parallel threads?:The processing done in
MAT_2is slower on average (and more prone to crashing) thanMAT_1, and the output ofMAT_1feeds other programs and processes (including human inspection) that do not need to wait forMAT_2to do its job.
Answers :
- For a solution that allows for the implementation of semaphores but does not rely on the PCT see Jonas’ answer below
- For other good approaches to the problem, see Yoda’s answer below
Personally, I’d use the parallel processing toolbox for this.
As far as I know, there is no straightforward way in Matlab to have systemwide file locks. However, in order to ensure that Matlab #2 only reads output of Matlab #1 when the file has finished writing, I suggest that after writing e.g the file
results_1.mat, Matlab #1 writes a second file,results_1.finished, which is an empty text file. Since the second file is written after the first, its existence signals that the results-file has been written. You can thus search for files with the extensionfinished, i.e.dir('*.finished'), and usefilepartsto get the name of the .mat file you’d like to load with Matlab #2.