C++
  Home arrow C++ arrow Page 2 - 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 
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 - The Original Problem


    (Page 2 of 6 )

    We have been implementing the Singleton Pattern [Gamma] in C++ to aid us in our quest to make sure we have only one unique instance of an implementation available during runtime. The Singleton is accessed exclusively through the ‘Instance()’ member function, which takes care of object creation and destruction. It also uses lazy initialization, which means that the Singleton is not created and stored until it is first accessed.

    The benefit of lazy initialization is that the Singleton won’t be created until it is needed, and if it isn’t needed, memory and construction time won’t be wasted on it. It does introduce a major problem when we want to use our Singleton in a multithreaded environment.

    Let us ignore policy based class design and everything else described in the previous articles, and look at the Instance function as described in "Design Patterns" [Gamma].

    Singleton* Singleton::Instance () {

    if (0 == pInstance)

    pInstance = new Singleton;

    return pInstance;

    }

    The Singleton should guarantee us only one instance of the implementation at runtime, but le'ts see what might happen when this code is run in a multithreaded environment.

    If thread A is suspended after the if statement and before the creation of the Singleton, thread B might come in (pInstance is still 0) and create a Singleton. As soon as thread A resumes execution it creates a second Singleton. That is not what we had in mind when implementing this pattern!

    Mutexes are used to synchronize thread access to sections of code. When a mutex is placed at the start of an Instance function’s body, we are sure that only one Singleton is ever created:

    Singleton* Singleton::Instance() {

    Guard lock(m_mutex);

    if (0 == pInstance)

    pInstance = new Singleton;

    return pInstance;

    }

    However it is very time consuming to get a lock on a mutex, and this solution may introduce a performance hit when you access the Singleton a lot through the ‘Instance()’ function. It sure doesn’t make sense to have to pay this price when you need it only once (i.e. when you have to create the Singleton object). This is where the Double Checked Locking Pattern comes in.

    More C++ Articles
    More By J. Nakamura


       · 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...
       · I see one problem with the mutex that brings me to the problem that caused me to...
       · Avoid the lock only after the critical section has completed and the function has...
     

    C++ ARTICLES

    - More Tricks to Gain Speed in Programming Con...
    - Easy and Efficient Programming for Contests
    - Preparing For Programming Contests
    - Programming Contests: Why Bother?
    - Polymorphism in C++
    - Overview of Virtual Functions
    - Inheritance in C++
    - Extending the Basic Streams in C++
    - Using Stringstreams in C++
    - Custom Stream Manipulation in C++
    - General Stream Manipulation in C++
    - Serialize Your Class into Streams in C++
    - Advanced File Handling with Streams in C++
    - File Handling and Streams in C++
    - The STL String Class







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek