Boost logo

Boost :

Subject: Re: [boost] [Boost.utility]
From: vicente.botet (vicente.botet_at_[hidden])
Date: 2010-01-26 12:58:48

----- Original Message -----
From: "Andrew Chinkoff" <achinkoff_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Tuesday, January 26, 2010 5:22 PM
Subject: Re: [boost] [Boost.utility]

>> 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?


the test (instance_ == NULL) will be thread-safe only if instance comparation and asignement are atomic. So the smart pointer trick will always improve performances as the stored instance is local to the thread as it is stored on the stack.


Boost list run by bdawes at, gregod at, cpdaniel at, john at