views:

47

answers:

1

Hi

In a multithreaded application. I have a bunch of function that loop through a collection to read the information. I also have a bunch of function that modifies that same collection.

I’m looking for a way to isolate all the read and the write together. I don’t want a write to be done while a read is in progress. I was thinking of using SyncLock on the collection object but this will block multiple read trying to work in parallel.

+3  A: 

Have you considered ReaderWriterLockSlim? You might want to check that using it is faster than a simple Monitor lock though. Do not consider the older ReaderWriterLock. Here's what Jeffrey Richter has to say about it:

The Microsoft® .NET Framework Class Library includes a ReaderWriterLock class in the System.Threading namespace that lets you obtain multiple-reader/single-writer semantics. While it is nice that a class like this exists, there are several problems with its implementation and I recommend you do not use it.

RichardOD