![]() |
Home · Modules · Classes · Namespaces · Functions |
The QxtFileLock class provides a crossplattform way to lock a QFile. More...
#include <QxtFileLock>
Inherits QObject.
The QxtFileLock class provides a crossplattform way to lock a QFile.
QxtFileLock supports range locking of a file. The File will take parentship of the lock. The lock gets cleaned up with the QFile, and it is released when the QFile is closed.
Example usage:
off_t lockstart = 0x10; off_t locklength = 30 QFile file("test.lock"); //the lock gets deleted when file is cleaned up QxtFileLock * writeLock = new QxtFileLock(&file,lockstart,locklength,QxtFileLock::WriteLock); if(file.open(QIODevice::ReadWrite)) { if(writeLock->lock()) { // some write operations writeLock->unlock(); } else //lock failed }
Note: QxtFileLock behaves different than normal unix locks on *nix. A thread can writelock the region of a file only ONCE if it uses two different handles. A different thread can not writelock a region that is owned by a other thread even if it is the SAME process.
Note: On *nix this class uses fctnl to lock the file. This may not be compatible to other locking functions like flock and lockf
Note: Please do not mix QxtFileLock and native file lock calls on the same QFile. The behaviour is undefined
Note: QxtFileLock lives in the same thread as the passed QFile
Warning: due to a refactoring issues of QFile this class will not work with Qt from version 4.3.0 to 4.3.2
The mode of the lock
Constant | Value | Description |
---|---|---|
QxtFileLock::ReadLock | 1 | A non blocking read lock |
QxtFileLock::WriteLock | 3 | A non blocking write lock |
QxtFileLock::ReadLockWait | 0 | A blocking read lock. The lock() function will block until the lock is created. |
QxtFileLock::WriteLockWait | 2 | A blocking write lock. The lock() function will block until the lock is created. |
Contructs a new QxtFileLock. The lock is not activated. file the file that should be locked offset the offset where the lock starts length the length of the lock mode the lockmode
Destructs the file lock.
Returns the file the lock is created on.
Returns true if the lock is active otherwise it returns false
Returns the length of the lock
Locks the file Returns true if succeeds, false otherwise.
Returns the mode of the lock
Returns the offset of the lock
Unlocks the file. Returns true if succeeds, false otherwise.
Copyright © 2007-2011 Qxt Foundation |
Qxt 0.6.2 |