如何首先获取排他锁,然后降级为共享而不释放锁 [英] How to obtain an exclusive lock *first* and then downgrade to shared without releasing the lock

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

问题描述

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通过 unlock_and_lock_shared() .

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

查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆