Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to manage a dictionary of lazily created objects in a multithreaded environment in .NET?
    text
    copied!<p>This question is in continuation of <a href="https://stackoverflow.com/questions/1593628/accessing-net-dictionary-in-a-multithreaded-environment">this one</a>. The deal is simple.</p> <p><strong>Given:</strong></p> <ul> <li>A collection of lazily created singletons.</li> <li>Multithreaded environment.</li> </ul> <p><strong>Wanted:</strong></p> <ul> <li>An implementation with as little lock penalty as possible when an already created object is accessed. It is OK to pay higher penalty on removal or lazy initialization of a new object.</li> </ul> <p>Let us consider the good old <strong>C++</strong> style implementation of a singleton, used as an illustration of the if-lock-if pattern:</p> <pre><code>public static SomeType Instance { get { if (m_instance == null) { lock(m_lock) { if (m_instance == null) { m_instance = new SomeType(); } } } return m_instance; } } </code></pre> <p>Again, this is just an illustration of the if-lock-if pattern.</p> <p>Clearly, there is no lock penalty at all when accessing an already constructed object. Is it possible to devise a collection of lazily created objects in the same spirit of keeping the penalty minimal when the particular object is already created? It is fine to pay higher penalty when the object is removed.</p> <p>Thanks.</p> <p><strong>EDIT:</strong></p> <p>I have rewritten the question to completely erase the word singleton from it, since people tend to attribute too much attention to it and not to the core of the question.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload