Fork into parallel but non-identical threads

5 vues (au cours des 30 derniers jours)
Emily Parker
Emily Parker le 30 Avr 2021
Réponse apportée : Akshat le 10 Mai 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)]);

Réponses (1)

Akshat
Akshat le 10 Mai 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!

Catégories

En savoir plus sur Startup and Shutdown dans Help Center et File Exchange

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by