Hello, I'm new on the list. I have a problem. I've been trying to make my application thread-safe by using lockfiles. I used System.Rewrite to lock the file, and System.IOResult to check the result. But I've encountered problems, and such problems are most likely occur, when more than one thread's trying to lock the file at the same time. I've been thinking what might be the problem, and I wondered if System.IOResult is thread-safe or not. I suppose not. :( 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+} if IOResult = 0 then begin try MailFile:= TFileStream.Create('spool\' + Name + '.eml', fmOpenRead); SpoolData:= TINIFile.Create('spool\' + Name + '.dat'); Envelope.ReturnPath:= SpoolData.ReadString('SpoolObject', 'Return-Path', ''); FOriginator:= TIPNamePair.Create(SpoolData.ReadString('Originator', 'Name', ''), SpoolData.ReadString('Originator', 'IP', '')); FOpened:= true; Result:= true; except System.Close(LockFile); Result:= false; end; end else Result:= false; end; The variable, "LockFile" is an untyped file, previously assigned. I decided to use Rewrite instead of another TFileStream, because I guess TFileStream acquires much more resources to implement features I don't even use. I'd like to ask your opinion, how should I replace Rewrite & IOResult. My ideas: 1. Use another TFileStream to acquire the lockfile. It should work, however as I stated before, I guess it would acquire much unused resources. 2. Use "FileOpen", I think such function exists in the Free Pascal library, however I never used it, and I'm not sure is it thread-safe. It should be, after all. 3. Use direct WinAPI call, CreateFile. In this case, my unit wouldn't be cross-platform anymore. However, now I'm writing my program for Windows, I'm considering porting it to Linux someday, thus I'd like to avoid platform-specific calls wherever possible. I think option 2 is the most preferable, but I'd like to hear your opinions as well, or maybe you have a better idea. Best regards, MegaBrutal _______________________________________________ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal