从两个线程保护字符串缓冲区?(Protect a string buffer from two th

2019-09-18 08:18发布

I am working with streaming packet strings through Indy sockets, and on the client side, I have a thread which reads incoming data from the TIdTCPClient and continuously appending this data to the end of a single string buffer. I have another thread which is continuously reading this buffer from the beginning, copying (and deleting) data as necessary (one complete packet at a time).

I know that in any case two threads accessing the same variable can be dangerous. But does this apply to strings too? Or just objects? Can I feel safe with this reading/writing of the same string from two different threads? If not, then what should I do to protect this string? This is a plain string called FBuffer.

I'm appending data to the end like so:

procedure TListenThread.CheckForData;
begin
  if FClientSocket.Connected then begin
    FClientSocket.IOHandler.CheckForDataOnSource(5000);
    if not FClientSocket.IOHandler.InputBufferIsEmpty then
      FBuffer:= FBuffer + FClientSocket.IOHandler.InputBufferAsString;
  end;
end;

And the other thread is reading it like so:

procedeure TPacketThread.CheckForPacket;
var
  P: Integer; //Deliminator position
  T: String;  //Temp copying string
  Z: Integer; //Expected packet size
begin
  P:= Pos('#', FBuffer);
  if P > 0 then begin //Is the deliminator found?
    T:= Copy(FBuffer, 1, P-1); //Copy up to deliminator...
    Z:= StrToIntDef(T, 0); //Convert packet size to integer...
    if Z > 0 then begin
      //Is there a full packet waiting in buffer?
      if Length(FBuffer) >= Z then begin
        //First, delete size definition and deliminator...
        Delete(FBuffer, 1, P);
        //Now grab the rest of it up to the packet size...
        T:= Copy(FBuffer, 1, Z);
        //Delete what we just copied...
        Delete(FBuffer, 1, Z);
        //Finally, pass this packet string for further processing...
        ProcessPacket(T);
      end;
    end;
  end;
end;

The code is a simplified version of my code just to demonstrate all the work I need to do with FBuffer.

Answer 1:

是的,你必须保护并发访问字符串缓冲区。 印有TIdThreadSafeString您可以使用这一目的,如类:

FBuffer: TIdThreadSafeString;
// make sure to Create() and Free() as needed..

procedure TListenThread.CheckForData; 
begin 
  if FClientSocket.Connected then begin 
    FClientSocket.IOHandler.CheckForDataOnSource(5000); 
    if not FClientSocket.IOHandler.InputBufferIsEmpty then 
      FBuffer.Append(FClientSocket.IOHandler.InputBufferAsString);
  end; 
end; 

procedure TPacketThread.CheckForPacket; 
var 
  P: Integer; //Deliminator position 
  T: String;  //Temp copying string 
  Z: Integer; //Expected packet size 
begin 
  FBuffer.Lock;
  try
    P:= Pos('#', FBuffer.Value); 
    if P > 0 then begin //Is the deliminator found? 
      T := Copy(FBuffer.Value, 1, P-1); //Copy up to deliminator... 
      Z := StrToIntDef(T, 0); //Convert packet size to integer... 
      if Z > 0 then begin 
        //Is there a full packet waiting in buffer? 
        if Length(FBuffer.Value) >= Z then begin 
          //First, delete size definition and deliminator... 
          FBuffer.Value := Copy(FBuffer.Value, P+1, MaxInt); 
          //Now grab the rest of it up to the packet size... 
          T := Copy(FBuffer.Value, 1, Z); 
          //Delete what we just copied... 
          FBuffer.Value := Copy(FBuffer.Value, Z+1, MaxInt); 
          //Finally, pass this packet string for further processing... 
          ProcessPacket(T); 
        end; 
      end; 
    end; 
  finally
    FBuffer.Unlock;
  end;
end; 

随着中说,给你表明了对数据包的格式是什么,我会采取不同的策略,而不是:

FBuffer: TIdThreadSafeStringList;
// make sure to Create() and Free() as needed..

procedure TListenThread.CheckForData; 
var 
  T: String;  //Temp copying string 
  Z: Integer; //Expected packet size 
begin 
  if FClientSocket.Connected then begin 
    if FClientSocket.IOHandler.InputBufferIsEmpty then begin
      FClientSocket.IOHandler.CheckForDataOnSource(5000);
      if FClientSocket.IOHandler.InputBufferIsEmpty then Exit;
    end; 
    // data is available, keep reading as long as packets are present...
    repeat
      T := FClientSocket.IOHandler.ReadLn('#');
      Z := StrToIntDef(T, 0);
      if Z > 0 then begin 
        T := FClientSocket.IOHandler.ReadString(Z); 
        FBuffer.Add(T); 
      end; 
    until FClientSocket.IOHandler.InputBufferIsEmpty;
  end; 
end; 

procedure TPacketThread.CheckForPacket; 
var 
  L: TStringList;
  T: String;
begin 
  L := FBuffer.Lock;
  try
    if L.Count = 0 then Exit;
    T := L[0];
    L.Delete(0);
  finally
    FBuffer.Unlock;
  end;
  ProcessPacket(T); 
end; 


Answer 2:

是的,你必须当从倍数线程访问保护字符串,你可以做到这一点使用crtical部分。 看看到EnterCriticalSection的 , LeaveCriticalSection , InitializeCriticalSection和DeleteCriticalSection功能。



文章来源: Protect a string buffer from two threads?