Re: question re. usage of "static" within static member functions of
a class
On Sep 11, 1:07 am, Jerry Coffin <jerryvcof...@yahoo.com> wrote:
In article <aac0ea5e-c259-4177-9781-d94931593069
@j9g2000prh.googlegroups.com>, joshuamaur...@gmail.com says...
[ ... ]
Firstly and most importantly, you're using double checked
locking, which is broken in effectively all C++
implementations. Don't do that. Please read, continue to
re-read if you don't get it, this excellent paper:
http://www.aristeia.com/Papers/DDJ_Jul_Aug_2004_revised.pdf
This is a decent paper, but it should be kept in context. Up
until shortly before he (helped) write that paper, Andrei
seems to have thought that the 'volatile' keyword was
sufficient to give assurances necessary for multithreading (in
essence that reading or writing a volatile variable acted as a
memory barrier).
I seem to recall that it was a couple of years before the paper
in question. Andrei did (naively) propose a means of achieving
thread safety using volatile. In fact, his solution worked, but
not for the reasons he thought---his solution actually only used
the fact that volatile is part of the type system. In the
following discussions, however, he quickly realized (and openly
admitted) that his understanding wasn't complete; since then
(and before writing the paper in question), he completed it.
The paper was also thoroughly reviewed before publication, to
ensure accuracy.
That paper followed shortly after he realized that this just
wasn't so. The tone of the paper is unfortunate though -- it
comes off as basically saying there's a problem with
double-checked locking, which really isn't the case at all.
This depends on how you defined double checked locking. There
is a definite problem in the code presented by Vlissides in his
original article, and that is what most people understand by
double checked locking. IIRC, the paper in question does make
it clear that double checked locking can be made to work using
assembler (at least on most platforms) or perhaps some
additional, system specific requests (other than just mutexes),
and while I don't think the paper mentions it, it can also be
made to work using thread local storage. In practice, it's
generally not worth it, since the additional assembler generally
does more or less what the outer mutex (which you're trying to
avoid) does, and costs about the same in run time.
The problem is that C++ (up through the 2003 standard) simply
lacks memory barriers. Double-checked locking is one example
of code that _needs_ a memory barrier to work correctly -- but
it's only one example of many.
It can be made to work with thread local storage as well,
without memory barriers.
[...]
The real problem was never with the DCLP itself, but with
attempting to do multi-threaded programming without the tools
necessary for the job.
Yes. The "problem" with DCLP is in fact just a symptom of a
larger problem, of people not understanding what is and is not
guaranteed (and to a lesser degree, of people not really
understanding the costs---acquiring a non-contested mutex is
really very, very cheap, and usually not worth trying to avoid).
--
James Kanze