[fpc-pascal] file locking cross platform

Michael Van Canneyt michael at freepascal.org
Sat Apr 4 08:23:14 CEST 2015

On Fri, 3 Apr 2015, no reply wrote:

> There are some major differences between the way Windows and Unix allow
> locking of files with their api's, but is it possible to make a cross
> platform LockFile() procedure or function for both unix and windows?
> By file locking I of course mean when for example you have to write data
> to a file but you want to lock it so no one else can write to the file,
> then UnlockFile() when you are done with it.
> If not cross platform, shouldn't one of the unix units have a LockFile
> function at least?

To the best of my knowledge, unix only has advisory file locking, as you discovered.
It works different from windows.

There is some linux specific mandatory locking, I think, but implementing it so it 
behaves the same as Windows is IMHO not possible.

The FileOpen/FileCreate (and hence TFileStream) implements the various fmShare* constants,
but they will only work as expected if all accessing applications are FPC applications.
Obviously we cannot guarantee that other apps "play well".


More information about the fpc-pascal mailing list