Home

QtLockedFile Class Reference

The QtLockedFile class extends QFile with advisory locking functions. More...

 #include <QtLockedFile>

Inherits QFile.

Public Types

Public Functions

Additional Inherited Members


Detailed Description

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.


Member Type Documentation

enum QtLockedFile::LockMode

This enum describes the available lock modes.

ConstantValueDescription
QtLockedFile::ReadLock1A read lock.
QtLockedFile::WriteLock2A write lock.
QtLockedFile::NoLock0Neither a read lock nor a write lock.


Member Function Documentation

QtLockedFile::QtLockedFile ()

Constructs an unlocked QtLockedFile object. This constructor behaves in the same way as QFile::QFile().

See also QFile::QFile().

QtLockedFile::QtLockedFile ( const QString & name )

Constructs an unlocked QtLockedFile object with file name. This constructor behaves in the same way as QFile::QFile(const QString&).

See also QFile::QFile().

QtLockedFile::~QtLockedFile ()

Destroys the QtLockedFile object. If any locks were held, they are released.

bool QtLockedFile::isLocked () const

Returns true if this object has a in read or write lock; otherwise returns false.

See also lockMode().

bool QtLockedFile::lock ( LockMode mode, bool block = true )

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().

LockMode QtLockedFile::lockMode () const

Returns the type of lock currently held by this object, or QtLockedFile::NoLock.

See also isLocked().

bool QtLockedFile::open ( OpenMode mode )

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().

bool QtLockedFile::unlock ()

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