jeudi 24 décembre 2015

C++11 Singleton. Static variable is thread safe? Why?

I just read, that this construct:

const bg::AppSettings& bg::AppSettings::GetInstance()
{
    static AppSettings instance;
    return instance;
}

is a thread safe and working way to create a singleton?! Am I correct, that the static AppSettings variable will be the same, every time I call this method?! I get a little bit confused about the scoping on this one ...

My normal approach was to use a unique_ptr as a static member of my class ... but this seems to work...can someone explain to me, what's going on here?!

And btw.: does the const make sense here?!

Aucun commentaire:

Enregistrer un commentaire