C++
  Home arrow C++ arrow Page 6 - C++ in Theory: Why the Double Check Lock P...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C++

C++ in Theory: Why the Double Check Lock Pattern Isn`t 100% Thread Safe
By: J. Nakamura
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 12
    2005-08-01

    Table of Contents:
  • C++ in Theory: Why the Double Check Lock Pattern Isn`t 100% Thread Safe
  • The Original Problem
  • The Double Checked Locking Pattern
  • Object Creation
  • Multiprocessor Machines
  • Portable Solutions

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    C++ in Theory: Why the Double Check Lock Pattern Isn`t 100% Thread Safe - Portable Solutions


    (Page 6 of 6 )

    At this point you will hopefully realize we are jumping through a lot of hoops, just to make sure we can construct a Singleton the lazy way. The simplest solution is to drop that requirement and initialize and destroy the Singleton in an orderly and controlled fashion. Are you having problems with the destruction sequence of Singletons after ‘at_exit()’ when your application is terminating? Stop being lazy and take control!

    When you add a ‘Create()’ and ‘Destroy()’ function to your Singleton class and call these during your application’s initialization and destruction phases respectively, it will be much easier to control the multithreaded demon lurking in your code.

    If this is not an option, another solution would be to use the mutex in the ‘Instance()’ function the expensive way, but to cache the pointer this function returns. This makes a lot of sense anyway, because  dereferencing a pointer is cheaper than making a function call every time you need to have access to the Singleton.

    Wow… that was quite a trip, trying to implement something that conceptually seemed so simple.

    Acknowledgement

    I would like to thank Scott Meyers for being so kind as to point out that the Double Checked Locked Pattern doesn’t make the Singleton 100 percent thread-safe.

    References

    [Meyers]

    ‘Effective C++’ – ISBN 0201924889

    ‘More Effective C++’ – ISBN 020163371X

    [Meyers/Alexandrescu]

    http://www.aristeia.com/Papers/DDJ_Jul_Aug_2004.pdf

    [Schmidt/Harrison]

    http://www.cs.wustl.edu/~schmidt/PDF/DC-Locking.pdf

    ‘Pattern Oriented Software Architectures Volume 2’ – ISBN 0471606952

    [GAMMA]

    ‘Design Patterns’ – ISBN 0201633612


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · well, why not add an intermediate step to make sure that pInstance is only != 0 when...
       · What about this:if(!initialized){ lock(); if(instance == 0){ instance =...
       · your didn't read the article carefully.p3:Trying to force the compiler to a...
       · if the first thread set the initialized = true first, the second thread will assume...
     

    C++ ARTICLES

    - Paths and Files
    - Directories in C++
    - Focusing on C++ Files
    - Const Correctness in C++
    - Manipulating Streams and Files with C++
    - Streams and Files
    - Multiplying Large Numbers with Karatsuba`s A...
    - Large Numbers
    - Dijkstra`s Shunting Algorithm with STL and C...
    - Brief Introduction to the STL Containers
    - The Standard Template Library
    - Templates in C++
    - C++ Programmer Alerts
    - C++ Programming Tips
    - First Steps in (C) Programming, conclusion






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT