Semaphores and locks in MATLAB

Viewed 8426

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:

  1. MAT_1 processes images sequentially, reading them one by one using imread, and outputs the result for each image using imwrite.
  2. MAT_2 reads the images output by MAT_1 using imread and 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:

  1. How would you approach this problem? Do I need to use semaphores or locks to prevent race conditions?
  2. 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_1 and MAT_2 to run in parallel threads?:

    The processing done in MAT_2 is slower on average (and more prone to crashing) than MAT_1, and the output of MAT_1 feeds other programs and processes (including human inspection) that do not need to wait for MAT_2 to 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
6 Answers
Related