Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>On problem with <code>ConcurrentBag</code> is that it is unordered so you cannot pull items out by index the same way you are doing it currently. However, you can iterate it via <code>foreach</code> to get the same effect. This iteration will be thread-safe. It will not go bizerk if an item is added or removed while the iteration is happening.</p> <p>There is another problem with <code>ConcurrentBag</code> though. It actually copies the contents to a new <code>List</code> internally to make the enumerator work correctly. So even if you wanted to just pick off a single item via the enumerator it would still be a O(n) operation because of the way enumerator works. You can verify this by disassembling it.</p> <p>However, based on context clues from your update I assume that this collection is going to be small. It appears that there is only one entry per "game client" which means it is probably going to store a small number of items right? If that is correct then the performance of the <code>GetEnumerator</code> method will be mostly insignificant.</p> <p>You should also consider <code>ConcurrentDictionary</code> as well. I noticed that you are trying to match items from the collection based on <code>zoneId</code>. If you store the items in the <code>ConcurrentDictionary</code> keyed by <code>zoneId</code> then you would not need to iterate the collection at all. Of course, this assumes that there is only one entry per <code>zoneId</code> which may not be the case.</p> <p>You mentioned that you did not want to use "local lists/copies", but you never said why. I think you should reconsider this for the following reasons.</p> <ul> <li>Iterations could be lock-free.</li> <li>Adding and removing items appears to be infrequent based context clues from your code.</li> </ul> <p>There are a couple of patterns you can use to make the list copying strategy work really well. I talk about them in my answers <a href="https://stackoverflow.com/a/10557130/158779">here</a> and <a href="https://stackoverflow.com/a/10018399/158779">here</a>.</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