How to use Locks?
Kye
Posts: 2,200
I'm trying to get my head arround the locks mentioned in the prop manual and the description seems just a bit confusing it.
The manual uses this code:
However "lockset" "sets a semaphore to true and gets its previous state" which after one execution of the "repeat until not" (repeat while)·loop above the lock mechanism would be pointless.
So,·is this assumption correct... that the "lockset" command·only sets the lock to true if the previous state was false? This kind of·operation would make sense.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Nyamekye,
The manual uses this code:
PUB ReadResource | Idx repeat until not lockset(SemID) 'wait until we lock the resource repeat Idx from 0 to 9 'read all 10 longs of resource LocalData[noparse][[/noparse]Idx] := long[noparse][[/noparse]Idx] lockclr(SemID) 'unlock the resource
However "lockset" "sets a semaphore to true and gets its previous state" which after one execution of the "repeat until not" (repeat while)·loop above the lock mechanism would be pointless.
So,·is this assumption correct... that the "lockset" command·only sets the lock to true if the previous state was false? This kind of·operation would make sense.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Nyamekye,
Comments
-Phil
Addendum: Come to think of it, lockset would have been much less confusing if its results were reversed: true if the lock was successfully obtained; false, otherwise.
Post Edited (Phil Pilgrim (PhiPi)) : 12/30/2009 10:25:00 PM GMT
The REPEAT is correct. It requires that the lock be clear for one iteration of the REPEAT (at which point the REPEAT exits) and the lock has to have been set when the repeat exits.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Nyamekye,