| **Navigation:**  [[introduction.htm|Language Reference]] > 13 - Built-in Functions >====== LOCK (exclusive file access) ====== | [[locale load environment file .htm|{{btn_prev_n.gif|Previous page}}]][[introduction.htm|{{btn_home_n.gif|Return to chapter overview}}]][[lockthread re lock the current execution thread .htm|{{btn_next_n.gif|Next page}}]] | | || **LOCK(**//file// [,//seconds//]**)** {{blk2blue.jpg|blk2blue.jpg}} | **LOCK** | Locks a data file. | | //file// | The label of a FILE opened for shared access. | | //seconds// | A numeric constant or variable which specifies the maximum wait time in seconds. | The **LOCK** statement locks a //file// against access by other workstations in a multi-user environment. Generally, this excludes other users from writing to or reading from the //file//. The file driver may or may not treat separate execution threads within a single program as another workstation or not. | LOCK(//file//) | Attempts to lock the //file// until it is successful. If it is already locked by another workstation, LOCK will wait until the other workstation unlocks it. | | LOCK(//file//,//seconds//) | Posts the "File Is Already Locked" error after unsuccessfully trying to lock the file for the specified number of //seconds//. | The most common problem to avoid when locking files is referred to as "deadly embrace." This condition occurs when two workstations attempt to lock the same set of files in two different orders and both are using the LOCK(//file//) form of LOCK. One workstation has already locked a file that the other is trying to LOCK, and vice versa. This problem may be avoided by using the LOCK(//file//,//seconds//) form of LOCK, and always locking files in the same order. **Errors Posted:** | 32 | File Is Already Locked | **Example:** **LOOP                 !Loop to avoid "deadly embrace"** ** LOCK(Master,1)      !Lock the master file, try 1 second** ** IF ERRORCODE() = 32 !If someone else has it** **  CYCLE              !try again** ** END** ** LOCK(Detail,1)      !Lock the detail file, try 1 second** ** IF ERRORCODE() = 32 !If someone else has it** **  UNLOCK(Master)     !unlock the locked file** **  CYCLE              !try again** ** END    ** ** BREAK               !Break loop when both files are locked** **END** **See Also:** [[unlock unlock a locked data file .htm|UNLOCK]] [[hold exclusive record access .htm|HOLD]] [[logout begin transaction .htm|LOGOUT]]