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