简体   繁体   English

在模拟器中计数连接和命令(Delphi 7 / Windows XP)

[英]Counting connections and commands in a simulator (Delphi 7/Windows XP)

I was working in a simulator for testing connections and commands sending to a server. 我在模拟器中进行测试,以测试发送到服务器的连接和命令。 The simulator has some counters like Total of sent commands, successfully sent commands, fail sent commands, connection attempts, successful connections, etc... 模拟器具有一些计数器,例如已发送命令总数,已成功发送命令,已失败发送命令,连接尝试,成功连接等。

The code that I used is the following: 我使用的代码如下:

procedure TALClient.SendCommand;
begin
  Try
    dlgMain.IncrementIntConx; //Increments conn attemps
    FTCP.Connect(1000);
    If FTCP.Connected Then
      Begin
        dlgMain.IncrementConections;  //increments successfully connections

        try
          dlgMain.IncrementIntSendCommand;  //Increments command sent attemps (A)
          FTCP.SendCmd(FCmd.FNemo + ' ' + FCmd.FParams);  // (Z)
          dlgMain.IncrementSendComm;  //Increments sent Commands (B)

          try
            FParent.CS.Acquire;
            FParent.FStatistic[Tag, FCmd.FTag].LastCodeResult := FTCP.LastCmdResult.NumericCode;
            FParent.FStatistic[Tag, FCmd.FTag].LastMsgResult := FTCP.LastCmdResult.Text.Text;
            FParent.CS.Release;
            if ((FTCP.LastCmdResult.NumericCode) = (497)) then
              Synchronize(UpdateCorrectCounters)  //increments successfully responds from server
            else
              Synchronize(UpdateErrorCounters);  //increments failed responds from server
          except
            Synchronize(UpdateErrorCounters);
          end;

        except
          dlgMain.IncrementFailCommand; //increments failed commands (C)
        end;
      End
    Else
      Synchronize(UpdateErrorCounters); //Increment failed responses from sever
  Finally
    If FTCP.Connected Then
      FTCP.Disconnect;
  End
end;

I have changed the code to many many other ways, but it never works fine. 我已将代码更改为许多其他方式,但始终无法正常工作。 The big problem is that the total count of sent commands is not equal to successfully sent commands plus failed sent commands. 最大的问题是已发送命令的总数不等于成功发送的命令加上失败发送的命令。 (in the code: A is not equal to B plus C). (在代码中:A不等于B加C)。 There are responses that I have never "seen" in the line marked as (Z), maybe "lost" responses... 在标记为(Z)的行中,有些响应我从未“看到”,也许是“丢失”的响应...

So, what I am doing wrong? 所以,我做错了什么?

I guess you are using multiple threads for your simulator. 我猜您正在为模拟器使用多个线程。 This looks like the classic Lost Updates problem to me. 在我看来,这似乎是经典的“ 丢失更新”问题。 You have to synchronize the counter-incrementing code. 您必须同步反增量代码。

Incrementing a variable is NOT thread-safe: 递增变量不是线程安全的:

Temp := CounterValue;
// If another thread intercepts here, we've got a lost update
Temp := Temp + 1;
CounterValue := Temp;

See this MSDN article to read more about concurrency issues. 请参阅此MSDN文章,以了解有关并发问题的更多信息。

如果仅使用计数,则可以使用Windows函数InterlockedIncrementInterlockedDecrement并且不需要任何锁定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM