Possible Duplicate:
Double-checked locking in .net
EDIT: lots of edits to clarify this question is not about singleton
I find myself writing code like this:
if(resourceOnDiskNeedsUpdating)
{
lock(lockObject)
{
if(resourceOnDiskNeedsUpdating) // has a previous thread already done this?
UpdateResourceOnDisk();
}
}
return LoadResourceFromDisk();
UpdateResource()
is a slow operation.
Does this pattern make sense?
Are there better alternatives?