Re: Singleton --- Just Look and give Suggestion's
On Mar 6, 8:41 am, anon <sfdd...@ebay.de> wrote:
Vaclav Haisman wrote:
I suggest you do not use the singleton pattern at all.
Not knowing what problem he's trying to solve, I wouldn't go
that far. I use it from time to time.
[...]
Singleton is glorified memory leak. Your A's dtor will never
be called, bad.
It will be called at the process end
In his code, it will be called three times at the end of main.
On the same object. In a correctly implemented singleton, it
may or may not be called, depending on the implementation. Most
of the time, it is preferable not to call it.
You cannot use your singleton in other code that runs during
global/static variables initialization, if you intend to
have global/static mutex to synchronize the singleton
instance creation. That limits its use.
You could use a statically initialized mutex, at least under
Unix. I', not sure how Windows programmers handle this problem,
but Boost threads has once, so there must be some solution.
Normally, the simplest solution is just to ensure that the
instance() function is called at least once before threading
start.
Using template like that or anything similar also makes your
singletons not really having single instance when DLLs on
Windows.
I agree - windows is shit.
It has nothing to do with Windows. You can ensure that there is
only a single instance in Windows, if that's what you need, or
you can end up with multiple instances in Unix, if that's what
you need (e.g. a plugin, where each plugin really operates in
its own environment).
Bottom line, singleton is bad idea. Properly managed life
time of globally visible pointer to A is much much better.
Then why are singleton questions appearing in this NG all the
time? It's uses are limited, but it is a very good idea
It's a tool. Sometimes useful, sometimes abused. The reason
questions about it appear so often is probably because it is the
simplest pattern in the GoF book, so beginners start with it.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34