If either RecNum or LastRec is specified, a range of records in the file is locked and the file given by FileNum must have been opened for random I/O. Otherwise, the entire file is locked. If RecNum is specified but LastRec is not, only a single record is locked. If LastRec is specified but RecNum is not, the range of the lock begins at the beginning of the file. If LastRec specifies a record beyond the actual end of the file, the lock extends to any records added beyond the actual end of the file.
For random files, multiple locks on overlapping or contiguous records are regarded as a single lock on the combined range and the "read" state of the most recent lock is used as the "read" state of the new lock. Similarly, it is permissible to unlock only a portion of a previous lock: the other records remain locked. When the program terminates any remaining locks are unlocked.
There is a limit on the number of locks that may exist on the system at any point in time but this limit is large. If the limit is exceeded, an error will occur. It is possible to exceed this limit while unlocking. If a middle portion of a previous lock is unlocked, the lock is split in two requiring an additional lock.
Thus a record which is accessed with intent to read only must be locked to prevent write access by other users. And a record which is accessed with intent to write must be locked to prevent any access by other users.
The following program fragments illustrate this method. The first fragment demonstrates access to the database by a reader. Note that the word "read" is used in the lock statement indicating intent to read only. Other readers will not be restricted by this lock.
on error goto 100 open "database" as #1 len = 80 field #1, 80 as name$ 10 lock #1, read, 1 get #filenum%, record% unlock #1, 1 print name$ 20 close #1 goto 999 100 if err <> 5 or erl <> 10 then on error goto 0 print "Record is being updated, sorry" resume 20 999 endThis second fragment demonstrates access to the database by a writer. Note that the word "read" is not used in the lock statement indicating intent to write. This lock prevents access of any kind to this record.
on error goto 100 input "What is the name"; n$ open "database" as #1 len = 80 field #1, 80 as name$ lset name$ = n$ 10 lock #1, 1 put #filenum%, record% unlock #1, 1 20 close #1 goto 999 100 if err <> 5 or erl <> 10 then on error goto 0 print "Record is in use, sorry" resume 20 999 end
from The Basmark QuickBASIC Programmer’s Manual by Lawrence Leinweber