如何判断 QMutex 是否被锁定?

发布于 2024-12-23 15:47:04 字数 241 浏览 5 评论 0原文

有谁知道如何检查并查看 QMutex 是否被锁定,而不使用以下函数:

bool QMutex::tryLock()

我不想使用 tryLock() 的原因是因为它做了两个things:

  1. 检查互斥体是否被锁定。
  2. 如果未锁定,则将其锁定。

出于我的目的,我对执行第二步(锁定互斥体)不感兴趣。

我只是想知道它是否被锁定。

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:

  1. Check and see if the mutex is locked.
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

忘东忘西忘不掉你 2024-12-30 15:47:05

根据定义,尝试锁定互斥体是判断其是否已锁定的唯一方法;否则,当这个虚构的函数返回时,您如何知道互斥体是否仍然被锁定?当函数返回时,它可能已被解锁;或者更重要的是,如果不执行锁定它所需的所有缓存刷新和同步,您实际上无法确定它是否被锁定。

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.

早乙女 2024-12-30 15:47:05

好吧,我猜如果不实际使用 tryLock() 就没有真正的方法可以做到我所要求的。

这可以通过以下代码来完成:

bool is_locked = true;

if( a_mutex.tryLock() )
{
    a_mutex.unlock();
    is_locked = false;
}

if( is_locked )
{
    ...
}

如您所见,它会解锁 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:

bool is_locked = true;

if( a_mutex.tryLock() )
{
    a_mutex.unlock();
    is_locked = false;
}

if( is_locked )
{
    ...
}

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.

鹿童谣 2024-12-30 15:47:05

也许是一个拥有一份许可证的 QSemaphore ? available() 方法可能会满足您的需要。

Maybe a QSemaphore with one permit? The available() method may give you what you need.

尴尬癌患者 2024-12-30 15:47:05

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.

眼眸印温柔 2024-12-30 15:47:05
static bool isLocked(const QBasicMutex *mut) {
  auto mdata = reinterpret_cast<const QBasicAtomicPointer<QMutexData> *>(mut);
  return mdata->load();
}

该代码应该在 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(...)) 来实现此目的。

对未锁定的互斥体进行测试本质上是不安全的,不应进行。

static bool isLocked(const QBasicMutex *mut) {
  auto mdata = reinterpret_cast<const QBasicAtomicPointer<QMutexData> *>(mut);
  return mdata->load();
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文