CheckSynchronize实现的不必要的复杂
Posted 朝闻道
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CheckSynchronize实现的不必要的复杂相关的知识,希望对你有一定的参考价值。
在system.classes单元中,CheckSynchronize在ThreadLock上持续调用TMonitor的Exit和Enter以保护SyncList。
因为代码做的第一件事是换出SyncList,并且由于对SyncList的所有其他访问也受到ThreadLock上的TMonitor的保护,这只是不必要的代码,增加了锁争用的可能性。
TMonitor.Enter(ThreadLock);
try
Pointer(LocalSyncList) := AtomicExchange(Pointer(SyncList), Pointer(LocalSyncList));
try
Result := (LocalSyncList <> nil) and (LocalSyncList.Count > 0);
if Result then
begin
while LocalSyncList.Count > 0 do
begin
SyncProc := LocalSyncList[0];
LocalSyncList.Delete(0);
TMonitor.Exit(ThreadLock);
try
try
if Assigned(SyncProc.SyncRec.FMethod) then
SyncProc.SyncRec.FMethod()
else if Assigned(SyncProc.SyncRec.FProcedure) then
SyncProc.SyncRec.FProcedure();
except
if not SyncProc.Queued then
SyncProc.SyncRec.FSynchronizeException := AcquireExceptionObject
else if Assigned(ApplicationHandleException) then
ApplicationHandleException(SyncProc.SyncRec.FThread);
end;
finally
SyncProc.SyncRec.FThread := nil;
TMonitor.Enter(ThreadLock);
end;
if not SyncProc.Queued then
TMonitor.Pulse(SyncProc.Signal)
else
begin
Dispose(SyncProc.SyncRec);
Dispose(SyncProc);
end;
end;
end;
finally
LocalSyncList.Free;
end;
finally
TMonitor.Exit(ThreadLock);
end;
用下面代码替换:
TMonitor.Enter(ThreadLock);
try
Pointer(LocalSyncList) := AtomicExchange(Pointer(SyncList), Pointer(LocalSyncList));
finally
TMonitor.Exit(ThreadLock);
end;
try
Result := (LocalSyncList <> nil) and (LocalSyncList.Count > 0);
if Result then
begin
while LocalSyncList.Count > 0 do
begin
SyncProc := LocalSyncList[0];
LocalSyncList.Delete(0);
try
try
if Assigned(SyncProc.SyncRec.FMethod) then
SyncProc.SyncRec.FMethod()
else if Assigned(SyncProc.SyncRec.FProcedure) then
SyncProc.SyncRec.FProcedure();
except
if not SyncProc.Queued then
SyncProc.SyncRec.FSynchronizeException := AcquireExceptionObject
else if Assigned(ApplicationHandleException) then
ApplicationHandleException(SyncProc.SyncRec.FThread);
end;
finally
SyncProc.SyncRec.FThread := nil;
end;
if not SyncProc.Queued then
TMonitor.Pulse(SyncProc.Signal)
else
begin
Dispose(SyncProc.SyncRec);
Dispose(SyncProc);
end;
end;
end;
finally
LocalSyncList.Free;
end;
https://quality.embarcadero.com/browse/RSP-16353
http://blog.sina.com.cn/s/blog_44fa172f0102wcsp.html
以上是关于CheckSynchronize实现的不必要的复杂的主要内容,如果未能解决你的问题,请参考以下文章