QLockFile 类

QLockFile 类使用文件在进程之间提供锁。 更多...

头: #include <QLockFile>
qmake: QT += core
Since: Qt 5.1

公共类型

enum LockError { NoError, LockFailedError, PermissionError, UnknownError }

公共函数

QLockFile (const QString & fileName )
~QLockFile ()
LockError error () const
bool getLockInfo (qint64 * pid , QString * hostname , QString * appname ) const
bool isLocked () const
bool lock ()
bool removeStaleLockFile ()
void setStaleLockTime (int staleLockTime )
int staleLockTime () const
bool tryLock (int timeout = 0)
void unlock ()

详细描述

QLockFile 类使用文件在进程之间提供锁。

锁定文件可以用于阻止多个进程并发性访问同一资源。例如,在磁盘上的配置文件或套接字、端口、共享内存区域 ...

序列化才有保证,若访问共享资源的的所有进程使用 QLockFile ,采用相同文件路径。

QLockFile 支持 2 用例:为短期操作保护资源 (如,验证配置文件是否已改变在保存新设置之前),和了解无限长期存活保护资源 (如,用户在编辑器中打开文档)。

当保护短期操作时,可以调用 lock () 并等待直到任何正在运行的操作完成为止。当长时间保护资源时,不管怎样,应用程序应该始终调用 setStaleLockTime (0) 然后 tryLock () 采用短超时,以警告用户资源被锁定。

若持有锁的进程崩溃,锁定文件停留在磁盘且可以阻止任何其它进程访问共享资源。由于此原因, QLockFile 会试着基于写入文件的进程 ID 检测这种 "陈旧" 锁文件。为控制进程 ID 同时被重用的状况,会比较当前进程名称与对应锁文件进程 ID 的进程名称。若进程名称不同,认为锁文件陈旧。此外,会考虑锁文件的最后修改时间 (默认 30 秒,对于短期存活操作用例)。若发现锁文件陈旧,会删除它。

因此,对于长时间保护资源的用例,应该调用 setStaleLockTime (0),和当 tryLock () 返回 LockFailedError ,通报用户文档被锁定,可能使用 getLockInfo () 了解更多细节。

注意: 在 Windows,此类检测陈旧锁时会遇到问题,若机器主机名包含 US-ASCII 字符集外的字符。

成员类型文档编制

enum QLockFile:: LockError

此枚举描述最后结果为调用 lock () 或 tryLock ().

常量 描述
QLockFile::NoError 0 成功获取锁。
QLockFile::LockFailedError 1 无法获取锁由于被另一进程持有。
QLockFile::PermissionError 2 无法创建锁定文件由于父级目录缺乏权限。
QLockFile::UnknownError 3 发生其它错误,例如:完整分区阻止写出锁定文件。

成员函数文档编制

QLockFile:: QLockFile (const QString & fileName )

构造新锁定文件对象。对象被创建在解锁状态下。当调用 lock () 或 tryLock (),锁定文件 fileName 将被创建,若它尚不存在。

另请参阅 lock () 和 unlock ().

QLockFile:: ~QLockFile ()

销毁锁定文件对象。若获得锁,这将释放锁通过删除锁定文件。

LockError QLockFile:: error () const

返回锁定文件错误状态。

tryLock () 返回 false ,此函数可以被调用以找出锁定失败的原因。

bool QLockFile:: getLockInfo ( qint64 * pid , QString * hostname , QString * appname ) const

检索锁定文件当前所有者的有关信息。

tryLock () 返回 false ,和 error () 返回 LockFailedError ,此函数可以被调用以找出有关现有锁定文件的更多信息:

  • 应用程序的 PID (返回在 pid )
  • the hostname it's running on (useful in case of networked filesystems),
  • the name of the application which created it (returned in appname ),

注意, tryLock () automatically deleted the file if there is no running application with this PID, so LockFailedError can only happen if there is an application with this PID (it could be unrelated though).

This can be used to inform users about the existing lock file and give them the choice to delete it. After removing the file using removeStaleLockFile (), the application can call tryLock () again.

此函数返回 true if the information could be successfully retrieved, false if the lock file doesn't exist or doesn't contain the expected data. This can happen if the lock file was deleted between the time where tryLock () failed and the call to this function. Simply call tryLock () again if this happens.

bool QLockFile:: isLocked () const

返回 true 若获得锁通过此 QLockFile 实例,否则返回 false .

另请参阅 lock (), unlock (),和 tryLock ().

bool QLockFile:: lock ()

创建锁定文件。

若另一进程 (或另一线程) 已经创建锁定文件,此函数将阻塞直到进程 (或线程) 释放它为止。

不允许在未先解锁的情况下从同一线程同一锁上多次调用此函数。此函数将 dead-lock 当递归锁定文件时。

返回 true 若获得锁,false 若由于不可恢复的错误 (如:父目录没有权限) 而无法获得锁。

另请参阅 unlock () 和 tryLock ().

bool QLockFile:: removeStaleLockFile ()

试图强行移除现有锁定文件。

不推荐调用此当保护短期活着操作时: QLockFile 已经负责移除锁定文件当它们早于 staleLockTime ().

此方法才应该被调用当保护长时间资源时,即:采用 staleLockTime (0),和之后 tryLock () 返回 LockFailedError ,且用户同意移除锁定文件。

返回 true 当成功时,false 若锁定文件无法被移除。这发生在 Windows,当拥有锁的应用程序仍在运行时。

void QLockFile:: setStaleLockTime ( int staleLockTime )

staleLockTime to be the time in milliseconds after which a lock file is considered stale. The default value is 30000, i.e. 30 seconds. If your application typically keeps the file locked for more than 30 seconds (for instance while saving megabytes of data for 2 minutes), you should set a bigger value using setStaleLockTime().

staleLockTime is used by lock () 和 tryLock () in order to determine when an existing lock file is considered stale, i.e. left over by a crashed process. This is useful for the case where the PID got reused meanwhile, so one way to detect a stale lock file is by the fact that it has been around for a long time.

另请参阅 staleLockTime ().

int QLockFile:: staleLockTime () const

返回时间 (以毫秒为单位),在此时间之后锁定文件被认为陈旧。

另请参阅 setStaleLockTime ().

bool QLockFile:: tryLock ( int timeout = 0)

试图创建锁定文件。此函数返回 true 若获得锁;否则它返回 false . If another process (or another thread) has created the lock file already, this function will wait for at most timeout milliseconds for the lock file to become available.

注意:传递负数作为 timeout 相当于调用 lock (), i.e. this function will wait forever until the lock file can be locked if timeout 为负。

If the lock was obtained, it must be released with unlock () before another process (or thread) can successfully lock it.

Calling this function multiple times on the same lock from the same thread without unlocking first is not allowed, this function will always return false when attempting to lock the file recursively.

另请参阅 lock () 和 unlock ().

void QLockFile:: unlock ()

释放锁,通过删除锁定文件。

调用 unlock() 在首先未锁定文件的情况下,什么都不做。

另请参阅 lock () 和 tryLock ().