Concurrency

  • AVOID ConcurrentBag<T> without benchmarking.

    This collection has been designed for very specific use-cases (when most of the time an item is dequeued by the thread that enqueued it) and suffers from important performance issues if used otherwise. If in need of a concurrent collection, prefer ConcurrentQueue<T>.

  • AVOID ReaderWriterLock<T> / ReaderWriterLockSlim<T> without benchmarking.

    While it may be tempting to use this kind of specialized synchronization primitive when dealing with readers and writers, its cost is much higher than a simple Monitor (usable with the lock keyword). Unless the number of readers executing the critical section at the same time is very high, the concurrency won’t be enough to amortize the increased overhead, and the code will perform worse.

Learn More