简体   繁体   English

将boost :: shared_lock升级为独占锁

[英]Upgrading boost::shared_lock to exclusive lock

Could someone please explain the correct usage for boost::upgrade_lock. 有人可以解释一下boost :: upgrade_lock的正确用法。 The following code results in a deadlock 以下代码导致死锁

//Global
typedef boost::shared_mutex Mutex;
typedef boost::shared_lock<Mutex> ReadLock;
typedef boost::upgrade_lock<Mutex> UpgradeLock; 
typedef boost::upgrade_to_unique_lock<Mutex> WriteLock;
Mutex sharedMutex;


//Multi threaded reader and writer
{
    ReadLock read(sharedMutex);

    for (int ii = 0; ii < vec.size(); ++ii) {
        Element e = vec[ii];

        if (e.needsUpdating()) {
            UpgradeLock upgrade(sharedMutex);

            WriteLock write(upgrade)

            //Do stuff
        }
    }
}

It doesn't deadlock if i unlock the read lock with read.unlock() before upgrading. 如果我在升级之前用read.unlock()解锁读锁,它不会死锁。 But it seems that this shouldn't be necessary? 但似乎这不应该是必要的吗?

In the boost::shared_mutex class (which implements the UpgradeLockable concept), a single thread should not attempt to acquire both a shared and an upgradeable (or unique) lock. boost::shared_mutex类(实现UpgradeLockable概念)中,单个线程不应尝试同时获取共享和可升级(或唯一)锁。 At any time, the UpgradeLockable can have N shared locks held (via lock_shared ), and 1 upgradeable lock (via lock_upgrade ). 在任何时候,UpgradeLockable都可以拥有N个共享锁(通过lock_shared )和1个可升级锁(通过lock_upgrade )。 The upgradeable lock can request that it become a unique lock, which blocks until it can become the exclusive holder, which requires all shared locks be released. 可升级锁可以请求它成为一个唯一的锁,它会阻塞,直到它成为独占持有者,这需要释放所有共享锁。 It is impossible to convert from a shared lock to a unique lock, or a shared lock to an upgradeable lock without releasing the shared lock first. 在不释放共享锁的情况下,无法从共享锁转换为唯一锁,或共享锁转换为可升级锁。

Note, the upgradeable lock is not exclusive (other shared locks can be held) just that it has special privileges to increase its strength. 注意,可升级锁不是独占的(可以保持其他共享锁)只是它具有增加其强度的特殊权限。 Unfortunately, multiple upgradeable threads are not allowed at the same time. 不幸的是,不允许同时使用多个可升级的线程。

In your case, the same thread is attempting to use lock_shared and lock_upgrade , which will deadlock. 在您的情况下,同一个线程正在尝试使用lock_sharedlock_upgrade ,这将导致死锁。 You can rewrite it as below, and it won't deadlock, but it's still a single point of contention for all of the readers, since only 1 will hold the upgrade lock at a time. 您可以按如下所示重写它,它不会死锁,但它仍然是所有读者的单点争用,因为只有1个一次会保留升级锁。 In which case, depending on your other functions, the complexity of a shared_mutex might not be necessary. 在这种情况下,根据您的其他功能,可能不需要shared_mutex的复杂性。 However, if other functions are still acquiring shared locks, then the below will perform as you expect. 但是,如果其他功能仍在获取共享锁,则下面的内容将按预期执行。

//Multi threaded reader and writer
{
    // Only 1 thread can pass this.  Other shared locks are also valid
    UpgradeLock read(sharedMutex); 

    for (int ii = 0; ii < vec.size(); ++ii) {
        Element e = vec[ii];

        if (e.needsUpdating()) {
            // Blocks here until all shareds are released
            WriteLock write(upgrade)

            //Do stuff
        }
    }
}

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

相关问题 Boost Shared_lock / Unique_lock,给作者优先权? - Boost Shared_lock / Unique_lock, giving the writer priority? 错误:没有匹配函数调用&#39;boost :: shared_lock <boost::shared_mutex> :: shared_lock(const Lock&)&#39; - error: no matching function for call to 'boost::shared_lock<boost::shared_mutex>::shared_lock(const Lock&)' 读写器锁的boost :: unique_lock和boost :: shared_lock - boost::unique_lock and boost::shared_lock for reader writer locks 使用boost :: upgrade_lock和shared_lock实现读/写线程安全 - Using boost::upgrade_lock and shared_lock to implement read/write thread safety 无法将参数1从&#39;const boost :: shared_mutex&#39;转换为&#39;const boost :: shared_lock <Mutex> &” - cannot convert parameter 1 from 'const boost::shared_mutex' to 'const boost::shared_lock<Mutex> &' 用 std::atomic 实现的 shared_lock - shared_lock implemented with std::atomic 将 shared_lock 升级为独特的锁使用、时序和设计 - Upgrade shared_lock to unique lock usage, timing and design 根据条件创建 shared_lock 或 unique_lock - Create shared_lock or unique_lock based on a condition 何时在 std::shared_lock 上使用 defer_lock? - When to use defer_lock on std::shared_lock? 具有两个独占锁组的共享锁 - Shared lock with two exclusive lock groups
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM