C++ singleton


 
Thread Tools Search this Thread
Top Forums Programming C++ singleton
# 8  
Old 10-05-2012
If the pointer is non-null, it's already been modified the one time it's going to get modified so it no longer needs to be protected by locking the mutex and the implied memory barriers

Last edited by Corona688; 10-05-2012 at 12:13 PM..
# 9  
Old 10-05-2012
Quote:
Originally Posted by achenle
If the pointer is non-null, it's already been modified the one time it's going to get modified so it no longer needs to be protected by locking the mutex and the implied memory barriers
You have not addressed the issues I laid out in my last post with regards to why the above statement is irrelevant. Did you deliberately ignore them, or did I not explain them satisfactorily/did you not understand them?

I was in your position not too long ago - I did not believe the person saying what I am saying now, but I was willing to learn and be proved wrong, so I did some research.

A quick web search revealed that it's called double-checked locking. Have a read of the external links at the bottom of that article (especially "C++ and the Perils of Double-Checked Locking") as well as The "Double-Checked Locking is Broken" Declaration which is very informative.

Last edited by Corona688; 10-05-2012 at 12:18 PM..
# 10  
Old 10-05-2012
Moderator's Comments:
Mod Comment Both of you, cool it down. This is programming, not national policy.
# 11  
Old 11-06-2012
I'm fond of this method....

Code:
class Single
{
public:
  static Single &getSingle()
  {
    static Single theSingle;
    return theSingle;
  }

private:
  Single();  // private constructor to enforce singleton

//public:  // (1) if you uncomment this line then (2) below
           //       can be made to compile (and crash!).
  ~Single();
};

Single::Single()
{
}

Single::~Single()
{
}

int main()
{
  Single &s = Single::getSingle();
//  delete &s;  // (2) won't compile if you uncomment unless you
                //     uncomment (1) above; but, who would do this?
  return 0;
}

This has the benefit that the reference can't be freed (though, as I show, the caller can jump through hoops to free it, so the private destructor is still good practice). However, I'd suspect that most people would see a reference and know that deleting it is probably bad.

Buyer beware, however, that the standard is pretty hush-hush on its thread safety. Though, most practical compilers (g++) wrap the local static member ("theSingle") with locks to insure it does not get constructed more than once. In my opinion, this is preferable to roll-your-own. However, if you must, a pthread_once_t control is probably the way to do it as here:

Code:
#include <pthread.h>

class Single
{
public:
  static Single &getSingle()
  {
     pthread_once(&onceCtl, BuildTheSingle);
     return *theSingle;
  }

private:
  static Single *theSingle;
  static pthread_once_t onceCtl;
  static void BuildTheSingle();

  Single();
  ~Single();
};

pthread_once_t Single::onceCtl = PTHREAD_ONCE_INIT;
Single *Single::theSingle = NULL;

void Single::BuildTheSingle()
{
  theSingle = new Single();
}

Single::Single()
{
}

Single::~Single()
{
}

Which uses the same reference return to, IMO, instruct the caller that it's not theirs and they shouldn't be free'ing it.

I suppose you could also use auto pointers or something, too, but...this is good enough for me, lol.

edit: for the record, the former implementation is thread-safe in C++0x, as such, in any newer code compiled with C++0x compliant compiler, I'd recommend it to rid all the boilerplate crap and distill it right to only that code which is both sensible and required. Just one man's opinion....

Last edited by DreamWarrior; 11-06-2012 at 11:11 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

3 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

AIX errpt : ( E7A89C7D Local adapter disabled after unstable singleton for long time )

Medel : 9117-MMC OS: AIX 6.1 Patch level : 6100-07-04-1216 Hacmp version : HACMP v 6.1.0.8 Oracle : 11.2.0.3 RAC Node : 2 node Dear, my one node server has been restarted early this morning, So, i tried to start HA and Oracle database. after that, the follow error appears at the node... (1 Reply)
Discussion started by: tomato00
1 Replies

2. Programming

C++ abstract (singleton) factory implementation...

I want to create an abstract factory template which will allow me to pass in an "ID" for a subclass and return the singleton instance of that class stored in the factory. It'd be easy to adapt for "multi-ton", but for its present use this isn't necessary. The requirements are: - I don't want... (2 Replies)
Discussion started by: DreamWarrior
2 Replies

3. Programming

What is singleton class ?

hello members, What is singleton class in c++ and in which case we will go with singleton class. Thanks & Regards Rajkumar_g (2 Replies)
Discussion started by: rajkumar_g
2 Replies
Login or Register to Ask a Question