Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to enumerate list items randomly in C#?
    primarykey
    data
    text
    <p>I have a challenging question for you big experts. This has not yet a practical usage in my code, but comes from an idea I just had.</p> <p>If I have an <code>IList&lt;T&gt;</code>, how do I implement an enumerator that randomly walks the list and that can be used by multiple threads simultaneously?</p> <p>For example, if I have elements <code>A</code>, <code>B</code>, <code>C</code>, <code>D</code>, <code>E</code>, <code>F</code> and two concurrent threads performing a <code>for-each</code> loop on the list with a ReaderLock acquired (so I'm sure nobody else will touch the list thus causing an exception), I would like their respective cycles to return, for example, <code>B</code>, <code>E</code>, <code>C</code>, <code>D</code>, <code>A</code>, <code>F</code> and <code>E</code>, <code>B</code>, <code>D</code>, <code>C</code>, <code>A</code>, <code>F</code>.</p> <p>The reason why I need this is because I need to place locks on <code>List&lt;SslStream&gt;</code> elements to send data to clients, <a href="http://msdn.microsoft.com/en-us/library/system.net.security.sslstream.aspx" rel="nofollow">because SslStream is not thread-safe</a>. Picking elements randomly (but making sure I pick <strong>them all</strong>) reduces the lock conflict probabilities and is supposed to improve I/O-bound operations performance.</p> <p>Please keep in mind that even if I told you <strong>why</strong> I need such an enumerator, I still like challenge. There can be other ways of sending the same data to multiple clients, but my question remains the same :) :)</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.
 

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