C# ReaderWriteLock 中可升级读锁与写锁的区别 [英] difference of upgradeable read lock vs a write lock in C# ReaderWriteLock

查看:105
本文介绍了C# ReaderWriteLock 中可升级读锁与写锁的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ReaderWriterLock 类中的可升级读锁和写锁有什么区别?

Whats the difference between an upgradeable read lock and a write lock in the ReaderWriterLock class?

根据 MSDN 的描述,只有一个线程可以进入可升级的读锁.如果我正确地遵循,我可以忘记升级它,因为它与写锁相同.我有什么遗漏吗?

Based on the MSDN description, only one thread can enter an upgradeable read lock. If I follow correctly, I could just forget about upgrading it since its the same as a write lock. Anything I'm missing?

推荐答案

(我假设你的意思是 ReaderWriterLockSlim,因为我不认为 ReaderWriterLock 有一个可升级的阅读模式.)

(I'm assuming you mean ReaderWriterLockSlim, as I don't think ReaderWriterLock has an upgradeable read mode.)

虽然只有一个线程可以进入可升级的读锁,但一个线程可以有一个可升级的读锁其他线程仍然可以进入读锁:

Although only one thread can enter an upgradeable read lock, one thread can have an upgradeable read lock and other threads can still enter a read lock:

在任何给定时间只有一个线程可以进入可升级模式.如果一个线程处于可升级模式,并且没有线程等待进入写入模式,那么任意数量的其他线程都可以进入读取模式,即使有线程等待进入可升级模式.

Only one thread can enter upgradeable mode at any given time. If a thread is in upgradeable mode, and there are no threads waiting to enter write mode, any number of other threads can enter read mode, even if there are threads waiting to enter upgradeable mode.

将其与写锁进行比较:

当有线程等待进入写入模式时,其他尝试进入读取模式或可升级模式的线程会阻塞,直到所有等待进入写入模式的线程超时或进入写入模式然后退出.

When there are threads waiting to enter write mode, additional threads that try to enter read mode or upgradeable mode block until all the threads waiting to enter write mode have either timed out or entered write mode and then exited from it.

换句话说,写锁是完全排他的,而可升级的读锁将允许其他线程继续进入/退出读模式直到需要升级.

In other words, a write lock is entirely exclusive, whereas an upgradeable read lock will allow other threads to keep entering/exiting for read mode until the upgrade is needed.

这篇关于C# ReaderWriteLock 中可升级读锁与写锁的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆