简体   繁体   English

如何正确使用boost :: timed_mutex和scoped_lock

[英]How to correctly use boost::timed_mutex and scoped_lock

I am trying to use both timed_mutex with the scoped_lock. 我正在尝试将timed_mutex与scoped_lock一起使用。 I have successfully used the scoped_lock before by following some examples but now I don't seem to find my way around neither I am able to properly understand the boost documentation. 在遵循以下示例之前,我已经成功使用了scoped_lock,但是现在我似乎找不到办法,无法正确理解boost文档。

The desired behavior is the following: try to acquire an scoped_lock for x time, if successful return true otherwise return false. 所需的行为如下:尝试获取x时间的scoped_lock,如果成功返回true,否则返回false。

Currently I have: 目前我有:

boost::timed_mutex _mutex;
boost::timed_mutex::scoped_lock scoped_lock(_mutex, boost::get_system_time() + boost::posix_time::miliseconds(10));

However when I try to find (through boost documentation or examples) if this scoped_lock will return a boolean or not I find nothing or find really different ways to do it. 但是,当我尝试查找(通过增强文档或示例)此scoped_lock是否返回布尔值时,我什么也没找到,或者找到了完全不同的方法来实现。

Therefore I ask which is the correct way to do it, how does it exactly works and maybe some indication as to how correctly "read" the documentation of boost. 因此,我想知道哪种方法是正确的,它是如何工作的,也许还可以说明如何正确“阅读” boost的文档。

UPDATE: 更新:

So 所以

boost::timed_mutex _mutex;
boost::timed_mutex::scoped_lock scoped_lock(_mutex, boost::get_system_time() + boost::posix_time::miliseconds(10));

if(scoped_lock.owns_lock()) {
    // exclusive code 
}

Will create a mutex which when I try to lock with scoped_lock.owns_lock() will try to acquire the lock during 10 miliseconds (in this case) and return false if the time's up and the lock wasn't acquired? 是否将创建一个互斥锁,当我尝试使用scoped_lock.owns_lock()进行锁定时,它将尝试在10毫秒内获取锁定(在这种情况下),如果时间到了并且未获得锁定,则返回false?

If you look at the documentation , boost::timed_mutex::scoped_lock is just an alias for boost::unique_lock<timed_mutex> : 如果您查看文档boost::timed_mutex::scoped_lock只是boost::unique_lock<timed_mutex>的别名:

class timed_mutex:
    boost::noncopyable
{
public:
    // ...

    typedef unique_lock<timed_mutex> scoped_timed_lock;
    typedef unspecified-type scoped_try_lock;
    typedef scoped_timed_lock scoped_lock;

    // ...
};

Now checking out the documentation for boost::unique_lock , it shows that there are two ways to determine if you own the lock: 现在查看boost::unique_lock文档 ,它显示了确定您是否拥有锁的两种方法:

template<typename Lockable>
class unique_lock
{
public:
    // ...

    explicit operator bool() const noexcept;
    bool owns_lock() const noexcept;

    // ...
};

Therefore, you can do either 因此,您可以

if(scoped_lock) {
    // we have the lock, yay!
}

or 要么

if(scoped_lock.owns_lock()) {
    // we have the lock, yay!
}

Incidentally, unique_lock has a constructor that takes relative time as a chrono::duration, which may or may not be cleaner than using absolute time. 顺便说一句,unique_lock具有一个将相对时间作为chrono :: duration的构造函数,它可能比使用绝对时间更干净。

Edit: Given this code: 编辑:给出此代码:

boost::timed_mutex _mutex;
boost::timed_mutex::scoped_lock scoped_lock(_mutex,
           boost::get_system_time() + boost::posix_time::miliseconds(10)); // <-- attempt to acquire mutex happens here!

if(scoped_lock.owns_lock()) {
    // exclusive code 
}

The attempt to acquire the mutex happens at the time the lock is constructed, not at the time owns_lock() is called. 获取互斥锁的尝试发生在构造锁时,而不是在调用owns_lock() Yes, the exclusive code will only execute if you successfully acquired the mutex. 是的,仅当您成功获取互斥锁后,排他代码才会执行​​。 I'm not sure what you mean by "return false" - this code doesn't return anything. 我不确定“返回假”是什么意思-此代码不返回任何内容。 If owns_lock() returns false, then you have failed to acquire the mutex and can't run the exclusive code, and you can communicate this to your caller anyway you want. 如果owns_lock()返回false,则说明您无法获取互斥体并且无法运行独占代码,并且可以随时将其与调用方进行通信。

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

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