multithreading - How to manage a dictionary of lazily created objects in a multithreaded environment in .NET? -


This question is in continuity. The deal is simple.

Gone:

  • Collection of lonely singletones.
  • Multi-specific environment.

Wanted:

  • An implementation with a small lock bar as much as possible when using a previously created item goes.

Consider the implementation of the good old C ++ style of a singleton, as an example of If-lock-if pattern: < / P>

  get some type of public static {get (if m_instance == empty) {lock (m_lock) {if (m_instance == zero) {m_instance = new type); }}} Return m_instance; Again, this is just an example of if-lock-if pattern.  

Obviously, there is no lock fines when reaching the already built object. Is it possible to prepare a collection of shameful items, when a particular object has already been made, in the same sense of keeping the penalty down?

Thank you.

Edit:

I have re-written this question completely, erase the word with eccentricity, because people get a lot of attention And do not have the origin of the question.

This is exactly the case that I still use hashtable on occasion. Hashtable multi-reader, single-author thread-safe, so that you can use it in double-check locking pattern as you would be a field:

  Some types of public stable example { {If (! M_ht.Contains (typeof (SomeType)) {lock (m_lock) {if (! M_ht.Contains (typeof (SomeType)) {m_ht [typeof (SomeType)] = new SomeType (); }}} Return (some type) m_ht [typeof (SomeType)]; }}  

Just do not even calculate all the keys or values.


Comments

Popular posts from this blog

asp.net - Javascript/DOM Why is does my form not support submit()? -

sockets - Delphi: TTcpServer, connection reset when reading -

javascript - Classic ASP "ExecuteGlobal" statement acting differently on two servers -