MATLAB Answers

PCT: Lock and Simultaneous Access

8 views (last 30 days)
Matlab2010
Matlab2010 on 23 Nov 2014
Commented: Matlab2010 on 1 Dec 2014
When using PCT, how can you enforce synchronous access to resources? Eg lock in C#.
I have some code,
parameters = [1 2 3 4];
my_files = getListOfFiles();
parfor i = 1 :I
myFunc(my_files, parameters);
end
and
function myFunc(my_files, p)
for j = 1: length(myFiles)
data = load(myFiles(j));
runFunction(data, p);
delete(data);
end
end
So in summary this code runs in a parfor loop and accesses files and works on them. The problem is that different parfor loops may want to access the same myFiles(j) at the same time. This means the resource is locked by the system, meaning a fatal error.
The error messages look like:
Would you like to replace the existing file D:\data\1.mat
15481315 bytes, modified on 23-11-2014 10:28
with a new one
15481315 bytes, modified on 21-11-2014 07:53
The process cannot access the file because it is being used by another process.
When this happens I would like to matlab wait to access the resource until it becomes free. How can i do this? My only thoughts on this are, but I think there must be a more sensible way,
function myFunc(my_files, p)
for j = 1: length(myFiles)
if(~exist(myFiles(j), 'file' == 2))
load(myFiles(j));
end
runFunction(data, p);
while exist(myFiles(j), 'file' == 2)
delete(myFiles(j));
end
end
end
Though when I try the above, I get at the load() call:
Unable to read MAT-file D:\folder\file.mat
File may be corrupt.
I am using 2014A. thanks

Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by