Well, based on the text, I think there was a mistake... >> Alternatively, it might be better to use more elaborate code to >> create rhs outside of the lock, as in the following example. Then, >> you can use an interlocked compare exchange to update x only if it >> is still null. This assumes that creation of duplicate rhs values >> does not cause negative side effects. >> >> if (x == null) >> { >> lock (this) >> { >> if (x == null) >> { >> // Perform some elaborate code to create rhs. >> x = rhs; >> } >> } >> }
However, it does make sense with a slight alteration -- and one that would be almost unnoticed due to positional similarities: >> if (x == null) >> { //Perform the rhs calculations here. >> lock (this) >> { >> if (x == null) >> { >> x = rhs; >> } >> } >> } In other words, after the first x == null check, not the second. You can read messages from the DOTNET archive, unsubscribe from DOTNET, or subscribe to other DevelopMentor lists at http://discuss.develop.com.