gpt4 book ai didi

c++ - 使用 QLockFile::setStaleLockTime() 但锁不会过时?

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:15:05 27 4
gpt4 key购买 nike

我使用以下代码将过时锁定时间设置为 100 毫秒:

QLockFile lock1(fn);
lock1.setStaleLockTime(100);
QVERIFY(lock1.lock());

QLockFile lock2(fn);
lock2.setStaleLockTime(100);
QVERIFY(lock2.lock());

我预计它只会阻塞 100 毫秒,但它会无限期地阻塞。这是为什么?

我是不是误解了锁文件应该如何过时?这是 what the docs say :

The value of staleLockTime is used by lock() and 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.

最佳答案

你误会了什么

If the process holding the lock crashes, the lock file stays on disk and can prevent any other process from accessing the shared resource, ever. For this reason, QLockFile tries to detect such a "stale" lock file, based on the process ID written into the file. To cover the situation that the process ID got reused meanwhile, the current process name is compared to the name of the process that corresponds to the process ID from the lock file. If the process names differ, the lock file is considered stale. Additionally, the last modification time of the lock file (30s by default, for the use case of a short-lived operation) is taken into account. If the lock file is found to be stale, it will be deleted.

所以不仅是 staleLockTime,还有进程 ID 检查和其他事情。所以你不能这样使用这个方法。

关于c++ - 使用 QLockFile::setStaleLockTime() 但锁不会过时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34694680/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com