atomically creating a file lock in MATLAB (file mutex)

后端 未结 5 823
伪装坚强ぢ
伪装坚强ぢ 2021-01-18 18:24

I am looking for a simple already implemented solution for atomically creating a file lock in MATLAB.

Something like:

file_lock(\'create\', \'mylockf         


        
5条回答
  •  孤独总比滥情好
    2021-01-18 18:50

    If you only need to run on OS X and Linux (not Windows), you can use the following:

    pathLock='/tmp/test.lock'
    
    % Try to create and lock this file.
    % In my case I use -r 0 to avoid retrying
    % You could use -r -1 to retry forever, or for a particular amount of time,
    % etc, see `man lockfile` for details.
    if ~system(sprintf('lockfile -r 0 %s',pathLock))
        % We succeeded, so perform some task which needs to be serialized.
        % runSerializedTask()
        % Now remove the lockfile
        system(sprintf('rm -f %s',pathLock));
    end
    

提交回复
热议问题