A real threaded singleton
In the stuff I've read about multi threaded singleton it seems to
assume that creating the Lock is atomic, and various sources,
including Sutter go for RAII.
{
Lock l;
if ( ! inst )....
}
But I don't see how to do this with a real O/S API.
The ones I've looked at have a two step process to making a lock, they
require a "Create" function and a "Start Locking" function.
A Lock class is presumably implemented as something like:
class Lock
{
private :
PrimitiveLockType PrimitiveLock;
PrimitiveHandle handle;
public:
Lock() {
PrimitiveLock = CreatePrimitiveLock():
handle = StartLocking (PrimitiveLock);
}
~Lock()
{
StopLocking(handle);
KillPrimitiveLock(PrimitiveLock);
}
};
Now if I have two threads there is a chance (albeit a small one) that
I end up creating two PrimitiveLocks
Thus far, I've got around it by making the PrimitiveLock a global
static like the Instance member.
That makes error handling awkward, though if this is failing at such
an early stage then the program's not likely to work anyway.
What am I missing ?
}