Fork into parallel but non-identical threads

5 views (last 30 days)
Emily Parker
Emily Parker on 30 Apr 2021
Answered: Akshat on 10 May 2024
I am interested in using imwrite in a loop to write 1 frame from 2 cameras to the disk simultaneously (each has associated timestamp). Right now they are written in the loop serially:
%camera 1%
t=datetime('now','Timezone','local','Format','d-MMM-y HH:mm:ss Z');
imwrite(buf2,thisFilenamec1);
disp(['Camera 1 Frame ',num2str(i+1),'/',num2str(frameCount),' at ',datestr(t)]);
%camera 2%
t2=datetime('now','Timezone','local','Format','d-MMM-y HH:mm:ss Z');
imwrite(getdata(vid),thisFilenamec2);
disp(['Camera 2 Frame ',num2str(i+1),'/',num2str(frameCount),' at ',datestr(t)]);

Answers (1)

Akshat
Akshat on 10 May 2024
Hi Emily,
Whenever we need to execute commands in a parallel fashion, we use the "parfor" loop instead of a normal "for" loop in MATLAB.
You would just require to start a parallel pool, and replace "for" with "parfor".
Now, according to my knowledge of how operating systems work in general, are that when a file is being edited, the OS puts a lock on that file, in order to not create inconsistencies in versions of files. You can think of it like this, let us say you are writing to a file, and parallely I open the file to write, whatever I write and whatever you write are going to create two versions of the same file.
Hence, this can be issue in this case as well, the OS will put a lock on a file when "imwrite" is called, so a parallel thread cannot open the file and an error will be thrown. The same is written by Walter Robson in a comment on the following answer:
A simple workaround is that you do the computations parallely, and store it in an array, then write the whole array to the file. This will reduce the computation overhead but the write will be sequential.
Hope this helps!

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!