So i read a lot about why this implementation isn’t thread-safe. But I didn’t find the answer how to make it thread safe and fast? The variant of make it thread safe is to add mutex(or in some cases just critical section will be enough), but it will make this method much more slower. So is there a variant to make this code thread safe and fast, or at least now as slow as adding mutex there?
static Singleton& getInstance()
{
static Singleton singleton;
return singleton;
}
PS: and yes, I also read a lot about thread safe Singletom implementation when we use Singleton pointer as a member of the class, the question is about this particular implementation of Singleton, without pointers and new and using lazy evaluation.
So seems the answer to my question best expressed Fred Larson in his comment:
“Fast, thread-safe, lazy – pick any two.”