C++ singleton pattern question

F

ferdinand.stefanus

Hi

Could someone tell me what's the difference between these two singleton
implementations:

First implementation:

class Singleton
{
public:
static Singleton& instance() { return _instance; }
//... other members
private:
Singleton();
static Singleton _instance;
//... other members
};

Second implementation:

class Singleton
{
public:
static Singleton* instance() { return _instance; }
static void Initialize() { _instance = new Singleton; }
//... other members
private:
Singleton();
static Singleton* _instance;
//... other members
};

I know that in the first implementation, _instance will be put in the
static memory, and it will always be present, whereas in the second
implementation, memory will only be allocated on the heap after
Singleton::Initialize() is called. But aside from that, is there any
practical difference between the two, like on what circumstances is the
first implementation preferred over the second (or vice versa)?

Thanks!
 
A

adbarnet

For one thing, the pointer implementation has no clean up code - so any
resources the singleton uses may not be returned.

regards,

Aiden
 
F

ferdinand.stefanus

Yes, but let's say I add another method, say CleanUp(), in the second
implementation that deletes _instance, and users are required to call
Initialize and CleanUp before and after using the class, respectively.
Are the first and second now functionally identical? Or is there a
situation where one implementation is better than the other? (e.g. in a
multi-threaded application, etc.)?
 
A

adbarnet

Apart from the explicit vs implicit memory management there is no real
difference - as far as multi - threading is concerned, you need to employ
some synchronisation primitives to protect simulataneous access to your
singleton - the same situation exists in both cases.

The 'popular' issue with threading, is a third possibile implementation
whereby:
MySingleton * instance()
{
static MySingleton * pInst = new MySingleton();

return pInst;
}

That changes your synchronisation requirements - but how is very dependent
on your particular design/usage.

hope that helps,

regards,
Aiden
 
A

adbarnet

Apart from the explicit vs implicit memory management there is no real
difference - as far as multi - threading is concerned, you need to employ
some synchronisation primitives to protect simulataneous access to your
singleton - the same situation exists in both cases.

The 'popular' issue with threading, is a third possibile implementation
whereby:
MySingleton * instance()
{
static MySingleton * pInst = new MySingleton();

return pInst;
}

That changes your synchronisation requirements - but how is very dependent
on your particular design/usage.

hope that helps,

regards,
Aiden
 
A

adbarnet

Apart from the explicit vs implicit memory management there is no real
difference - as far as multi - threading is concerned, you need to employ
some synchronisation primitives to protect simulataneous access to your
singleton - the same situation exists in both cases.

The 'popular' issue with threading, is a third possibile implementation
whereby:
MySingleton * instance()
{
static MySingleton * pInst = new MySingleton();

return pInst;
}

That changes your synchronisation requirements - but how is very dependent
on your particular design/usage.

hope that helps,

regards,
Aiden
 
A

adbarnet

Apart from the explicit vs implicit memory management there is no real
difference - as far as multi - threading is concerned, you need to employ
some synchronisation primitives to protect simulataneous access to your
singleton - the same situation exists in both cases.

The 'popular' issue with threading, is a third possibile implementation
whereby:
MySingleton * instance()
{
static MySingleton * pInst = new MySingleton();

return pInst;
}

That changes your synchronisation requirements - but how is very dependent
on your particular design/usage.

hope that helps,

regards,
Aiden
 
S

Spacen Jasset

adbarnet said:
Apart from the explicit vs implicit memory management there is no real
difference - as far as multi - threading is concerned, you need to employ
some synchronisation primitives to protect simulataneous access to your
singleton - the same situation exists in both cases.

The 'popular' issue with threading, is a third possibile implementation
whereby:
MySingleton * instance()
{
static MySingleton * pInst = new MySingleton();

return pInst;
}
....

Doing is this way, with a static 'inside' a function guarantees that
MySingleton will be initialised even if you try and access it before
main() is called. e.g from another singleton, or initialisation.
 
T

Tomas

Ok, just to be complete, lets add the fourth implementation

class Singelton
{
Singelton();

public:

static Singelton& Instance() { static Singelton s; return s; }
};

cheers
 
R

Rolf Magnus

Yes, but let's say I add another method, say CleanUp(), in the second
implementation that deletes _instance, and users are required to call
Initialize and CleanUp before and after using the class, respectively.
Are the first and second now functionally identical? Or is there a
situation where one implementation is better than the other? (e.g. in a
multi-threaded application, etc.)?

Read up on the "static initialization order fiasco" in this newsgroup's FAQ.
 
N

Noah Roberts

Hi

Could someone tell me what's the difference between these two singleton
implementations:

You don't control initialization in the first case. In the second case
you do.

I usually do something like this:

class Singleton
{
Singleton *instance;
Singleton() {}
public:
void function1() { blah; }
static void function1 { if (instance == NULL) instance = new
Singleton();
instance->function1(); }
};

in Singleton.cpp:
Singleton *Singleton::instance = NULL;

I don't know off hand if C++ allows static and non-static functions to
have same signature...you may have naming issues w/ regard to above.
It is the basic structure that is important. You will probably want a
const accessor also.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,170
Latest member
Andrew1609
Top