QtLockedFile Class


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

Header: #include <QtLockedFile>

Public Types

enum LockMode { ReadLock, WriteLock, NoLock }

Public Functions

QtLockedFile(const QString &name)
virtual ~QtLockedFile()
bool isLocked() const
bool lock(SharedTools::QtLockedFile::LockMode mode, bool block = true)
SharedTools::QtLockedFile::LockMode lockMode() const
bool unlock()

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 QtLockedFile. 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.

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

Property Documentation

Member Function Documentation


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

[virtual] QtLockedFile::~QtLockedFile()

Destroys the instance of QtLockedFile. The destructor is virtual.

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(SharedTools::QtLockedFile::LockMode mode, bool block = true)

SharedTools::QtLockedFile::LockMode QtLockedFile::lockMode() const

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

See also isLocked().

bool QtLockedFile::unlock()

Member Variable Documentation

Related Non-Members

Macro Documentation

© 2018 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.