Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to obtain an exclusive lock *first* and then downgrade to shared without releasing the lock

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?

like image 948
Elliot Cameron Avatar asked Oct 03 '22 12:10

Elliot Cameron


1 Answers

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

An implementation of this concept is provided by the upgrade_mutex class.

like image 117
ComicSansMS Avatar answered Oct 13 '22 10:10

ComicSansMS