Change 2 . Just removing ReadThread calls from ReadThread and WriteThread , I saw Locked outperform RWLocked . I suppose Hans hit a nail on the head; your methods are too fast and do not create competition. When I added Thread.Sleep(1) to the Get and Add Locked and RWLocked (and used 4 read streams versus 1 write stream), RWLocked beat Locked pants.
Edit : Well, if I really thought when I first posted this answer, I would understand at least why you placed Thread.Sleep calls there: you tried to play the read script more often than it writes. This is not the best way to do this. Instead, I would add extra overhead to your Add and Get methods to create a greater chance of contention (as Hans suggested ), create more read threads than write (to provide more frequent reads than writes), and delete Thread.Sleep calls Thread.Sleep from ReadThread and WriteThread (which actually reduce the conflict by reaching the opposite of what you want).
I like what you have done so far. But here are a few questions that I see right off the bat:
- Why call
Thread.Sleep ? This simply accumulates the execution time by a constant amount, which artificially leads to a convergence of work results. - I will also not include the creation of new
Thread objects in the code measured by your Stopwatch . This is not a trivial object to create.
If you notice a significant difference, if you consider the two questions above, I do not know. But I believe that they should be considered before the discussion continues.
Dan Tao Nov 18 '10 at 17:01 2010-11-18 17:01
source share