Threadsafe foreach enumeration of lists

后端 未结 11 1614

I need to enumerate though generic IList<> of objects. The contents of the list may change, as in being added or removed by other threads, and this will kill my enumerati

11条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-17 19:06

    Wrap the list in a locking object for reading and writing. You can even iterate with multiple readers at once if you have a suitable lock, that allows multiple concurrent readers but also a single writer (when there are no readers).

提交回复
热议问题