Subject: Re: [boost] [Boost.utility]
From: Frank Mori Hess (frank.hess_at_[hidden])
Date: 2010-01-26 11:31:24
-----BEGIN PGP SIGNED MESSAGE-----
On Tuesday 26 January 2010, Andrew Chinkoff wrote:
> 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()
> Did I miss the point?
First, you are using double-checked locking which is unsafe (google it).
Second, using boost::mutex will open you up to problems from the static
initialization order fiasco (google it too). You should use boost::once to
initialize the singleton object.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
-----END PGP SIGNATURE-----
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk