C++
  Home arrow C++ arrow The Singleton Pattern Revisited
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++

The Singleton Pattern Revisited
By: J. Nakamura
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 12
    2005-05-02

    Table of Contents:
  • The Singleton Pattern Revisited
  • The creation of a Singleton
  • Policy-based Class Design
  • Tweaking the Singleton Class with Policies
  • Singleton's Thread Safety

  • 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


    The Singleton Pattern Revisited


    (Page 1 of 5 )

    A reader wrote in some time after we ran a short series on the Singleton pattern with some questions. These included how to construct a singleton when the class has a non-trivial constructor with arguments that are calculated at run-time, and how to use the singleton in a multi-threaded environment. If you were wondering about these issues as well, keep reading.

    Some time ago I had a short email exchange with one of the DevHardware readers. He originally sent me an email with a couple of good questions about the Singleton Pattern articles I wrote. After answering his questions he suggested that the things we discussed in email might be worth another article on the Singleton pattern.

    I think that he was right. There are some issues I haven’t touched upon that can give you serious problems in certain settings. Articles and books are nice until you face the problem of adapting the information you get from them to your own needs.

    The first problem he ran into was how to construct a singleton when the class has a non-trivial constructor with arguments that are calculated at run-time. In the code I provided with my previous articles about the singleton, the class was instantiated in a Creation class which was called a creation policy. They assume that the object that has to be created inside the singleton supports a default constructor (one that takes no arguments).

    The next question he had was whether it was safe to use the singleton code I provided in a multi-threaded environment. Well… it is not. So I'm taking his advice and revisiting the Singleton Pattern based on his questions.

    More C++ Articles
    More By J. Nakamura


       · The "Double-Checked Locking Pattern" is broken per Meyers and Alexandrescu....
       · I may miss something here, but when usingif (!m_instance){ Guard...
       · The m_Mutex is intended to be static, thus it is initialised outside the class. I am...
       · Earlier you used this[b]Guard lock(m_mutex);[/b]But in [i]Double-Checked...
     

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