DELPHI跨平台的临界替代者
在WINDOWS里面使用臨界來保護多線程需要訪問的共享對象,現在,DELPHI有了新的跨平臺臨界保護者--System.TMonitor
代碼演示如下:
FConnections := TObjectDictionary<TIdTCPConnection,TDSTCPChannel>.Create;
。。。
procedure TCMServerForm.CMServerTransportDisconnectEvent(Event: TDSTCPDisconnectEventObject);
var
Index: Integer;
begin
if (FConnections = nil) or (Event.Connection = nil) then
Exit;
//進入臨界保護
System.TMonitor.Enter(FConnections);
try
FConnections.Remove(TIdTCPConnection(Event.Connection));
TThread.Synchronize(nil, procedure
begin
//update the connection list box, removing the connection that was just closed
Index := ConnectionsList.Items.IndexOfObject(Event.Connection);
if Index > -1 then
begin
ConnectionsList.Items.Delete(Index);
if ConnectionsList.SelCount = 0 then
SessionIdList.ClearSelection;
end;
end);
finally
// 終止臨界保護
System.TMonitor.Exit(FConnections);
end;
end;
轉載于:https://www.cnblogs.com/hnxxcxg/p/5670707.html
總結
以上是生活随笔為你收集整理的DELPHI跨平台的临界替代者的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Matlab和C++混合编程
- 下一篇: HTML 4.01 规定了三种文档类型