Original article: 2008-02-18
This article is Based on. NET 2.0
Readerwriterlock may be used in multi-threaded programming environments. Readerwriterlock's usage and how to use.
How readerwriterlock is implemented, or when different lock requests are met at the same time, and how readerwriterlock is handled, which is not mentioned in the above article may be a bit confusing for users. Compared with the Read and Write locks in the database, it is easy to understand. The following describes two threads.
There are four types of requests in the order of arrival
Reader-reader. The second reader directly obtains read control without waiting;
Reader-writer, the second must wait until the first call releasereaderlock () to release the read control before obtaining the write control;
Writer-Writer. The second one needs to wait for the first call releasewriterlock () to release write control before obtaining write control;
Writer-reader. The second one needs to wait for the first call releasewriterlock () to release write control before obtaining read control.
The actual situation may be more than two threads, for more information, please read this article http://www.rainsts.net/article.asp? Id = 171
If you think readerwriterlock is not easy to use, can you read this article http://www.cnsdn.com.cn/blog/article.asp? Id = 1972. The conversion from readerlock to writerlock (upgradetowriterlock) is not atomic, and an alternative lockfree method and its implementation are introduced. In addition, the "performance is very poor" mentioned in this article, which is not described in detail by the author; I use readerwriterlock less often and it is unclear whether it is really bad.