LatticeLocker simply uses the lock and unlock function of class Lattice. The advantage of LatticeLocker over these functions is that the destructor of LatticeLocker is called automatically by the system, so unlocking the lattice does not need to be done explicitly and cannot be forgotten. Especially in case of exception handling this can be quite an adavantage.
This class is meant to be used with the UserLocking option. It can, however, also be used with the other locking options. In case of PermanentLocking(Wait) it won't do anything at all. In case of AutoLocking it will acquire and release the lock when needed. However, it is possible that the system releases an auto lock before the LatticeLocker destructor is called.
The constructor of LatticeLocker will look if the lattice is
already appropriately locked. If so, it will set a flag to
prevent the destructor from unlocking the lattice. In this way
nested locks can be used. I.e. one can safely use LatticeLocker
in a function without having to be afraid that its destructor
would undo a lock set in a higher function.
Similarly LatticeLocker will remember if a lattice was
already read-locked, when a write-lock is acquired. In such a
case the destructor will try to ensure that the lattice remains
read-locked.
// Open a lattice to be updated. PagedArray<Float> myLattice (Table ("theLattice", LatticeLock::UserLocking, Lattice::Update); // Start of some critical section requiring a lock. { LatticeLocker lock1 (myLattice, FileLocker::Write); ... write the data } // The LatticeLocker destructor invoked by } unlocks the table.
If the constructor acquired the lock, the destructor releases the lock and flushes the data if changed.
Has this process the read or write lock, thus can the table be read or written safely?