IDTCPServer实时接收八个客户端的数据,如果处理线程同步的问题。 ( 积分: 200 )

  • 主题发起人 主题发起人 WangZhaoHui
  • 开始时间 开始时间
W

WangZhaoHui

Unregistered / Unconfirmed
GUEST, unregistred user!
由于我同时要有八个客户端实时的发送数据,我在服务端接收时,有时由于处理压力过大,可能会导致全局变量被改写。我已经建立接收缓冲区,所以我希望线程同步,我问的是用哪种方案最好,由于我在execute中执行的过程有参数,可能用不了Synchronize,我是用阻塞,互斥,信号量哪一种好呢。代码怎么写
Clients是一个全局的ThreadList,我主要是怕它的信息被改写。
procedure TfrmMain.TCPServerExecute(AThread: TIdPeerThread);
var
ActClient: PClient;
begin
if not AThread.Terminated and AThread.Connection.Connected then
begin
AThread.Connection.ReadBuffer(ReceiveData, SizeOfReceiveData);
ActClient := PClient(AThread.Data);
处理接收数据存入缓冲区。....
end;
end;
 
由于我同时要有八个客户端实时的发送数据,我在服务端接收时,有时由于处理压力过大,可能会导致全局变量被改写。我已经建立接收缓冲区,所以我希望线程同步,我问的是用哪种方案最好,由于我在execute中执行的过程有参数,可能用不了Synchronize,我是用阻塞,互斥,信号量哪一种好呢。代码怎么写
Clients是一个全局的ThreadList,我主要是怕它的信息被改写。
procedure TfrmMain.TCPServerExecute(AThread: TIdPeerThread);
var
ActClient: PClient;
begin
if not AThread.Terminated and AThread.Connection.Connected then
begin
AThread.Connection.ReadBuffer(ReceiveData, SizeOfReceiveData);
ActClient := PClient(AThread.Data);
处理接收数据存入缓冲区。....
end;
end;
 
AThread.Connection.Socket.Send(Buffer,SizeOf(CBLoginResult)+1);
AThread.Synchronize(GetList);
 
TTThreadList是线程安全的吧,它会处理你担心的问题。
 
问题是这样的,共有8个客户端线程上传数据,速度大概每秒500包,有时候发送时也会出现堆包现象,会导致我接收瞬时间处理压力较大,结果有时就会出现接收有的客户端数据出错
procedure TfrmMain.IDTCPServerExecute(AThread: TIdPeerThread);
var
ActClient: PClient;
ReceiveData: TReceiveData;//是一个接收数据包大小为100字节
begin
if not AThread.Terminated and AThread.Connection.Connected then
begin
//我怀疑是这句出错了,能不能是安全的,数据接收的太多时会不会有问题呢?共八个线程实时的发大量数据
AThread.Connection.ReadBuffer(ReceiveData, SizeOf(ReceiveData));

ActClient := PClient(AThread.Data);
PushReceiveQueue(ReceiveData, rdHead);//往全局接收缓冲区时存数,共八个缓冲区对应八个客户端
if not TimerEnabled then
begin
Changing := False;
TimerEnabled := True;
ProTimeCallBack := TimeProc;
hTimeID := TimeSetEvent(30, 0, ProTimeCallBack, 1, 1);//打开画曲线的定时器
end;
end;
end;
//存放接收数据的全局的缓冲区数组
procedure PushReceiveQueue(aReceiveData: TReceiveData; Index: Integer);
begin
arryReceiveQueue[Index, arryQueueHead[Index]] := aReceiveData;
Inc(arryQueueHead[Index]);
arryQueueHead[Index] := arryQueueHead[Index] mod vQueueLength;
Inc(arryQueueCount[Index]);
end;
 
用临界区
Synchronize就算能用也不管用的
 
maze 帮我看看在哪里写啊。
 
procedure PushReceiveQueue(aReceiveData: TReceiveData; Index: Integer);
begin
EnterCriticalSection(DataLock);
try
arryReceiveQueue[Index, arryQueueHead[Index]] := aReceiveData;
Inc(arryQueueHead[Index]);
arryQueueHead[Index] := arryQueueHead[Index] mod vQueueLength;
Inc(arryQueueCount[Index]);
finally
LeaveCriticalSection(DataLock);
end;
end;
 
后退
顶部