Boost logo

Boost :

Subject: Re: [boost] Need second opions from some threading / objectcreation experts...
From: Joel falcou (joel.falcou_at_[hidden])
Date: 2011-05-15 11:52:36


> Sigh... yes that's why we *still* don't have a singleton in Boost.

And somehow, I like we dont have any public singleton class. I know what
happens when you give singleton to people: they use it everywhere ;)

> That was what I was hinting at - I had assumed that use of
> Boost.Thread's call_once was the only way to do this right, and then I
> saw the code and wasn't so sure.
>
> The thing I'm concerned about is this:
>
> * The code currently relies on a call to a function in a static object
> to force that object to be instantiated and initialize the singleton, but:
> * Once compiler optimizations are turned on, that function call will be
> optimized away to a no-op.
> * There's now no code that's using the global object.
> * A clever linker says "hey we don't need this anymore" and removes it
> from the program image, so:
> * The singleton no longer gets initialized before main starts (only when
> it's first called).
>
> Of course this optimization changes program behavior so it ought to be
> forbidden... but you know I'm paranoid ;-)

Being paranoid here is actually the sane thing.

I think having a pointer based singleton with compare-and-swap
initailisation is probably better. IIRC there is such a beast in Modern
C++ design and Loki but I dunno how OK it is.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk