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  
Dedicated Servers  
Actuate Whitepapers 
Moblin 
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++

The Singleton Pattern Revisited
By: J. Nakamura
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 11
    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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    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

    - 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
    - First Steps in (C) Programming, continued
    - First Steps in (C) Programming, introduction
    - C++ Preprocessor: Always Assert Your Code Is...
    - C++ Preprocessor: The Code in the Middle
    - Programming in C
    - Temporary Variables: Runtime rvalue Detection
    - Temporary Variables: Chasing Temporaries Away







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway