Multi-threaded read and write isolation

In a multi-threaded application. I have a bunch of functions that go through a collection to read information. I also have a bunch of functions that modify the same collection.

I am looking for a way to isolate everything I read and write together. I do not want the recording to be done while reading. I was thinking about using SyncLock in a collection object, but this blocks multiple reads trying to work in parallel.

+3
source share
1 answer

Did you consider ReaderWriterLockSlim ? You might want to check that using this is faster than simply locking the monitor . Do not read the old ReaderWriterLock. Here's what Jeffrey Richter has to say about it:

Class Microsoft® .NET Framework Library includes a ReaderWriterLock class in the System.Threading namespace, which allows you to get multiple read / with one writer semantics. Although it is good that such a class exists, there are several problems with it and I recommend that you not use it.

+3
source

Source: https://habr.com/ru/post/1735945/


All Articles