Boost logo

Boost Users :

From: Anthony Williams (anthony.ajw_at_[hidden])
Date: 2008-08-13 10:40:32


"Igor R" <boost.lists_at_[hidden]> writes:

> This's rather basic question, but I couldn't find any example in the docs.
>
> typedef boost::shared_lock<boost::shared_mutex> shared_lock;
> shared_mutex mutex_;
> ///
> {
> shared_lock lock(mutex_);
> // reading locked data...
>
> // here I want to midify the data, how can I upgrade the lock to be exclusive?
> }

You can't change a shared_lock to an unique_lock. If you want to do
this, use an upgrade_lock. There can be at most one upgrade_lock held
at any one time on a shared_mutex, but an upgrade_lock can share
ownership with any number of shared_locks. You can then upgrade the
lock by transferring ownership from the upgrade_lock to a unique_lock:

{
    upgrade_lock<shared_mutex> lock(mutex_);
    // reading locked data

    if(want_to_upgrade)
    {
        unique_lock<shared_mutex> lock2(boost::move(lock));
        // access with unique ownership
    }
}

Anthony

-- 
Anthony Williams            | Just Software Solutions Ltd
Custom Software Development | http://www.justsoftwaresolutions.co.uk
Registered in England, Company Number 5478976.
Registered Office: 15 Carrallack Mews, St Just, Cornwall, TR19 7UL

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