J
JohnQ
SomeObject* instance() // flawed way
{
static SomeObject* instance = NULL;
LockThing(); // locking even when not required
if(instance == NULL) // instance may still be NULL when another thread
checks
instance = new SomeObject();
UnlockThing(); // oh dang, more unnecessary function calls after first
pass
return instance;
}
SomeObject* instance() // OK?
{
static bool been_there_done_that = LockThing();
static volatile instance = new SomeObject(); // no problemo (on X86
hardware)
static bool been_there_done_that_B = UnlockThing();
return instance; // fast is nice! No overhead after first pass through
function
}
John
{
static SomeObject* instance = NULL;
LockThing(); // locking even when not required
if(instance == NULL) // instance may still be NULL when another thread
checks
instance = new SomeObject();
UnlockThing(); // oh dang, more unnecessary function calls after first
pass
return instance;
}
SomeObject* instance() // OK?
{
static bool been_there_done_that = LockThing();
static volatile instance = new SomeObject(); // no problemo (on X86
hardware)
static bool been_there_done_that_B = UnlockThing();
return instance; // fast is nice! No overhead after first pass through
function
}
John