Seems like thread-safety could be enforced by a simple spinlock during object construction.
Code:
void SpinLock(int &lock);
void SpinUnlock(int &lock);
class TheSingleton
{
static int mSpinLock;
static bool mInstantiated;
public:
static void DoInstantiate();
static void Instantiate()
{
SpinLock(mSpinLock);
if(!mInstantiated)
{
DoInstantiate();
mInstantiated = true;
}
SpinUnlock(mSpinLock);
};
int TheSingleton::mSpinLock = 0;
bool TheSingleton::mInstantiated = false;
Sure, if two threads try to instantiate at the same time, one of them will spin for a while -- but it will only happen once, and ONLY if two threads try to instantiate at the same time.
There are no blocking waits, no OS dependencies.