如何判断 QMutex 是否被锁定?
有谁知道如何检查并查看 QMutex 是否被锁定,而不使用以下函数:
bool QMutex::tryLock()
我不想使用 tryLock() 的原因是因为它做了两个things:
- 检查互斥体是否被锁定。
- 如果未锁定,则将其锁定。
出于我的目的,我对执行第二步(锁定互斥体)不感兴趣。
我只是想知道它是否被锁定。
Does anyone know how to check and see if a QMutex is locked, without using the function:
bool QMutex::tryLock()
The reason I don't want to use tryLock() is because it does two things:
- Check and see if the mutex is locked.
- If it's not locked then lock it.
For my purposes, I am not interested in performing this second step (locking the mutex).
I just want to know if it is locked or not.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
根据定义,尝试锁定互斥体是判断其是否已锁定的唯一方法;否则,当这个虚构的函数返回时,您如何知道互斥体是否仍然被锁定?当函数返回时,它可能已被解锁;或者更重要的是,如果不执行锁定它所需的所有缓存刷新和同步,您实际上无法确定它是否被锁定。
Trying to lock a mutex is by definition the only way to tell if it's locked; otherwise when this imaginary function returned, how would you know if the mutex was still locked? It may have become unlocked while the function was returning; or more importantly, without performing all the cache-flushing and syncronization necessary to lock it, you couldn't actually be sure if it was locked or not.
好吧,我猜如果不实际使用 tryLock() 就没有真正的方法可以做到我所要求的。
这可以通过以下代码来完成:
如您所见,它会解锁 QMutex“a_mutex”(如果它能够锁定它)。
当然,这不是一个完美的解决方案,因为当它到达第二个 if 语句时,互斥体的状态可能已经改变。
OK, I'm guessing there is no real way to do what I'm asking without actually using tryLock().
This could be accomplished with the following code:
As you can see, it unlocks the QMutex, "a_mutex", if it was able to lock it.
Of course, this is not a perfect solution, as by the time it hits the 2nd if statement, the mutex's status could have changed.
也许是一个拥有一份许可证的 QSemaphore ? available() 方法可能会满足您的需要。
Maybe a QSemaphore with one permit? The available() method may give you what you need.
QMutex 专为锁定和解锁功能而设计。某些自定义计数器可能会满足收集统计数据的要求。
尝试使用前面提到的 @Luca Carion 的 QSemaphore。
QMutex is designed just for locking and unlocking functionality. Gathering statistics may be satisfied with some custom counters.
Try QSemaphore as @Luca Carion mentioned earlier.
该代码应该在 Qt 5 上运行并且不会扰乱互斥状态。
每个 QBasicMutex 都持有一个(原子)指针(称为 d_ptr),如果不被拥有,则为 NULL;如果被拥有但无争议,则为特殊值;或者指向平台相关结构的指针(在 Unix 上,这是基本上是一个 pthread 互斥体)(如果该互斥体是拥有的并且存在争议的话)。
我们需要reinterpret_cast,因为
d_ptr
是私有的。更多信息可以在这里找到:https://woboq.com/blog /internals-of-qmutex-in-qt5.html
一个合法的用例是验证互斥体是否确实被锁定,例如,如果它是函数前提条件。我建议使用
Q_ASSERT(isLocked(...))
来实现此目的。对未锁定的互斥体进行测试本质上是不安全的,不应进行。
This code should work on Qt 5 and doesn't mess with the mutex state.
Every QBasicMutex holds a single (atomic) pointer (called
d_ptr
) that is NULL if not owned, a special value if it is owned but uncontested or a pointer to a platform dependent structure (on Unix, this is basically a pthread mutex) if the mutex is owned and contested.We need the reinterpret_cast because
d_ptr
is private.More info can be found here: https://woboq.com/blog/internals-of-qmutex-in-qt5.html
A legitimate use case is to verify that a mutex is indeed locked, for example if it is a function precondition. I suggest using
Q_ASSERT(isLocked(...))
for this purpose.Testing for an unlocked mutex is inherently unsafe and should not be done.