By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,193 Members | 826 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,193 IT Pros & Developers. It's quick & easy.

C++ singleton pattern question

P: n/a
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!

Jul 23 '05 #1
Share this Question
Share on Google+
10 Replies


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

regards,

Aiden
<fe****************@gmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
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!


Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Jul 23 '05 #2

P: n/a
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.)?

Jul 23 '05 #3

P: n/a
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

<fe****************@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
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.)?


Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Jul 23 '05 #4

P: n/a
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

<fe****************@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
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.)?


Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Jul 23 '05 #5

P: n/a
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

<fe****************@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
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.)?


Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Jul 23 '05 #6

P: n/a
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

<fe****************@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
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.)?


Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Jul 23 '05 #7

P: n/a
adbarnet wrote:
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.
Jul 23 '05 #8

P: n/a
Ok, just to be complete, lets add the fourth implementation

class Singelton
{
Singelton();

public:

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

cheers
Jul 23 '05 #9

P: n/a
fe****************@gmail.com wrote:
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.

Jul 23 '05 #10

P: n/a

fe****************@gmail.com wrote:
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.

Jul 23 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.