From: David Portabella on
> Is there a method to create a safe file mutex in Matlab?

See this workaround:
atomically creating a file lock in MATLAB (file mutex)
http://stackoverflow.com/questions/3451343/automically-writing-a-file-in-matlab/3452143#3452143
From: Walter Roberson on
David Portabella wrote:
>> Is there a method to create a safe file mutex in Matlab?
>
> See this workaround:
> atomically creating a file lock in MATLAB (file mutex)
> http://stackoverflow.com/questions/3451343/automically-writing-a-file-in-matlab/3452143#3452143
>

That's not safe either.
From: Sean on
I have done this before, and had a similar problem. The solution that worked for me (which is probably not the best, but did fix my race condition) was to write a temporary file and then delete it when I was done writing to the log file. If the temporary file existed, then my script would know that another instance of Matlab was currently writing to the log, and that it should pause for a moment and try again.

Again, this is kind of a hackish solution, but it worked.

Sean
From: Jan Simon on
Dear Sean,

> The solution that worked for me (which is probably not the best, but did fix my race condition) was to write a temporary file and then delete it when I was done writing to the log file. If the temporary file existed, then my script would know that another instance of Matlab was currently writing to the log, and that it should pause for a moment and try again.

The problem of this method is, as described above:
Two instances test at the same time, if the temporary file is existing. Then both instances decide, that there is none and create it. This *is* a racing condition.

My Professor told me:
1. A program is a deterministic algorithm, which stops after a finite time.
2. Write programs, if you want to use a computer.

Although from the view point of the universe, each evaluation in a computer is deterministic, for practical usage a racing condition is not. If you can proove, that the frequency of collisions is smaller than the inverse of the age of the universe (or the age of computers), I'd be satisfied.

Kind regards, Jan