Re: Double checked locking pattern article on aristeia
on Mon Oct 24 2011, Alexander Terekhov <terekhov-AT-web.de> wrote:
Dave Abrahams wrote:
[...]
--8<---------------cut here---------------start------------->8---
Singleton* Singleton::instance ()
{
Singleton* tmp = pInstance;
... // insert memory barrier
if (tmp == 0) {
Lock lock;
tmp = pInstance;
if (tmp == 0) {
tmp = new Singleton;
... // insert memory barrier
pInstance = tmp;
}
}
return tmp;
}
--8<---------------cut here---------------end--------------->8---
Well, it's a little bit hard to say what they're assuming, because under
C++03 you don't have any standard-portable right to expect something
called a "memory barrier" to work, even if you could lay your hands on
one.
That said, I *think* the point here is that without the barriers, no
effects that are formally "visible" according to the standard force the
boundaries of the lock to exclude the read of pInstance or to include
the write... but I would ask the authors to explain this in more detail
if I were you.
With C++11 fences:
Singleton* Singleton::instance () {
Singleton* tmp = pInstance; // atomic relaxed (competing) load
atomic_thread_fence(memory_order_acquire);
if (tmp == 0) {
Lock lock;
tmp = pInstance; // non-competing relaxed load (need not be atomic)
if (tmp == 0) {
tmp = new Singleton;
atomic_thread_fence(memory_order_release);
pInstance = tmp; // atomic relaxed store
}
}
return tmp;
}
Sure... the question isn't how to translate that into C++11, but more,
what's the rationale for placing each of those fences where they are?
My first instinct would have been that you need a fence before the read
(to ensure visibility of anything that's been written to pInstance from
another thread) and after the write (to ensure the write to pInstance
*becomes* visible in other threads). The only rationale I can guess for
the placement above is... well, what I guessed above.
But it's always possible I still don't understand fences correctly ;-)
--
Dave Abrahams
BoostPro Computing
http://www.boostpro.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]