c# - Is it good to test a condition then lock then re-test the condition -


possible duplicate:
double-checked locking in .net

edit: lots of edits clarify question not singleton

i find myself writing code this:

    if(resourceondiskneedsupdating)     {         lock(lockobject)         {             if(resourceondiskneedsupdating) // has previous thread done this?                 updateresourceondisk();         }     }     return loadresourcefromdisk(); 

updateresource() slow operation. pattern make sense?
there better alternatives?

this called "double-checked locking".

you need memory fence make correct.

see wikipedia's article on double checked locking in .net


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -