zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    CNN网络架构演进:从LeNet到DenseNet
    Dropout VS Batch Normalization
    Batch Normalization
    达梦数据库序列号升级的办法
    [Function Programming] Function modelling -- 9. Monad Transformers
    [Functional Programming] Function modelling -- 8. Compose Functors
    [Javascript] HTML5 DOM project
    [ML L3] SVM Intro
    博客粘贴图片自动上传到服务器(Java版)
    wordpress粘贴图片自动上传到服务器(Java版)
  • 原文地址:https://www.cnblogs.com/findumars/p/6307385.html
Copyright © 2011-2022 走看看