When is ReaderWriterLockSlim better than a simple lock?

后端 未结 10 1910
孤街浪徒
孤街浪徒 2020-11-29 16:16

I\'m doing a very silly benchmark on the ReaderWriterLock with this code, where reading happens 4x more often than writting:

class Program
{
    static void          


        
10条回答
  •  感情败类
    2020-11-29 16:57

    There's no contention in this program. The Get and Add methods execute in a few nanoseconds. The odds that multiple threads hit those methods at the exact time are vanishingly small.

    Put a Thread.Sleep(1) call in them and remove the sleep from the threads to see the difference.

提交回复
热议问题