|
Boost : |
Subject: Re: [boost] [Boost.utility]
From: Andrew Chinkoff (achinkoff_at_[hidden])
Date: 2010-01-26 11:22:25
> I think you missed the point. A typical spelling is
some_singleton_class::instance(). That member function
> must determine whether the Singleton has been instantiated and that, often
> in a thread safe way. Thus,
> there's a function call and some sort of synchronized access/manipulation
> of state. That's costly.
> In my version, that cost is paid once per context by the smart pointer
> constructor. All accesses via that
> smart pointer *do not* incur the instance() overhead.
Below is the typical realization of A::Instance():
static A& Instance()
{
if (instance_ == NULL)
{
boost::mutex::scoped_lock locker(mtx_); // this is the thread safe cost!
instance_ = new A();
}
return *instance_;
}
You should note that:
1) Cost for thread safe synchronization is paid only once. After instance
had created this cost is no longer paid.
2) Cost for function call (A::Instance()) is replaced with smart_ptr::get()
one.
Did I miss the point?
-- View this message in context: http://old.nabble.com/-Boost.utility--tp27309940p27325361.html Sent from the Boost - Dev mailing list archive at Nabble.com.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk