C++
  Home arrow C++ arrow Page 3 - 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 Double Checked Locking Pattern


    (Page 3 of 6 )

    The creation of the Singleton has to be synchronized to make sure that only one Singleton can ever be created in a multithreaded environment. We would also like to acquire the needed lock for this synchronization only when the Singleton has not been created yet.

    It is easy to make sure we won’t acquire the lock when it is not needed by checking whether our Singleton exists already or not; only when the pointer is 0 will we acquire the lock before we create the Singleton. It is still possible for a thread to be suspended immediately after the first check but before it receives the lock. Another thread might come in to get the lock and create the Singleton. When the lock is released, the first thread can acquire it and again it has to verify that the Singleton has not been created yet. This is the Double Checked Locking Pattern as conceived by Douglas Schmidt and Tim Harrison [Schmidt/Harrison]:

    Singleton* Singleton::Instance() {

    if (0 == pInstance) {

    Guard lock(m_mutex);

    if (0 == pInstance)

    pInstance = new Singleton;

    }

    return pInstance;

    }

    You will probably look twice the first time you see this pattern applied, but it makes a lot of sense when you look closer. If thread A is suspended after the if statement but before acquiring a lock on the mutex, thread B can come in to acquire the lock and create the Singleton object. If thread B were to be suspended at any point during the creation, thread A will still have to wait for the release of the synchronization lock. The moment thread A resumes execution, pInstance won’t be 0 anymore and it won’t create a second instance of the Singleton.

    Solved! Or is there a snake hiding in the grass? (This is a Dutch proverb. No pun towards Python was intended here).

    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 4 Hosted by Hostway
    Stay green...Green IT