Forum > Beginners

Howto send Record to thread via Pointer?

(1/5) > >>

jan741:
I need send a record(containing numeral and string) to multiple threads. How to?

Tried different things, this doesn't work:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs,  StdCtrls, dbugintf, LazLoggerBase, pingsend; const  threadcount = 100;  stringlen = 10000; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    Button2: TButton;    procedure Button1Click(Sender: TObject);    procedure Button2Click(Sender: TObject);  private    { private declarations }  public    { public declarations }  end;   myrec = record    i : longint;    s : string;  end;   Pmyrec = ^myrec; var  Form1: TForm1;  finished : longint; threadvar         thri : ptrint; implementation{$R *.lfm} { TForm1 } function f(p : Pointer) : ptrint; var  s : ansistring;  thisrec : myrec;begin   Writeln('thread ', longint(p^.i), 'started');  thri:=0;  while (thri<stringlen) do    begin    s:=s+'1';    inc(thri);    end;  Writeln('thread ',p^.i,' finished with',p^.s);  InterLockedIncrement(finished);  f:=0;end; procedure TForm1.Button1Click(Sender: TObject);begin end; procedure TForm1.Button2Click(Sender: TObject);var  r : myrec;  x : longint;begin   finished:=0;   r.s := ' test ';   for x :=1 to threadcount do     r.i := x;     BeginThread(@f,@r);   while finished<threadcount do ;   Writeln(finished);end;  end.

Zvoni:
errr.... is there a Begin/End missing in the For-loop?
The For-Loop as it stands only executes line 81 for 100 times
BeginThread is only executed once (with r.i=100) because it's outside the For-Loop

jan741:
Yes thanks.
That was also a problem. I'm more used to {} or inclinations(python)  8-)

jan741:
Now I got it working. But thirsrec^ in every thread is referencing to the same object (on the programstack I think) So every thread writes "100" as output. So, I should have a local copy(on the thread-stack if that exists) for every thread of r:myrec. Is that possible?


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs,  StdCtrls, dbugintf, LazLoggerBase, pingsend; const  threadcount = 100;  stringlen = 10000; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    Button2: TButton;    procedure Button1Click(Sender: TObject);    procedure Button2Click(Sender: TObject);  private    { private declarations }  public    { public declarations }  end;   myrec = record    i : longint;    s : string;  end;   Pmyrec = ^myrec; var  Form1: TForm1;  finished : longint; threadvar         thri : ptrint; implementation{$R *.lfm} { TForm1 } function f(p : Pointer) : ptrint; var  s : ansistring;  thisrec : ^myrec;begin  thisrec := Pmyrec(p);  Writeln('thread ', longint(thisrec^.i), ' started');  thri:=0;  while (thri<stringlen) do    begin    s:=s+'1';    inc(thri);    end;  Writeln('thread ',thisrec^.i,' finished with',thisrec^.s);  InterLockedIncrement(finished);  f:=0;  writeln('end of function');end; procedure TForm1.Button1Click(Sender: TObject);begin end; procedure TForm1.Button2Click(Sender: TObject);var  r : myrec;  x : longint;begin   finished:=0;   r.s := ' test ';   for x :=1 to threadcount do     begin       r.i := x;       BeginThread(@f,@r);       writeln('ready for nex t');     end;   while finished<threadcount do ;   Writeln('goopend ' + IntToStr(finished));end;end.

mika:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program Unit1;{$apptype console}{$mode objfpc}{$H+}  uses{$ifdef UNIX}  cthreads, {$endif}   Classes, SysUtils; const      threadcount = 100;      stringlen = 10000; type  myrec = record    i : longint;    s : string;  end;   Pmyrec = ^myrec; var  finished : longint; threadvar         thri : ptrint;  function f(p : Pointer) : ptrint; var  s : ansistring;  thisrec : ^myrec;begin  thisrec := Pmyrec(p);  Writeln('thread ', longint(thisrec^.i), ' started');  thri:=0;  while (thri<stringlen) do    begin    s:=s+'1';    inc(thri);    end;  Writeln('thread ',thisrec^.i,' finished with',thisrec^.s);  InterLockedIncrement(finished);  f:=0;  writeln('end of function');end; procedure button;var  r : array [1..threadcount] of myrec;  x : longint;begin   finished:=0;    for x :=1 to threadcount do     begin       r[x].s := ' test ';       r[x].i := x;       BeginThread(@f,@r[x]);       writeln('ready for nex t');     end;   while finished<threadcount do ;   Writeln('goopend ' + IntToStr(finished));end; begin     button;end.In way you are using record in thread, you have to have an original record in main thread. I made array of record and passed new unique record to new thread.

Navigation

[0] Message Index

[#] Next page

Go to full version