Skip to main content
Previous section   Next section

LOCK

Obtains a logical process lock.

Synopsis

LOCK name [THEN statements] [ELSE statements]

Arguments

name A number or a string, or an expression that evaluates to a number or a string specifying a lock name. Commonly, an integer from 0 through 64.

Description

The LOCK statement sets a named process lock, preventing other processes from obtaining a lock with the same name.

Process locks are not incremental: A process can set the same lock multiple times with LOCK. A single UNLOCK releases the lock

Commonly, name evaluates to an integer in the range 0 through 64. However, in Caché any number or string may be specified as a logical lock name. The lock name may not be empty, so LOCK "" sets LOCK 0.

You can specify optional THEN and ELSE clauses. If you obtain lock name, the THEN clause is executed. If you already have lock name, the THEN clause is also executed. If you could not obtain lock name because it is held by another resource, the ELSE clause is executed. If you could not obtain lock name because it is held by another resource, the ELSE clause is executed.

Unlike READU locks, process locks set in a program are not released automatically when the program terminates. The lock belongs to the process, and persists for the life of the process, unless unlocked explicitly using the UNLOCK statement.

You can determine which locks are held using the LIST.LOCKS command line command. You can unlock one or all locks using the CLEAR.LOCKS command line command. These commands are described in the Caché MultiValue Commands Reference.

Example

The following example uses the LOCK statement to obtain a logical lock named 17.

a=17
LOCK a THEN PRINT "Got the Lock"
  ELSE PRINT "Couldn't get the lock"
  .
  .
  .
  UNLOCK a
Copy code to clipboard

See Also