Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm not familiar with the <code>ConcurrentHashMap</code> you're talking about. Perhaps you could define what function it performs that you'd like to have in C# too.</p> <p>If you just want to guarantee that a collection is thread-safe for concurrent access then you're best off defining your own lock strategy to control access to it.</p> <p>The <code>Hashtable.Synchronised(...)</code> method seen in earlier versions of the .NET framework has been removed (along with the hiding of all ICollection.SyncRoot implementations via explicit implementations in <code>System.Collections.Generic</code>). This is because whenever you lock on a publicly accessible object, you're opening up the possibility for other callers to lock on your objects in ways that you can't control, and therefore deadlocks become possible.</p> <p>Also, a 'synchronised' collection can only guarantee that calls to individual members are safe, however many scenarios involve 'transactions' including many members. Consider the following example:</p> <pre><code>if (collection.Contains(item)) collection.Get(item); </code></pre> <p>The <code>map</code> object (whatever type it is) might guarantee that both the <code>Contains</code> and <code>Get</code> methods are thread-safe, but it can't guarantee that the item is still contained in the map by the time <code>Get</code> is called. Instead you should use code like this:</p> <pre><code>private readonly object _mapLock = new object(); public void Method() { lock (_mapLock) { if (collection.Contains(item)) collection.Get(item); } } </code></pre> <p>@Oskar suggests using <code>ReaderWriterLock</code>. This class has a terrible implementation with worse performance than simple locking. If you're using newer versions of the framework, use <code>ReaderWriterLockSlim</code> instead.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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