What is the best way to atomically lock multiple files? One possibility I’ve thought of is a simple .lock file in the directory (I’m only protected from other instances of my program; I’m not necessarily trying to thwart other programs from disrespecting the lock.) However, everything I’ve learned about thread-safety screams “bad idea!” Would this be a safe way to go? Is trying to create a file atomic, so if I succeed in creating it, I know I have the lock? How should I go about this?
I’m writing my own bug tracker (mostly as an exercise, I know I could find good solutions out there,) which stores bugs in files on the network. I envision it kind of like SVN – a single directory gets taken over by the program, and used to hold bugs, revisions, screenshots, etc, all managed by the tracker. I’m assuming SVN has a way to ensure multiple clients don’t make commits at the exact same time, etc.
Open each file exclusively, if you find one that can’t be opened, then unlock the ones you’ve already locked. Always lock in alphabetical order to avoid deadlocks.