Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I <a href="http://philosopherdeveloper.com/posts/how-to-build-a-thread-safe-lock-free-resizable-array.html" rel="noreferrer">gave it a try a while back</a> (also: <a href="https://github.com/dtao/ConcurrentList" rel="noreferrer">on GitHub</a>). My implementation had some problems, which I won't get into here. Let me tell you, more importantly, what I learned.</p> <p>Firstly, there's no way you're going to get a full implementation of <code>IList&lt;T&gt;</code> that is lockless and thread-safe. In particular, random insertions and removals are <em>not</em> going to work, unless you also forget about O(1) random access (i.e., unless you "cheat" and just use some sort of linked list and let the indexing suck).</p> <p>What I <em>thought</em> might be worthwhile was a thread-safe, limited subset of <code>IList&lt;T&gt;</code>: in particular, one that would allow an <code>Add</code> and provide random <em>read-only</em> access by index (but no <code>Insert</code>, <code>RemoveAt</code>, etc., and also no random <em>write</em> access).</p> <p>This was the goal of <a href="http://philosopherdeveloper.com/posts/boy-can-dream.html" rel="noreferrer">my <code>ConcurrentList&lt;T&gt;</code> implementation</a>. But when I tested its performance in multithreaded scenarios, I found that <strong>simply synchronizing adds to a <code>List&lt;T&gt;</code> was faster</strong>. Basically, adding to a <code>List&lt;T&gt;</code> is lightning fast already; the complexity of the computational steps involved is miniscule (increment an index and assign to an element in an array; that's <em>really it</em>). You would need a <em>ton</em> of concurrent writes to see any sort of lock contention on this; and even then, the average performance of each write would still beat out the more expensive albeit lockless implementation in <code>ConcurrentList&lt;T&gt;</code>.</p> <p>In the relatively rare event that the list's internal array needs to resize itself, you do pay a small cost. So ultimately I concluded that this was the <em>one</em> niche scenario where an add-only <code>ConcurrentList&lt;T&gt;</code> collection type would make sense: when you want <em>guaranteed</em> low overhead of adding an element on <em>every single call</em> (so, as opposed to an amortized performance goal).</p> <p>It's simply not nearly as useful a class as you would think.</p>
    singulars
    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. 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