Home · Modules · Classes · Namespaces · Functions

QxtFileLock Class Reference
[QxtCore module]

The QxtFileLock class provides a crossplattform way to lock a QFile. More...

    #include <QxtFileLock>

Inherits QObject.

Public Types

Public Functions

Public Slots

Additional Inherited Members


Detailed Description

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


Member Type Documentation

enum QxtFileLock::Mode

The mode of the lock

ConstantValueDescription
QxtFileLock::ReadLock1A non blocking read lock
QxtFileLock::WriteLock3A non blocking write lock
QxtFileLock::ReadLockWait0A blocking read lock. The lock() function will block until the lock is created.
QxtFileLock::WriteLockWait2A blocking write lock. The lock() function will block until the lock is created.


Member Function Documentation

QxtFileLock::QxtFileLock ( QFile * file, const off_t offset, const off_t length, const QxtFileLock::Mode mode = WriteLockWait )

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

QxtFileLock::~QxtFileLock ()

Destructs the file lock.

QFile * QxtFileLock::file () const

Returns the file the lock is created on.

bool QxtFileLock::isActive () const

Returns true if the lock is active otherwise it returns false

off_t QxtFileLock::length () const

Returns the length of the lock

bool QxtFileLock::lock ()   [slot]

Locks the file Returns true if succeeds, false otherwise.

QxtFileLock::Mode QxtFileLock::mode () const

Returns the mode of the lock

off_t QxtFileLock::offset () const

Returns the offset of the lock

bool QxtFileLock::unlock ()   [slot]

Unlocks the file. Returns true if succeeds, false otherwise.


Copyright © 2007-2011 Qxt Foundation
Qxt 0.6.2