Double-Checked Locking Optimization
I was recently in a discussion about when the double-checked locking pattern should be used. Having never heard of double-checked locking as a specific pattern, I had to do a little digging. Wikipedia to the rescue:
http://en.wikipedia.org/wiki/Double-checked_locking
The article does a great job of summarizing the pattern and when it should be used, but it leaves some questions unanswered about when exactly it breaks down, especially in particular implementations such as C#. So a little more digging unearths these discussions:
http://blogs.msdn.com/brada/archive/2004/05/12/130935.aspx
The above link is a great discussion about the actual problems encountered when using the pattern in C#. In particular, this article:
http://msdn.microsoft.com/msdnmag/issues/05/10/MemoryModels/default.aspx
really gets at the heart of the matter with an in-depth discussion about memory models in general, and the .NET CLR memory model in particular. It then provides a great summary of some low-lock techniques and how they can be correctly implemented.
Conclusion? As difficult as concurrency development is already, it seems an undue to burden to introduce the complexity (and potentially hard-to-find bugs) that these techniques do. However, it's nice to know they're there and exactly when and why they work (and when and why they don't), even if you never need them.
Bryn Rhodes
Database Consulting Group LLC
Comments
Just wanted to say Hello to everyone.
Much to read and learn here, I'm sure I will enjoy !
Posted by: Sensbachtal | March 17, 2008 05:02 PM