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

  • 相关阅读:
    Oracle求连续的年份
    关于Extjs MVC模式上传文件的简单方式
    后进先出 stack、 先进先出Queue
    python的文件操作
    C#的接口
    C#的访问级别
    C#的继承
    SQL查询语句 常用示例
    SQL Server数据库常用函数
    数据库建立和连接
  • 原文地址:https://www.cnblogs.com/findumars/p/6307385.html
Copyright © 2011-2022 走看看