I have written a database application using a binary file as storage. it is accessed via powershell cmdlets.
You can put information into the database using the put- and you can read information using get-.
The problem is synchronisation. What is the best way to ensure that the cmdlets don’t access the file at the same time?
The put- must have exclusive access ie no other writers or readers can access the file. The get- doesn’t need exclusive access or readers can access the database at the same time.
Am I best using a file based locking mechanism or a .NET based synchronisation mechanism?
I’m not aware of any .NET-based synchronization mechanisms that would be appropriate here (read on – I’m making an assumption that may not be true).
If you absolutely must have exclusive access for a ‘put,’ then either (a) file-based locking, or (b) database-based locking might be appropriate. For example, a ‘put’ command might first check a special database field to see if it can gain exclusive access; if it can, it would update that field to show that exclusive access is already taken, preventing another simultaneous put from happening.
If I’m interpreting your question correctly, there’s a possibility for this database to be accessed by multiple computers, meaning a mutex wouldn’t really work since they’re designed for inter-thread synchronization. If that’s not the case and only a single computer would be accessing the file at any given time, then a mutex would work well.