2

This is an interview question.

In C++, for singleton design pattern, is it possible that there may be multiple instances of the class? If yes, in what cases?

My idea:

In multithreading cases, without synchronous protections, it is possible that we may have multiple instances generated by multiple threads.

We can use a mutex to do the protection. For example, use a mutex to protect a global variable as counter to keep the numbers of instances to assure that there is only one instance.

Any better ideas?

Pubby
  • 51,882
  • 13
  • 139
  • 180
user1000107
  • 405
  • 7
  • 14

7 Answers7

3

In C++11 there is a new facility to make once-only initialization reliable - std::call_once. You can find a good reference for it here. If you're working with a compiler that supports C++11, this gives you a nice platform-independent, standard way of accomplishing it.

At the platform dependent level, most operating systems provide a means of doing once-only initialization. E.g. in iOS or OS X you can use dispatch_once from Grand Central Dispatch. On Windows, there's a One-Time Initialization API available in Vista and later.

Those are nice when you don't have a standard alternative, but thanks to C++11, you no longer need to do that sort of thing in a platform-dependent manner. It's perfect for singleton initialization.

aalpern
  • 652
  • 4
  • 8
  • thanks, what if multiple threads call std::call_once ? and, what if muy c++ version does not support C++11 ? thanks – user1000107 Feb 09 '12 at 18:47
  • @user1000107, i think the example linked by aalpern says it all? – lurscher Feb 09 '12 at 20:55
  • call_once() ensures that it's called only once, even when multiple threads enter the code. The pattern that call_once() uses is the same as what GCD uses; there's a "token" (`std::once_flag` in C++11, `dispatch_once_t` in GCD) which the implementation will use to record whether the code has been executed or not. You need to use a separate instance of `std::once_flag` per invocation of `std::call_once`. – aalpern Feb 09 '12 at 22:17
2

it might seem obvious, but if you have multiple processes, you can have multiole singleton instances. I'm more of a java guy tho, but if you have anything equivalent with ClassLoader's, then you can again have multiple singletons. Basically you can have as many singletons as entities controlling the instantiation (talking about factory pattern here)

vlad-ardelean
  • 7,480
  • 15
  • 80
  • 124
1

on OS X, static objects in different dynamic libraries will be different instances. On Windows and Linux they will behave as expected.

The only guaranteed way to have unique static objects across a process in different platforms is to use the static local variable idiom in exported functions, that is, instead of doing this:


foo.h

static Foo* global_instance;

foo.cpp

static Foo* global_instance = new Foo();

you need to do this:


foo.h

Foo* get_global_instance();

foo.cpp

Foo* get_global_instance() {
     static Foo instance;
     return &instance;
    }
lurscher
  • 25,930
  • 29
  • 122
  • 185
  • Foo* get_global_instance() { static Foo instance; return &instance; } should be Foo* get_global_instance() { if (instance == NULL) static Foo instance; return &instance; } – user1000107 Feb 09 '12 at 18:09
  • @user1000107, no my friend. static Foo instance is an initialization that only happens once, the first time the function is invoked – lurscher Feb 09 '12 at 18:25
  • @user1000107, aalpern' solution is the most general solution for your problem. – lurscher Feb 09 '12 at 18:27
0

The obvious answer: it depends on the implementation, but if the possibility exists for more than one instance, the implementation is broken. So the question is really because: what sort of errors can you make when implementing a singleton? (To which the answer is: pretty much the same errors you can make when implementing any pattern.)

James Kanze
  • 150,581
  • 18
  • 184
  • 329
0

Check also problems with double-checked locking singleton pattern. Issue descirbed in the Internet a lot of times. But... 99% of singletons I've seen in my life use this pattern :)

Kamil_H
  • 501
  • 1
  • 5
  • 12
0

OK, this is possible. On the interface you have singleton... but in the implementation chose to have multiple instances for whatever reason.. maybe pooling?

PermanentGuest
  • 5,213
  • 2
  • 27
  • 36
0

interview questions usually try to test your knowledge of a subject and usually have some trick to them. I think in this case the answer is this: if there is more that one instance to the class, then it isn't a singleton pattern.