TMultiReadExclusiveWriteSynchronizer-再了解
System.SysUtils 中
{ TMultiReadExclusiveWriteSynchronizer minimizes thread serialization to gain read access to a resource shared among threads while still providing complete exclusivity to callers needing write access to the shared resource. (multithread shared reads, single thread exclusive write) Read locks are allowed while owning a write lock. Read locks can be promoted to write locks within the same thread. (BeginRead, BeginWrite, EndWrite, EndRead)
TMultiReadExclusiveWriteSynchronizer最小化线程串行化来获得 读访问权在线程之间共享资源,同时仍然提供完整 排他性调用者需要写访问共享资源。 (多流共享读取、单线程独家编写) 读锁允许同时拥有一个写锁。 读锁可以晋升为写锁在同一线程。 (BeginWrite BeginRead EndWrite EndRead)
Note: Other threads have an opportunity to modify the protected resource when you call BeginWrite before you are granted the write lock, even if you already have a read lock open. Best policy is not to retain any info about the protected resource (such as count or size) across a write lock. Always reacquire samples of the protected resource after acquiring or releasing a write lock.
注:其他线程有机会修改受保护的资源 当你叫BeginWrite之前授予写锁,甚至 如果你已经有一个读锁打开。最好的政策是不保留 任何信息受保护的资源(如数量或大小) 写锁。总是重新获取样本后的受保护的资源 获取或释放一个写锁。
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. In general, it's better to just always reacquire samples of the protected resource after obtaining a write lock.
BeginWrite 的函数结果表明是否另一个线程 写锁定在当前线程等待写锁。 真正的返回值意味着写锁了 任何干预由其他线程修改。错误的返回值 意味着另一个线程写锁了你等待的时候,所以 资源保护MREWS对象应考虑修改。 任何样品的受保护的资源应该被丢弃。 一般来说,最好是总是重新获取样品的保护 资源在获得一个写锁。
The boolean result of BeginWrite and the RevisionLevel property help cases where reacquiring the samples is computationally expensive or time consuming. RevisionLevel changes each time a write lock is granted. You can test RevisionLevel for equality with a previously sampled value of the property to determine if a write lock has been granted, implying that the protected resource may be changed from its state when the original RevisionLevel value was sampled.
布尔BeginWrite的结果 和RevisionLevel属性帮助重获样品的情况 计算昂贵或耗费时间。 RevisionLevel改变每次写锁。您可以测试 RevisionLevel平等之前采样值的属性 确定一个写锁已经被授予,这意味着受保护 资源可能会改变原始RevisionLevel时其状态 采样值。
Do not rely on the sequentiality of the current RevisionLevel implementation (it will wrap around to zero when it tops out). Do not perform greater than / less than comparisons on RevisionLevel values. RevisionLevel indicates only the stability of the protected resource since your original sample. It should not be used to calculate how many revisions have been made.
不依赖于当前的序列性 RevisionLevel实现(它将环绕零上衣时)。 不执行大于或小于比较RevisionLevel值。 RevisionLevel表明只有稳定的受保护的资源 你的原始样品。它不应该被用来计算有多少 修正了。
type TMultiReadExclusiveWriteSynchronizer = class(TInterfacedObject, IReadWriteSync) private FSentinel: Integer; FReadSignal: THandle; FWriteSignal: THandle; FWaitRecycle: Cardinal; FWriteRecursionCount: Cardinal; tls: TThreadLocalCounter; FWriterID: Cardinal; FRevisionLevel: Cardinal; procedure BlockReaders; procedure UnblockReaders; procedure UnblockOneWriter; procedure WaitForReadSignal; procedure WaitForWriteSignal; {$IFDEF DEBUG_MREWS} procedure Debug(const Msg: string); {$ENDIF} public constructor Create; destructor Destroy; override; procedure BeginRead; procedure EndRead; function BeginWrite: Boolean; procedure EndWrite; property RevisionLevel: Cardinal read FRevisionLevel; end; {$ELSE} type TMultiReadExclusiveWriteSynchronizer = TSimpleRWSync; {$ENDIF}