TMREWSync (TMultiReadExclusiveWriteSynchronizer) 问题

TMREWSync (TMultiReadExclusiveWriteSynchronizer) questions

我对 TMREWSync 有疑问:

  1. 是否可以在不解锁的情况下将读锁升级为写锁?

  2. 不明白如果 BeginWrite returns false 我该怎么办?

  1. 是的,TMREWSync支持锁升级。像这样使用它:BeginRead ... BeginWrite ... EndWrite ... EndRead.

  2. System.SysUtils 中的源代码状态:

The function result of BeginWrite indicates whether another thread got the write lock while the current thread was waiting for the write lock. Return value of True means that the write lock was acquired without any intervening modifications by other threads. Return value of False means another thread got the write lock while you were waiting, so the resource protected by the MREWS object should be considered modified. Any samples of the protected resource should be discarded.

我的建议是您编写的算法不依赖于此 return 值。源代码同意我的观点:

In general, it's better to just always reacquire samples of the protected resource after obtaining a write lock. The boolean result of BeginWrite and the RevisionLevel property help cases where reacquiring the samples is computationally expensive or time consuming.