Boost logo

Boost Users :

Subject: [Boost-users] Try-upgrade locking
From: Kelvin Chung (kelvSYC_at_[hidden])
Date: 2011-12-13 12:57:20


Suppose I have a boost::upgrade_lock<boost::shared_mutex>. When I want
to create the boost::upgrade_to_unique_lock<boost::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_lock<boost::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_lock<boost::shared_mutex> upgradeLock(mutex);
...
if (...) {
        // Substitute for boost::upgrade_to_unique_lock<boost::shared_mutex>,
but also with aspects of
        // boost::try_to_lock, basically
        my_upgrade_to_unique_lock<boost::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


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net