
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 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() one.
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) iEYEARECAAYFAktfGNwACgkQ5vihyNWuA4Vn5gCeKxGYmiiIdEAO2EsZFFt2SjHV fGEAoN0qvVFJl3qGtmGBDufYfTWDZrq6 =WKvK -----END PGP SIGNATURE-----