I implemented a Meyers Singleton, then realized it could be vulnerable to the destructor fiasco problem.
As a result, I changed the code to be:
Instance *getInstance()
{
static Instance* singleton = new Instance();
return singleton;
}
After implementing this, and no apparent bugs occuring, a coworker was implementing a different singleton and used std::call_once
instead.
I've now realized that after much searching, I couldn't find if the "Leaky Meyers Singleton" is a threadsafe pattern. Should the leaky singleton be changed to std::call_once
? Or is it threadsafe as-is?
Is the pointer considered a "block-scope" variable? If so I think it would be thread-safe, but if not there's significant bugs introduced with the current leaky singleton approach.
Yes, this is thread-safe: no two threads will ever try to initialize the same variable with static storage duration at the same time. That includes the entirety of evaluating the initializer.