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.
If I have an IList<T>, how do I implement an enumerator that randomly walks the list and that can be used by multiple threads simultaneously?
For example, if I have elements A, B, C, D, E, F and two concurrent threads performing a for-each 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, B, E, C, D, A, F and E, B, D, C, A, F.
The reason why I need this is because I need to place locks on List<SslStream> elements to send data to clients, because SslStream is not thread-safe. Picking elements randomly (but making sure I pick them all) reduces the lock conflict probabilities and is supposed to improve I/O-bound operations performance.
Please keep in mind that even if I told you why 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 🙂 🙂
Create an array of the same size as your list, initially populate it as a[i] = i and then shuffle using a Fisher Yates algorithm.
Your enumerator can then iterate over this array, returning elements from your source list at the random index provided.