14. mt::LockWrite "variablename"

[<<<] [>>>]

Lock mt variable for write protection. While the variable is locked no programs can access it, not even the one that locked the variable.

Usually there is no need to alter the MT variables, as the variables are automatically locked while their value is altered. Locking is needed when a program needs to alter more than one MT variable in a coincise manner. Another possibility is when a program wants to alter a variable and the new value depends on the previous value. As a simplest example is when aprogram wants to increment a variable.

In either case the programs have to have a pseudo MT variable, which is used in the locking and unlocking functions. Usually the programs use these variables only to lock and to release and do not care their values, albeit there is nothing that would prohibit using the values.

For example a program (X) wants to increment the MT variable "B" and it has to keep track of the variable "BB" to ensure that "BB" is the double of "B".

In the meantime another (Y) program wants to decrement "BB" by two and set "B" to the half of "BB" to keep the rule.

Program X:

mt::GetVariable "B",B
B = B+1
mt::SetVariable "B",B
mt::SetVariable "BB",2*B

Program Y:

mt::GetVariable "BB",BB
BB = BB-2
mt::SetVariable "BB",BB
mt::SetVariable "B",B \ 2

The lines of the two programs can execute in any mixed order.

See the following scenario: