Boost logo

Boost :

From: Howard Hinnant (hinnant_at_[hidden])
Date: 2003-09-28 12:18:57


On Sep 27, 2003, at 9:50 PM, Daniel Wallin wrote:

> At 02:57 2003-09-28, Howard Hinnant wrote:
>>> In that case, couldn't you allocate the scoped_lock dynamically and
>>> move it
>>> when copying?
>>
>> I was really hoping to avoid heap allocation on this one.
>
> Even if it's pooled? Something like this:
>
> locked_ptr<X> singleton()
> {
> static X x;
> static locker_pool p;
> return locked_ptr<X>(&x, p.allocate());
> }
>
> (sample implementation attached)

Wow, thanks again.

I've taken a closer look at both of your suggestions, and I really like
your first one the best (with recursive_mutex subbed in):

      locked_ptr(const locked_ptr& x)
         : m_p(x.m_p)
         , m_mutex(x.m_mutex)
         , m_lock(x.m_mutex)
      {}
...
      boost::recursive_mutex& m_mutex;
      boost::recursive_mutex::scoped_lock m_lock;

This is the simplicity that I knew I must be missing! :-)

Your second suggestion looks good too. Indeed it looks to me like you
could use your second suggestion with a non-recursive mutex. I think
your second suggestion essentially implements a recursive mutex using a
non-recursive one.

-Howard


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