开发者

Delphi: one thread cannot see other thread's array

ANSWER: Allright, that was quite simple (probably I made this mistake coz I need more sleep :) ): I've created First Thread and it created Sub-Threads and FREE-ed it-self. So it was naturall that some sub-thread souldn't access not existing memory (before they tryed - First Thread wasn`t already in memory).

Hi, I'm trying to make simple application. My needs are to create for example 1-3 threads (I will call them First Threads), which will cr开发者_JAVA百科eate next few threads (I will call them Sub-Threads).

I know how to do it, this is what I have done:

First Thread definition:

 type
 TFirstThread = class(TThread)
   // 
   strict private
     fID:cardinal; //fID = position on watki array + 1
     fDoneItems:cardinal;
     fItems:TSomeRecordAr;     

     //(...)
     procedure ParseItem(var item: TSomeRecord; itemID:cardinal);
   private
   public                 
   published                            
     function GetItem(itemindex:cardinal):TSomeRecord;  
     procedure SetItem(itemindex: cardinal; item: TSomeRecord);                             
     //(...)
     procedure Execute; override;
 end;

TSomeRecord is:

TSomeRecord = record
  str,str2:string;
  lst:TStrings;
  continue:boolean;
end;

Sub-Thread definition:

TSubThread = class(TThread)
  public
    fReady:boolean;
    fID,fItemID:cardinal;
    procedure Execute; override;
end;

And also array of First Threads:

watki:array of TFirstThread;

Body of First Threads:

{ TFirstThread }

//(...)

procedure TFirstThread.ParseItem(var item:TSomeRecord; itemID:cardinal);
begin
     //(...)
     with TSubThread.Create(False) do begin
       fID:=Self.fID;
       fItemID:=itemID;
       fReady:=True;
     end;
end;

procedure TFirstThread.Execute;
var
  i:cardinal;
begin
FreeOnTerminate:=True;
while fReady=False do
  Sleep(10);

//(...)
fDoneItems := 1;

for i := 0 to High(fItems) do begin
 ParseItem(fItems[i], i);
end;
// 
end;

function TFirstThread.GetItem(itemindex: cardinal): TSomeRecord;
begin
 result:=fItems[itemindex];
end;

procedure TFirstThread.SetItem(itemindex: cardinal; item: TSomeRecord);
begin
 fItems[itemindex]:=item;
end;

Body of Sub-Threads:

procedure TSubThread.Execute;
var
ftd:string;
tries:cardinal;
fItem:TSomeRecord;
begin
FreeOnTerminate:=True;
while fReady=False do
Sleep(10);
try
  //(...)
   fItem := watki[fID-1].GetItem(fItemID); //HERE AV << 
   fItem.continue:=True;
  //(...)
finally
watki[fID-1].SetItem(fItemID, fItem);
//(...)
//Free;
end;
end;

This is how doeas it looks in practice: While I'm testing, I'm creating just 1 First Thread and start it. It has 3 items, so it creates 3 Sub-Threads.

When I make breakpoint in TFirstThread this is what I can see:

http://i.stack.imgur.com/EGaBO.jpg

everything is OK, but after that when I make breakpoint in TSubThread this is what i get:

http://i.stack.imgur.com/gXhHW.jpg

so everithing is OK except fItems - idk why, but I can't see it's content. So ofc I get AV, coz item I want to get doesn't exist.

Why can it be like that? Any solutions?

Thanks in advance.

Btw I'm using Delphi 2009

ANSWER: Allright, that was quite simple (probably I made this mistake coz I need more sleep :) ): I've created First Thread and it created Sub-Threads and FREE-ed it-self. So it was naturall that some sub-thread souldn't access not existing memory (before they tryed - First Thread wasn`t already in memory).


Allright, that was quite simple (probably I made this mistake coz I need more sleep :) ): I've created First Thread and it created Sub-Threads and FREE-ed it-self. So it was naturall that some sub-thread souldn't access not existing memory (before they tryed - First Thread wasn`t already in memory).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜