in reply to How to do atomic file locking?

It is also important to consider the operating-system and file-system.   Some scenarios provide only “advisory locking.”   Here is a good web-page that discusses the issue for Linux ... but Windows is different.   The flock perldoc page is mandatory reading.

You also need to be sure that all disk writes are actually complete before you unlock.   This StackOverflow article seems like a good one to peruse.

Remember that, if you have a more-complicated situation (and if you can be certain that no “unaware” programs might be accessing the same files at the same time), you can use a “lock file” ... a dummy file whose only purpose is to be the target of lock/unlock requests.

Replies are listed 'Best First'.
Re^2: How to do atomic file locking?
by Acapulco (Novice) on Aug 26, 2015 at 17:03 UTC

    Thanks for the advice. Yes, I'm aware this solution is only "advisory-based", but since I'm creating the lock file ex profeso for this and I'm sure this won't be residing in a network file system and other such nuances I believe it should work fine

    Regarding the file flushing, I was not aware of this but it definitely makes sense, otherwise we might end up with inconsistencies. Fortunately though, in my particular use case I usually open the shared file write something and close it immediately and as far as I can tell, close should flush.

    Again, thanks a lot for the advice. I like to be extra careful since I know concurrency is very easy to get wrong