[Gambas-user] Documentation for LOCK

Me adamnt42 at gmail.com
Mon Aug 20 14:55:43 CEST 2018


On Mon, 20 Aug 2018 13:53:41 +0200
Hans Lehmann <hans at gambas-buch.de> wrote:

> Hello,
> 
> this example can also be found in the documentation for LOCK 
> (http://gambaswiki.org/wiki/lang/lock):
> 
> 8<---------------------------------------------------------
> 
> Dim hLock As Stream
> ' Try to acquire the lock
> Try hLock = Lock "~/my-lock"
> If Error Then
>    Print "Locked is already acquired. Try again later."
>    Return
> Endif
> ' File is locked, you can do the job now!
> ...
> ' Do not forget to release the lock
> Unlock hLock
> 
> 8<---------------------------------------------------------
> 
> and this sentence:
> 
> Beware that the locked file is emptied! So never use a file whose 
> contents is important.
> 
> Doesn't this sentence contradict the above example? I would like to 
> protect the file I want to edit exclusively against competing parallel 
> access by locking it? The file should not be emptied - I want to edit it!
> 
> With kind regards
> 
> Hans
> 
> ----[ Gambas mailing-list is hosted by https://www.hostsharing.net ]----

Having never tried it, this is only a guess.
The idea of the Lock command is, I assume, to attempt to create a file of the name "my-lock" (or whatever) in the  specified location (in the examples case, in the user's home directory).
The idea of the Unlock command is to delete that same (presumably) file.  
The sentence you refer to, is correct. Never use a file that may have significant information in it.  Further, never output any significant information into that file expecting it to be available some "next time".

It might help to " touch --help" to see what I believe is going on here. 

In short, only use Lock to do exactly that, create if possible a lock file. If not possible, then some other process has already got a lock on the situation, so don't try to use the other files relavant to the situation until the lock file is destroyed. It's a simple semaphore. The IDE uses this to detect is a project is already opened by some other instance of the IDE.  Simple to see. Open the IDE, load a project, leave it as it is, then open another instance of the IDE and try to open the same project. You will get a message to the extent of something like "This project is already open. Do you want to open it anyway?" 


hth
b



-- 
Me <adamnt42 at gmail.com>


More information about the User mailing list