KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
If I have a deeply immutable type (all members are readonly and if they are reference type members, then they also refer to objects that are deeply immutable). I would like to implement a lazy initialized property on the type, like this: private ReadOnlyCollection<SomeImmutableType> m_PropName = null; public ReadOnlyCollection<SomeImmutableType> PropName { get { if(null == m_PropName) { ReadOnlyCollection<SomeImmutableType> temp = /* do lazy init */; m_PropName = temp; } return m_PropName; } } From what I can tell: m_PropName = temp; ...is threadsafe. I'm not worried too much about two threads both racing to initialize at the same time, because it will be rare, both results would be identical from a logical perspective, and I'd rather not use a lock if I don't have to. Will this work? What are the pros and cons? Edit: Thanks for your answers. I will probably move forward with using a lock. However, I'm surprised nobody brought up the possibility of the compiler realizing that the temp variable is unnecessary, and just assigning straight to m_PropName. If that were the case, then a reading thread could possibly read an object that hasn't finished being constructed. Does the compiler prevent such a situation? (Answers seem to indicate that the runtime won't allow this to happen.) Edit: So I've decided to go with an Interlocked CompareExchange method inspired by this article by Joe Duffy . Basically: private ReadOnlyCollection<SomeImmutableType> m_PropName = null; public ReadOnlyCollection<SomeImmutableType> PropName { get { if
Tags (comma-separated)
Save Edits
Cancel