本文介绍了如何首先获取排他锁,然后降级为共享而不释放锁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Stack Overflow有几个示例,其中一个函数首先首先获得可升级锁,然后然后通过升级获得 独占访问权限.我的理解是,如果不仔细使用,此可能会导致死锁,因为两个线程可能都获得了可升级/共享的锁,然后都试图进行升级,这时两个线程都无法进行,因为另一个线程具有共享锁

Stack Overflow has several examples where a function obtains an upgradeable lock first and then obtains exclusive access by upgrading. My understanding is that this can cause deadlocks if not used carefully since two threads may both obtain the upgradeable/shared lock and then both attempt to upgrade, at which point neither can proceed because the other has a shared lock.

我想要的是先获取互斥锁,然后降级为共享锁,而不必完全释放该锁.我找不到这样的例子.有什么想法吗?

What I want is to obtain the exclusive lock first and then downgrade to a shared lock without releasing the lock completely. I cannot find an example of this. Any ideas?

推荐答案

Boost通过 UpgradeLockable 概念.您正在寻找的方法是 unlock_and_lock_shared() .

Boost offers this functionality through the UpgradeLockable concept. The method you are looking for is unlock_and_lock_shared().

upgrade_mutex 类.

这篇关于如何首先获取排他锁,然后降级为共享而不释放锁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 13:52