Boost logo

Boost :

From: Andrew J Bromage (ajb_at_[hidden])
Date: 2002-08-06 21:49:33


G'day all.

On Tue, Aug 06, 2002 at 08:28:49PM -0500, Rene Rivera wrote:

> Hmmm, maybe I missed something but... If it's singleton type data why not
> make it static?

I want it for nonstatic data. Here's what I want to be able to write:

    // Disclaimer: The following code is untested and
    // non-const-correct for brevity.

    class something
    {
    public:
        const expensive_data&
        get_expensive()
        {
            m_once.call_once(&make_expensive_data);
            return *m_expensive;
        }

        something(const other_data& other)
            : m_other(other)
        {
        }

    private:
        boost::once m_once;
        std::auto_ptr<expensive_data> m_expensive;

        other_data m_other;

        void
        make_expensive_data()
        {
            m_expensive = new expensive_data(m_other);
        }
    };

Moreover (and this is what makes it tricky), I want to do it with as
little mutex contention as possible. In particular I want to avoid
obtaining a lock when get_expensive() is called after the expensive
data has been created.

Cheers,
Andrew Bromage


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