开发者

Isolating read and write in multithreaded

开发者 https://www.devze.com 2022-12-22 01:52 出处:网络
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.

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 S开发者_运维问答yncLock on the collection object but this will block multiple read trying to work in parallel.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消