[Gambas-user] Locking files - best method

Benoît Minisini gambas at ...1...
Sat Feb 14 13:10:45 CET 2009


> Sharing a file exclusively locked very briefly and getting everything
> to work correctly amongst multiple programs can be brain-twisting.
> With increasing use of mulit-cores, the possibility for sharing files
> using any lock technique is more important than ever before
> because all associated programs can execute simultaneously.
>

You can use the LOCK/UNLOCK commands and a specific void file on the disk to 
create system exclusive sections in your code.

Let's suppose you want to modify a file named "/tmp/foo".

...
DIM hLock AS Stream
DIM hFile AS Stream

' Try to acquire the lock
TRY hLock = LOCK "/tmp/foo.lock"
IF ERROR THEN
  PRINT "'foo' is being modified. Try again later."
  RETURN
ENDIF

' Lock was acquired, you can modify the file now!
hFile = OPEN "/tmp/foo" FOR OUTPUT
...
CLOSE #hFile

' Do not forget to release the lock
UNLOCK hLock
...

-- 
Benoît




More information about the User mailing list