C++
  Home arrow C++ arrow 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


    (Page 1 of 6 )

    Back in January, Jun Nakamura discussed Singletons in a series of three articles, and revisited the subject in May. In the May article, he described the Double Checked Locking Pattern as a way to make Singletons thread safe. Unfortunately, it's not that simple, as he explains in this article.


    If you wish to catch up with the articles from January covering the Singleton, you may do so here:


    The Double Checked Locking Pattern looks like a simple way to ensure that we can make Singletons thread safe. While the simplest solution is often the best, this one contains complexities underneath that don’t show until you take a closer look...a much closer look.

    C++ as a language has no notion of threading. Unfortunately, this makes it possible for compilers to break our programs in the most unexpected ways!

    As if matters are not complex enough when trying to write thread-safe C++ code, we should also think hard about the machine code a compiler may actually generate from our C++ code. We must always try to be very vigilant. Sometimes, we’ll get lucky and have someone point out what we might have missed.

    In my case Scott Meyers [Meyers] was kind enough to point out that the Double-Checked Locking Pattern [Schmidt/Harrison] described in my previous article "The Singleton Pattern Revisited" is not reliable in C++. I suggest you read the excellent article he and Andrei Alexandrescu wrote on this topic at http://www.aristeia.com/Papers/DDJ_Jul_Aug_2004.pdf for a thorough explanation. I will merely summarize the problem here for you.

    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
    Stay green...Green IT