12

Google results say there are more than 1 singleton template/baseclass in boost, which one do you suggest?

Chris Martin
  • 28,558
  • 6
  • 66
  • 126
James Bond
  • 6,039
  • 14
  • 45
  • 58
  • 11
    Singletons are evil. They are just global variables in a politically correct disguise! – Tronic Mar 11 '10 at 15:36
  • To give that a background, jalf put it well e.g. here: http://stackoverflow.com/questions/86582/singleton-how-should-it-be-used/761711#761711 (i think he elaborated more somewhere else, but can't find it now). – Georg Fritzsche Mar 11 '10 at 15:53
  • 2
    @gf: I think I've made that point in at least a dozen different questions by now. ;) here ( http://stackoverflow.com/questions/1020312/are-singletons-really-that-bad/1020384#1020384 ) is another one. And apparently I converted GMan to my point of view as well, according to his blog: ( http://blackninjagames.com/?p=24 ), but can't find the thread it's based on right now. :) So yeah, while it might not answer the OP's question, I'd say "don't use any of the singleton classes in boost". :) – jalf Mar 11 '10 at 17:47
  • 1
    @gf @jalf: It was this one: http://stackoverflow.com/questions/2080233/is-it-good-programming-to-have-lots-of-singleton-classes-in-project/2080242#2080242 – GManNickG Mar 11 '10 at 18:40
  • 1
    @jalf: Maybe you could elaborate a bit on it in a blog post that could be referred to? :) @GMan: Ah, thanks. – Georg Fritzsche Mar 11 '10 at 19:28
  • If singletons are evil, is DI a bless? :)) – mlvljr Mar 11 '10 at 19:28
  • Factories as Singletons? Does a program ever need more than one instance of a factory? A factory is a global object. I use the Singleton for factories and *managers* (objects that manage things). – Thomas Matthews Mar 11 '10 at 20:01
  • 1
    A factory can also be local (and dep-injected).. – mlvljr Mar 11 '10 at 20:02
  • 1
    @Thomas: If you only need one, then only use one. There's no reason you must prevent further instantiations, except to obfuscate code. – GManNickG Mar 11 '10 at 21:18
  • 1
    @Thomas: How do you *know* that your program won't ever need more than one factory instance? What if you're wrong? If it's not a singleton, I can create one instance, and if it turns out I was wrong and I needed two, I can create another. With a singleton, I have one instance, and if I find out I needed two, I'm screwed. Why would you hardcode the class to prevent yourself from creating multiple instances? Does it prevent errors? Do you often *accidentally* create multiple factories? – jalf Mar 12 '10 at 00:01
  • @mlvljr: I'm not much into anthropomorphizing my code. I don't like to think of code constructs as "evil" or "good" or "blessed" (or as "smelly", for that matter). Singletons are *almost always* the wrong solution to a problem you never actually had in the first place. I won't rule out that a case exists where it's the right tool, but I can't think of what it might be. As for DI, I'm not a huge fan of that either. It's convenient, but it's also a good way to lose track of your dependencies, and to weaken class invariants. I'm on the fence when it comes to DI. Useful, but hardly "a blessing" :) – jalf Mar 12 '10 at 00:06
  • 2
    ..beware, bad smelly singletons, an evangelist comes! :)) [I do not have any specific person in mind here, just joking] – mlvljr Mar 12 '10 at 16:46
  • 1
    @gf: Your wish is my command: http://jalf.dk/blog/2010/03/singletons-solving-problems-you-didnt-know-you-never-had-since-1995/ ;) – jalf Mar 13 '10 at 00:49
  • @jalf: I have a 3rd party device API that requires all calls made in a the same thead, which I need to use concurrently in a multithreaded application. Solution: use cross-thread calls and run all device interaction in a dedicated thread. My thread *needs* to be a singleton, or my app burns. – peterchen Jul 27 '11 at 13:35
  • @peterchen: that doesn't make sense. How can a thread be a singleton? Wrapping that API in a something that marshals all calls to a single API thread makes sense. But there is zero reason why it has to be done in a singleton. – jalf Jul 27 '11 at 13:57
  • @jalf: Sorry, I could have made that clearer: The data associated with the thread, at the very least the thread *handle*, or any data structure you use to figure out the thread is already there and how to communicate with it. – peterchen Jul 27 '11 at 15:13

2 Answers2

8

You shouldn't use the singletons in boost, they are for internal purpose only (see the "detail" folders of separate libes). That's why you don't have a Singleton library (yet) exposed on the boost website.

A singleton class is very simple to implement but there are many variants that are useful in specific cases so you should use something that fit what you think a singleton should behave like.

Now, there is other libraries providing singleton, the most generic being Loki. But it could blow your mind ;)


Update : There is now a proposed library called Singularity that is meant to provide non-global singleton (with option to make it global) that forces you to have clear creation and destruction points of the object.

See the review request : http://boost.2283326.n4.nabble.com/Review-Request-Singularity-tt3759486.html

Some boost devs seems to consider using it instead of some hacks, but C++11 makes makeing a class Singleton easier than before so it will depends on the review.

Klaim
  • 60,771
  • 31
  • 121
  • 186
1

My version of boost has following singleton.hpp headers:

C:\boost_1_38_0\boost\pool\detail\singleton.hpp
C:\boost_1_38_0\boost\serialization\singleton.hpp
C:\boost_1_38_0\boost\thread\detail\singleton.hpp

I haven't used any of those, but I'd probably stay away from the ones in detail directories.

Anyway, http://torjo.com/tobias/index.html#boost_utility_singleton.reference.singleton looks like one to use, but it doesn't seem to be really a part of boost (not accepted yet?).

UncleBens
  • 38,655
  • 6
  • 51
  • 88