简体   繁体   English

delphi 用 TStringList 模拟 fifo

[英]delphi simulate fifo with TStringList

i need to process a received data from a tcp link.the data are frames of hex string at length of 203 bytes.我需要处理从 tcp 链接接收到的数据。数据是 203 字节长度的十六进制字符串帧。

i save them at the end of tstringlist我将它们保存在 tstringlist 的末尾

MyList.Add( input );

and from a second thread read the first string and process it and remove firs from the list并从第二个线程读取第一个字符串并处理它并从列表中删除 firs

procedure TMyThread.Execute;
 var str : string;
begin
    while not Terminated do
    begin
      FTermEvent.WaitFor(100);
      if not Terminated then
      begin
          str := MyList[0];     
          MyList.Delete(0);
          //some process
      end;
    end
end;

The question is, is this thread safe?!问题是,这个线程安全吗?

If you are afraid to loose input data using database, you can try to use TThreadStringList.如果您害怕使用数据库丢失输入数据,您可以尝试使用 TThreadStringList。 I imagine that your software receives data from multiple devices simultaneously (and in this case you should create a multi-thread socked to make you sure to receive all the data) if you are receiving data from a single device instead, you should make sure that the tcp protocol supports a sort of aknowlage system to avoid losing data or at least to report in a log, the data that your application has not been able to receive completely.我想您的软件同时从多个设备接收数据(在这种情况下,您应该创建一个多线程 socked 以确保您确保接收所有数据)如果您从单个设备接收数据,您应该确保tcp 协议支持一种识别系统,以避免丢失数据或至少在日志中报告您的应用程序无法完全接收的数据。

Anyway, TThreadStringList is a simple wrapper for TStringList.无论如何,TThreadStringList 是 TStringList 的简单包装器。 It should makes possible to access a String List from different threads without any conflicts.它应该可以从不同的线程访问字符串列表而不会发生任何冲突。 I can't test it but for you should be easy and quick to try.我无法对其进行测试,但对您来说应该可以轻松快速地尝试。

The link: TThreadStringList链接: TThreadStringList

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

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