Boost logo

Boost :

Subject: Re: [boost] [thread] upgrading read lock to write lock
From: Peter Dimov (pdimov_at_[hidden])
Date: 2011-02-06 18:10:12


Howard Hinnant wrote:
...
> int main()
> {
>
> ting::upgrade_mutex m;
> ting::upgrade_lock<ting::upgrade_mutex> readlock(m);
> // reads go here

The problem is that only one thread can do this at a time, and I suspect
that in the original problems, there are many such readers, some of which
need to then write, but only occasionally. This is one way to do it:

take read lock;
int v = data.version_;
// reads go here
drop read lock;

if( need to write )
{
    take write lock;

    if( v != data.version_ )
    {
        retry reads;
    }

    if( still need to write )
    {
        ++data.version_;
        // writes go here
    }

    drop write lock;
}

Note that no upgrade locks are needed.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk