The LOCK statement sets an execution lock which establishes exclusive use by one process until the lock is removed. mvBase provides a set of 256 execution locks. These locks can be accessed only through mvBASIC. Execution locks should not be confused with file item locks, since they use a very different mechanism.
Execution locks are set with the LOCK statement by specifying a lock number. That lock number is determined only by local convention. mvBase establishes 256 slots (0 through 255), and keeps track of whether the slot is taken or not. It is up to the application programs to take advantage of this structure.
For example, suppose a particular subroutine tends to slow down the system each time it is used. If a LOCK statement is used at the beginning of the subroutine, then only one user will be able to execute the subroutine at any given time. The lock number should be unique.
Execution locks are released at the termination of the program, or at the encounter of an UNLOCK statement. The UNLOCK statement can be used to release a specific lock number or to release all locks set by the current program.
LOCK is supplied with optional THEN and ELSE clauses that have the same effect as the LOCKED clause for READU. Normally, when a LOCK statement is used on a lock number which is already locked, the program will wait for the lock to be lifted before continuing with execution. However, if the THEN or ELSE clause is included, the program will simply execute the ELSE clause, if present, and continue with execution immediately.
The THEN and ELSE clauses help to avoid the situation called a deadly embrace. A deadly embrace happens when, for instance, one user sets execution lock 1, and another user sets execution lock 2. The first user then attempts to access the procedure controlled by lock 2, and the second user attempts to access the procedure controlled by lock 1. Each user is now stuck, since the program will wait indefinitely for the lock to be released, and neither is free to release the lock that has already been set by their process. If the THEN or ELSE clause is used, however, the deadly embrace cannot occur.
For a list of all execution locks, use the WHAT command.
See Also
Overview of mvBASIC Statements and Functions
Sending Output to the Screen and Printer
Reading and Updating File Items
Reading and Writing Tapes or Floppy Disks