码迷,mamicode.com
首页 > 其他好文 > 详细

CheckSynchronize实现的不必要的复杂

时间:2017-01-19 17:50:13      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:try   monit   ati   sign   lis   cal   blog   procedure   nbsp   

在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实现的不必要的复杂

标签:try   monit   ati   sign   lis   cal   blog   procedure   nbsp   

原文地址:http://www.cnblogs.com/findumars/p/6307385.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!