Re: Double-Checked Locking pattern issue
"George" <George@discussions.microsoft.com> wrote in message
news:AFF9AD5D-1564-4099-AA56-C36A23EA124B@microsoft.com
to memory is expensive, it is conceivable the CPU may reorder the
write to the pointer as early as possible.
The only reason I could think of is writing earlier to memory could
save the register and we could save the register for later use.
What are your points about why writing early will improve
performance? Could you show more description or some pseudo code
please?
I'm not a hardware expert, I don't know how precisely CPUs improve
performance. All I know is that they can and do rearrange instructions
the way I describe, for reasons of their own. So one has to write code
that still works despite of this. Naive implementation of DCLP is broken
by such rearrangement, which is what the article you cited in your first
post is all about.
Besides, even if the CPU does _not_ rearrange instructions after all, on
modern architectures two memory writes performed by one CPU in a certain
order may appear to another CPU as if performed in a different order (so
called weak memory ordering). In particular, the other CPU may observe
the new value of the variable last written to, but the old value for the
variable that was written before. I gave you examples of this in my
first response. Again, naive DCLP implementation is broken by this.
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925