Home |
The QtLockedFile class extends QFile with advisory locking functions. More...
#include <QtLockedFile>
Inherits QFile.
The QtLockedFile class extends QFile with advisory locking functions.
A file may be locked in read or write mode. Multiple instances of QtLockedFile, created in multiple processes running on the same machine, may have a file locked in read mode. Exactly one instance may have it locked in write mode. A read and a write lock cannot exist simultaneously on the same file.
The file locks are advisory. This means that nothing prevents another process from manipulating a locked file using QFile or file system functions offered by the OS. Serialization is only guaranteed if all processes that access the file use QLockedFile. Also, while holding a lock on a file, a process must not open the same file again (through any API), or locks can be unexpectedly lost.
The lock provided by an instance of QtLockedFile is released whenever the program terminates. This is true even when the program crashes and no destructors are called.
This enum describes the available lock modes.
Constant | Value | Description |
---|---|---|
QtLockedFile::ReadLock | 1 | A read lock. |
QtLockedFile::WriteLock | 2 | A write lock. |
QtLockedFile::NoLock | 0 | Neither a read lock nor a write lock. |
Constructs an unlocked QtLockedFile object. This constructor behaves in the same way as QFile::QFile().
See also QFile::QFile().
Constructs an unlocked QtLockedFile object with file name. This constructor behaves in the same way as QFile::QFile(const QString&).
See also QFile::QFile().
Destroys the QtLockedFile object. If any locks were held, they are released.
Returns true if this object has a in read or write lock; otherwise returns false.
See also lockMode().
Obtains a lock of type mode. The file must be opened before it can be locked.
If block is true, this function will block until the lock is aquired. If block is false, this function returns false immediately if the lock cannot be aquired.
If this object already has a lock of type mode, this function returns true immediately. If this object has a lock of a different type than mode, the lock is first released and then a new lock is obtained.
This function returns true if, after it executes, the file is locked by this object, and false otherwise.
See also unlock(), isLocked(), and lockMode().
Returns the type of lock currently held by this object, or QtLockedFile::NoLock.
See also isLocked().
Opens the file in OpenMode mode.
This is identical to QFile::open(), with the one exception that the Truncate mode flag is disallowed. Truncation would conflict with the advisory file locking, since the file would be modified before the write lock is obtained. If truncation is required, use resize(0) after obtaining the write lock.
Returns true if successful; otherwise false.
See also QFile::open() and QFile::resize().
Releases a lock.
If the object has no lock, this function returns immediately.
This function returns true if, after it executes, the file is not locked by this object, and false otherwise.
See also lock(), isLocked(), and lockMode().
Copyright © 2009 Nokia Corporation and/or its subsidiary(-ies) | Trademarks | Qt Solutions |