[fpc-pascal] Is System.IOResult thread-safe? (If not, I need to replace it.)

MegaBrutal megabrutal at gmail.com
Wed Nov 10 13:48:21 CET 2010


2010/11/10 Jonas Maebe <jonas.maebe at elis.ugent.be>:
>> Not a big
>> deal, though, I may use an other function to lock the file in a
>> thread-safe manner.
>>
>> This is my function (that tries to lock the file):
>>
>> function TSpoolObjectReader.Open: boolean;
>> begin
>>  {$I-} Rewrite(LockFile, 1); {$I+}
>
> Rewrite does not cause an error if a file already exists. It simply
> overwrites it. You cannot first check whether it exists and then create it
> if it doesn't, because in between another thread could have created the lock
> file already.

Well, my object should keep the lockfile opened as long as it's
working. If the file has already existed, but Rewrite is able to lock
it, that means that my application was shut down improperly. But it's
not a problem. Actually, the operating system should refuse to open
the file if a 2nd thread tries to open it. To sum it up, my code
doesn't work like you assume, that it creates and closes a file, and
it checks for the existence of the file to check whether it can work
on the object; instead it opens the file, and keeps it opened until it
finishes its work, then closes and deletes the file.



More information about the fpc-pascal mailing list