singleton

T

thomas

hi guys,
-----code-----
class A{
private:
A(){}
public:
static A *getInstance(){
static A *a = new A(); //-------L1----
return a;
}
};
-----code-----

I use the static keyword to declare a static instance A,
I wonder if getInstance() is called multiple times, will L1 be
executed multiple times?

generally a initialization sentence of static variable will be
executed only once, but I'm not sure what will the c++ compiler expand
for L1.
 
I

Ian Collins

thomas said:
hi guys,
-----code-----
class A{
private:
A(){}
public:
static A *getInstance(){
static A *a = new A(); //-------L1----
return a;
}
};
-----code-----

I use the static keyword to declare a static instance A,
I wonder if getInstance() is called multiple times, will L1 be
executed multiple times?
No, function static variables are initialised once when the function is
first called.

You could also write

static A *getInstance(){
static A a ;
return &a;
}
 
D

dave_mikesell

hi guys,
-----code-----
class A{
private:
A(){}
public:
static A *getInstance(){
static A *a = new A(); //-------L1----
return a;
}};

-----code-----

I use the static keyword to declare a static instance A,
I wonder if getInstance() is called multiple times, will L1 be
executed multiple times?

Put a debug print statement in A() and write a small test program and
you'll have your answer.
 
R

Rahul

hi guys,
-----code-----
class A{
private:
A(){}
public:
static A *getInstance(){
static A *a = new A(); //-------L1----
return a;
}};

-----code-----

I use the static keyword to declare a static instance A,
I wonder if getInstance() is called multiple times, will L1 be
executed multiple times?

generally a initialization sentence of static variable will be
executed only once, but I'm not sure what will the c++ compiler expand
for L1.

make sure its thread safe too... you could consider using a mutex or
binary semaphore for the same...
 
I

Ian Collins

Rahul said:
make sure its thread safe too... you could consider using a mutex or
binary semaphore for the same...

For the same what? The implementation should guarantee the static
variable is initialised once only.
 
C

Colin

class A{
private:
A(){}
static A* a=null;
public:
static A *getInstance(){
if(a==null)
{
a=new A(); //remeber delete it..
}
return a;
}
};
 
R

Rahul

class A{
private:
A(){}
static A* a=null;
public:
static A *getInstance(){
if(a==null)
{
a=new A(); //remeber delete it..
}
return a;
}

};

What if the static member function getInstance() is invoked by two
threads and when one thread enters into the if condition and a thread-
switch happens causing the other thread too to enter into the if
condition? It would become a race condition and would result in a
memory leak...

I hope you got the point...
 
I

Ian Collins

[please don't top-post, corrected]
class A{
private:
A(){}
static A* a=null;

The above is illegal (you can't initialise a non-const static member
this way) and you haven't declared null.
public:
static A *getInstance(){
if(a==null)
{
a=new A(); //remeber delete it..

This form is only really useful if you intend to delete the singleton.
 
M

Martin York

hi guys,
-----code-----
class A{
private:
A(){}
public:
static A *getInstance(){
static A *a = new A(); //-------L1----
return a;
}};

-----code-----

I use the static keyword to declare a static instance A,
I wonder if getInstance() is called multiple times, will L1 be
executed multiple times?

generally a initialization sentence of static variable will be
executed only once, but I'm not sure what will the c++ compiler expand
for L1.


Why return a pointer?
Why not return a reference then you do not have any confusion on who
is the owner!


public:
static A& getInstance()
{
static A a;
return a;
}
 
J

James Kanze

No, function static variables are initialised once when the function is
first called.

More correctly, when control flow first encounters the
declaration. No difference here, but if there were code before
the declaration, it could throw. (And of course, they're not
"function static variables", but "block scope static variables";
they can occur in an if, for example.)
You could also write
static A *getInstance(){
static A a ;
return &a;
}

Note that neither are necessarily thread-safe.

Also (just for completeness' sake), the variable is only
considered initialized once the complete initialization has
finished. If in one of the above, the constructor of A throws
an exception, it will be called again the next time the function
is called.
 
J

James Kanze

For the same what? The implementation should guarantee the
static variable is initialised once only.

The standard guarantees that the variable is only initialized
once, unless, of course, the program has undefined behavior
elsewhere. The standard also considers a call to pthread_create
(or it's equivalent under Windows) undefined behavior, however,
so anytime threading is involved, you have to consider the
guarantees given by the implementation. As far as I know, g++
is the only compiler which guarantees "thread safety" for the
above, and it's implementation is defective on at least one
platform. So as a programmer, you do have to pay attention.
 
J

James Kanze

[...]
This form is only really useful if you intend to delete the
singleton.

This form is the most usual case in my code, precisely because I
don't want to delete the singleton. In fact, I don't even want
its destructor to ever be called.
 
M

ManicQin

Thmas if you look closly you'll see the difference between what you
wrote and what Colin wrote
Colin's class A has a static member variable that the singleton is
responsible for keeping only one instance of him in the system.
Thomas's code is a singleton that creates new instances of a local
variable each time getInsatnce is called, (which is not very singleton
of him... Unless Thomas gave a wrong code...)

Colin:
 
T

Thomas J. Gritzan

[top-posting corrected]
> Thmas if you look closly you'll see the difference between what you
> wrote and what Colin wrote
> Colin's class A has a static member variable that the singleton is
> responsible for keeping only one instance of him in the system.
> Thomas's code is a singleton that creates new instances of a local
> variable each time getInsatnce is called, (which is not very singleton
> of him... Unless Thomas gave a wrong code...)

That's wrong. Both versions create only one instance of the object.

A variable declared 'static' locally in a function body is initialized only
once.
 
M

ManicQin

That's wrong. Both versions create only one instance of the object.

A variable declared 'static' locally in a function body is initialized only
once.
Yhe once I pressed the send button I started doubting myself and
realized that basically that's the point of the entire thread... I
removed the post.
 
I

Ian Collins

James said:
[...]
This form is only really useful if you intend to delete the
singleton.

This form is the most usual case in my code, precisely because I
don't want to delete the singleton. In fact, I don't even want
its destructor to ever be called.
If you don't want to delete the instance, why not use a "block scope
static variable"?
 
K

Kai-Uwe Bux

Ian said:
James said:
[...]
public:
static A *getInstance(){
if(a==null)
{
a=new A(); //remeber delete it..
This form is only really useful if you intend to delete the
singleton.

This form is the most usual case in my code, precisely because I
don't want to delete the singleton. In fact, I don't even want
its destructor to ever be called.
If you don't want to delete the instance, why not use a "block scope
static variable"?

Aren't static objects destructed at the end of the program?


Best

Kai-Uwe Bux
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top