Re: Will Modern C++ Design ever get fixed?
On 10/23/2010 2:32 PM, Miles Bader wrote:
Witold Kuzminski<witold.kuzminski@gmail.com> writes:
Please see the quote from the article you provided the link to,
and which you co-authored; it is from the introduction:
"This article explains why Singleton isn???t thread safe, how DCLP
attempts to address that problem, why DCLP may fail on both
uni- and multiprocessor architectures, and why you can???t
(portably) do anything about it."
How can one do something about it _non-portably_?
E.g., if the single-time initialization of `shared_field' (only called
when shared_field == NULL) looks like:
{
LockGuard guard (lock);
if (! shared_field)
{
local_variable = make_expensive_data_structure ();
MEMORY_BARRIER ();
shared_field = local_variable;
}
}
where "MEMORY_BARRIER" is __sync_synchronize or equivalent (both acts
as a compiler memory-barrier and emits an appropriate CPU-specific
memory-barrier instruction or whatever).
Then I guess one could encapsulate that in a class that used the
efficient technique on systems where some MEMORY_BARRIER was available
(90% ?), and an explicit lock or something on anything else.
Thanks,
-miles
I'm sorry, but your code snipped does not represent Doubly-Checked
Locking Pattern. The whole idea of DCLP is to forgo the locking in case
when "shared_field" has been initialized. Your code will take the lock
in any event.
Furthermore, if your implementation of LockGuard involves POSIX calls,
then there's no need for any MEMORY_BARRIER.
Andy.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]