简体   繁体   English

查询std :: mutex的锁定状态

[英]Query std::mutex for lock state

I have a situation where I'd like to do something like that shown below, but there doesn't seem to be a way of querying the mutex without changing its state. 我有一种情况,我想做一些如下所示的事情,但似乎没有办法在不改变状态的情况下查询互斥锁。 I don't want the someFunctionCalledRepeatedlyFromAnotherThread() to hang around waiting for the mutex to free up if it is locked. 我不希望someFunctionCalledRepeatedlyFromAnotherThread()在等待互斥锁释放时等待挂起。 It must return immediately after performing some alternative action. 它必须在执行一些替代操作后立即返回。 I'm guessing this omission is there for safety, as the lock may free up in the time between querying it and the function returning. 我猜这个遗漏是为了安全,因为锁可能在查询它和返回函数之间释放。 In my case, bad stuff will not happen if the lock is freed while doSomeAlternativeAction() is happening. 在我的情况下,如果在doSomeAlternativeAction()发生时释放了锁,则不会发生错误。 The fact I'm in this situation probably means I'm doing something wrong, so how should I change my design? 我在这种情况下的事实可能意味着我做错了什么,所以我应该如何改变我的设计呢?

class MyClass 
{
    std::mutex initMutex;
public:
    void someInitializationFunction()
    {
        std::lock_guard<std::mutex> lock(initMutex);
        // Do some work
    }

    void someFunctionCalledRepeatedlyFromAnotherThread()
    {
        if (initMutex.isLocked()) 
        {
            doSomeAlternativeAction();
            return;
        }
        // otherwise . . .
        std::lock_guard<std::mutex> lock(initMutex);
        // carry on with work as usual
    }
}

Asking mutex for its state is useless: it may be unlocked now, but by the time you get around to lock it, it may be locked. 要求互斥锁的状态是没用的:它现在可以解锁,但是当你绕过锁定它时,它可能会被锁定。 So it does not have such method. 所以它没有这样的方法。

It does, however, have a method try_lock() which locks it if it is not locked and returns true if it acquired the lock and false otherwise. 但是,它确实有一个方法try_lock() ,如果它没有被锁定就锁定它,如果它获得了锁则返回true ,否则返回false And std::unique_lock (the more advanced version of std::lock_guard ) has option to call it. std::unique_lockstd::lock_guard的更高级版本)可以选择调用它。

So you can do: 所以你可以这样做:

void someFunctionCalledRepeatedlyFromAnotherThread()
{
    std::unique_lock<std::mutex> lock(initMutex, std::try_to_lock);
    if(!lock.owns_lock())
    {
        doSomeAlternativeAction();
        return;
    }
    // otherwise ... go ahead, you have the lock
}

Sounds like you want to use std::unique_lock instead of std::lock_guard. 听起来你想使用std :: unique_lock而不是std :: lock_guard。 The try_lock method works similar to the TryEnterCriticalSection on Windows, whereby the function will atomically acquire the lock and return 'true' if it can, or just return 'false' if the lock cannot be acquired (and will not block). try_lock方法的工作方式类似于Windows上的TryEnterCriticalSection,该函数将自动获取锁,如果可以,则返回'true',如果无法获取锁定,则返回'false'(并且不会阻塞)。 Refer to http://msdn.microsoft.com/en-us/library/hh921439.aspx and http://en.cppreference.com/w/cpp/thread/unique_lock/try_lock . 请参阅http://msdn.microsoft.com/en-us/library/hh921439.aspxhttp://en.cppreference.com/w/cpp/thread/unique_lock/try_lock Note that unique_lock also has other members available for trying to lock such as try_lock_for and try_lock_until. 请注意,unique_lock还有其他可用于尝试锁定的成员,例如try_lock_for和try_lock_until。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM