LOCK |
![]() ![]() ![]() |
The LOCK statement obtains one of 64 system wide task locks.
Format
LOCK lock.num {THEN statement(s)} {ELSE statement(s)}
where
The THEN and ELSE clauses are both optional. Neither is required.
Task locks provide a means of synchronising the activities of multiple processes without using locks on records in data files. The LOCK statement attempts to acquire the lock identified by lock.num.
The THEN clause is executed if the lock was available or was already held by this process.
The ELSE clause is executed if the lock is held by another process. If no ELSE clause is present, the LOCK statement waits for the lock to become available. This wait can be interrupted by using the break key.
The STATUS() function returns zero if the lock was free when the LOCK statement was executed. Otherwise it returns the user number of the process that held the lock. This will be the user number of the current process if it already owned the lock.
There is no means for a program to determine which task locks are held by the user except by attempting to acquire each lock in turn and checking the value of the STATUS() function. Beware that unlike read, update and file locks, task locks are only automatically released on leaving QM, not on return to the command prompt.
Examples
LOCK 7 THEN ...processing statements... UNLOCK 7 END ELSE ABORT "Cannot obtain task lock"
This program fragment obtains task lock 7, performs some critical processing and then releases the lock. The program aborts if the lock is not available.
LOCK 7
This statement attempts to obtain task lock 7 but, unlike the previous example, waits for the lock to become free if it is owned by another user. |