Boost logo

Boost :

From: Alexander Nasonov (alnsn-mycop_at_[hidden])
Date: 2003-07-25 08:12:41


Sometimes I have to write code like this:

timed_mutex mtx;

void foo()
{
    {
        xtime xt = get_it();
        timed_mutex::scoped_timed_lock lock1(mtx, xt);
        if(lock1)
        {
            // ...
        }
    }

    {
        boost::xtime xt = get_it();
        timed_mutex::scoped_timed_lock lock2(mtx, xt);
        if(lock2)
        {
            // ...
        }
    }
}

I don't like extra scope needed to control locking. IMO, ScopeGuard idiom
can be used to get rid of it. In example below class "lock" plays the role
of ScopeGuard:

void foo()
{
    xtime xt = get_it();
    if(lock lock1 = timed_mutex::scoped_timed_lock(mtx, xt))
    {
        // ...
    }

    xt = get_it();
    if(lock lock2 = timed_mutex::scoped_timed_lock(mtx, xt))
    {
        // ...
    }
}

-- 
Alexander Nasonov
Remove minus and all between minus and at from my e-mail for timely response

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