我有两个线程,使用全局变量共享数据
一个线程写入,一个线程读取,可是为什么会经常读取失败呢
codearry:tcodearray //全局变量数组
cs:TCriticalSection;
线程A的循环如下:
while not Self.Terminated do
  begin
   icode=getcode;
  if icode<>0 then
  begin
  cs.enter
  codearray[i].code=icode;
  cs.leavr;
  inc(i)
  sleep(400);
  end
  else
  sleep(10);
  end;
线程B中循环读取
while not Self.Terminated do
  begin
  cs.enter;
  icode=codearray[i].code;
  cs.leave;
  if icode<>0 then
  begin
  ......
  sleep(400);
  inc(i)
  end
  else
  sleep(10);
  end;
请那位大哥能帮助一下有没有更好的数据共享办法?
现在的问题是线程B会读取失败导致错误了一些需要处理的过程
请大侠们指点一下,万分感谢

解决方案 »

  1.   

    var 
     CS:TRTLCriticalSection; 
    begin 
    InitializeCriticalSection(CS);  //开始初始化临界区 
      EnterCriticalSection(CS);      //进入临界区 
      在这个里面加入你要保护同步的资源,比如变量,全局链表等资源 
      LeaveCriticalSection(CS);      //离开临界区  DeleteCriticalSection(CS);    //关闭释放资源 
    end; 
      

  2.   

    1.tcodearray 是如何定义,如何初始化的?换句话说使用下标(程序当中的i)去操作会不会导致越界?
    2.TCriticalSection有没有进行初始化?
    3.能不能贴你最原始的代码?
      

  3.   

    是不是应该象下面这样,楼主试试看:
    cs:=TCriticalSection.Create;
    try
      ...
      cs.Enter;
      ...
      cs.Leave;
      ...
    finally
      cs.Free;
    end;
      

  4.   

    Unit xxxx;
    interface
    ...
    var
      CodeArry:TCodeArray //全局变量数组 
      CS:TCriticalSection; 
    implementation
    ...
    Initialization
      CS := TCriticalSection.Create;
      InitCodeArray(CodeArray);
    finilization
      CS.Enter;
      FreeCodeArrayIfNeed(CodeArray);
      CS.Leave;
      CS.Free;
    end.
    线程A的循环如下: 
    while not Self.Terminated do 
      begin 
      icode=getcode; 
      if icode <>0 then 
      begin 
      cs.enter 
      codearray[i].code=icode; 
      cs.leavr; 
      inc(i) 
      sleep(400); 
      end 
      else 
      sleep(10); 
      end; 
    线程B中循环读取 
    while not Self.Terminated do 
      begin 
      cs.enter; 
      icode=codearray[i].code; 
      cs.leave; 
      if icode <>0 then 
      begin 
      ...... 
      sleep(400); 
      inc(i) 
      end 
      else 
      sleep(10); 
      end; 
      

  5.   

    untmain.....procedure Mainfrm.StartbtnClick(Sender: TObject);
    begin
      if Startbtn.Caption='开始' then
      begin
        Startbtn.Caption:='停止';
        FillChar(codearray,SizeOf(codearray),0); //初始化codearray
        Athread.keyindex:=0;  //初始化线程的计数
        Bthread.keyindex:=0;  //初始化线程的计数
        Athread.Resume;
        Bthread.Resume;
        mmo1.Lines.Clear;
        mmo2.Lines.Clear;
        cs:=TCriticalSection.Create;
      end
      else
      begin
        Startbtn.Caption:='开始';
        Athread.Suspend;
        Bthread.Suspend;
      end;
    end;线程A的循环如下: 
    while not Self.Terminated do 
      begin 
      icode=getcode; 
      if icode <>0 then 
      begin 
      cs.enter 
      try
      codearray[Fkeyindex].code=icode; 
      inc(Fkeyindex);  //(单独计数)
      finally
      cs.leavr; 
      end;
      sleep(400); 
      end 
      else 
      sleep(10); 
      end; 
    线程B中循环读取 
    while not Self.Terminated do 
      begin 
      cs.enter; 
      try
      icode=codearray[Fkeyindex].code; 
      finally
      cs.leave;
      end; 
      if icode <>0 then 
      begin 
      ...... 
      sleep(400); 
      icode:=0; 
      inc(Fkeyindex)  //单独计数
      end 
      else 
      sleep(10); 
      end; 
      

  6.   

    找到原因了,由于cs导致挂起使用B线程的操作失去时效性,谢谢大家,结贴走人!再次感谢