
Le 13/12/11 18:57, Kelvin Chung a écrit :
Suppose I have a boost::upgrade_lockboost::shared_mutex. When I want to create the boost::upgrade_to_unique_lockboost::shared_mutex and upgrade to exclusive locking, this blocks until all the shared lock holders leave. However, it does not appear that there is a non-blocking version of this, unlike, say, making a boost::unique_lockboost::shared_mutex and passing in boost::try_to_lock.
So what should I do in order to create my own "try-upgrade" lock? I'm trying to use it like so:
boost::upgrade_lockboost::shared_mutex upgradeLock(mutex); ... if (...) { // Substitute for boost::upgrade_to_unique_lockboost::shared_mutex, but also with aspects of // boost::try_to_lock, basically my_upgrade_to_unique_lockboost::shared_mutex deferredWriteLock(lock);
// Do stuff where it doesn't matter whether I have exclusive or not, while the readers leave if (!deferredWriteLock) deferredWriteLock.lock(); // Now we need exclusive // We have exclusive, do more stuff } // Back to regular upgrade_lock here
Hi,
There is a draft proposal from Howard Hinnant "Shared locking in C++"
http://home.roadrunner.com/~hinnant/bloomington/shared_mutex.html that
doesn't have upgrade_to_unique_lock but that allows to try to lock an
upgrade_lock. Even if the interface is a little bit different I guess
that the lecture will help you a lot.
If I understand you wan to lock a mutex with shared ownership until you
need to have exclusive ownership and then retrieve the shard ownership
again. Unfortunately I think that the current interface doesn't let you
to try to move from shared to exclusive ownership without blocking, and
I don't know how to move from a exclusive ownership to a shared on.
If you can block when getting exclusive ownership, you will be able to
implement it with the current interface as follows (Note pseudo-code)
IUC, Howard's proposal contains all the ingredient to do it using a an
initial shared_lock, trying to move to an upgrade_lock and if successful
moving then to a unique_lock and last moving back to the initial
shared_lock. See the example:
upgrade_mutex mut;
// ...
void foo()
{
shared_lock