[Gambas-user] Locking files - best method

Benoît Minisini gambas at ...1...
Sat Feb 14 18:34:55 CET 2009


> Benoît Minisini schrieb:
> >> 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
> > ...
>
> Just one question:
>
> Why do you use AS Stream and not AS File?
>
> Rolf
>
>

It is a mistake. You can use Stream for the lock, as you just use hLock with 
LOCK and UNLOCK. But hFile should be a File.

Regards,


-- 
Benoît




More information about the User mailing list