[Gambas-user] Locking files - best method

Rolf-Werner Eilert eilert-sprachen at ...221...
Sat Feb 14 18:41:51 CET 2009


Benoît Minisini schrieb:
>> 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,
> 
> 

Ok, that's fine :-) I thought there was another thing I hadn't 
understood yet...

Have a nice Sunday!

Rolf






More information about the User mailing list