Recent

Author Topic: multitask question  (Read 694 times)

Dzandaa

  • Sr. Member
  • ****
  • Posts: 428
  • From C# to Lazarus
multitask question
« on: April 30, 2025, 08:33:26 pm »
Hi, everybody,

I have a Specialize TObjectList in my main program

Code: Pascal  [Select][+][-]
  1. plotPoints: specialize TObjectList<Point3f>;

I also have a function MyThread that compute a tempPoints also a Specialize TObjectList

Based on LocalThread, different values are computed.

Code: Pascal  [Select][+][-]
  1. threadvar
  2.  thri : ptrint;
  3.  
  4. function MyThread(p : pointer) : ptrint;
  5. var
  6.  tempPoints: specialize TObjectList<Point3f>;
  7.  LocalThread: Integer;
  8. begin
  9.  tempPoints := specialize TObjectList<Point3f>.Create();
  10.  LocalThread := integer(p); // from 0 to 2
  11.  

is it possible to launch 3 threads that compute the tempPoints an then add the result to plotPoints without conflict and how?

like:

Code: Pascal  [Select][+][-]
  1. procedure MyForm.ComputePoints();
  2. var
  3.  ThreadOrder: Integer;
  4. begin
  5.   plotpoints.Clear;
  6.  
  7.   ThreadFinished := -1;
  8.   for ThreadOrder := 0 to Pred(numThreads) do
  9.   BeginThread(@MyThread,pointer(ThreadOrder));
  10.   while ThreadFinished < Pred(numThreads) do ;
  11.  

Any idea?

Yhank you.

B->
Regards,
Dzandaa

MarkMLl

  • Hero Member
  • *****
  • Posts: 8393
Re: multitask question
« Reply #1 on: April 30, 2025, 09:49:03 pm »
Define a subclass of TThread which has a counter class property with an internal critical section. Instantiate this as appropriate.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

d2010

  • Full Member
  • ***
  • Posts: 161
Re: multitask question
« Reply #2 on: April 30, 2025, 10:44:34 pm »
Hi, everybody,
I have a Specialize TObjectList in my main program

I don't user TObjectList , I don'tv use TForm ...
If you need other routine form outside (.eg. Call function from cpkunzip.dll) as
thread , I use this unit. Already I tested in old'Lazarus , but in Lazarus 4.0,
I don~t known, the results are good Or Bad.
All your calledfunction must have this format.
Var body_fas_corogyes:=Addr(YouRFunction);
Code: Pascal  [Select][+][-]
  1. Var fas_addtolog:tap_AddtoLog=nil;
  2.     fas_addtobar:tap_AddtoLog=nil;
  3.     fas_instolog:tap_AddtoLog=nil;
  4.     fas_copsfill_acideecho:tap_copsfill_acideecho=nil;
  5.     fas_copsfill_acidechon:tap_copsfill_acidechon=nil;
  6.     fas_body_Batch2Aseembler:procedure=nil;
  7.     body_fas_hyperlinkdisplaytooltip:function(p:pchar):integer=nil;
  8.     body_fas_corogyes:function(p:pchar):integer=nil;
  9.     body_fas_fartvlax:function(p:pchar):integer=nil;
  10.     body_fas_cleardiropenttd:function(p:pchar):pchar=nil;
  11. // HERE  is NY-TOP-CALLER **
  12. Function Spec_try_Parallel10Invoke(cmdc:Tmcmde):Integer;
  13. Begin
  14.    result:=0;
  15.     with  kSpec_Thread do
  16.       Begin romania:='SetConsole';
  17.             DemoStart:=pcje;  //rem: search equal zero
  18.             if (DemoStart<0) then
  19.                     Begin Don1(0);
  20.                           DemoStart:=0;
  21.                      End;
  22.          case cmdc of
  23. kMcmde_Myes: han[DemoStart]:=BeginThread(nil,0,Addr(body_fas_corogyes),pchar('2'),0,m_dwThreadId[DemoStart]);
  24. kMcmde_Moka: han[DemoStart]:=BeginThread(nil,0,Addr(body_fas_corogyes),pchar('1'),0,m_dwThreadId[DemoStart]);
  25. kMcmde_MfartVlax:han[DemoStart]:=BeginThread(nil,0,Addr(body_fas_fartvlax),pchar('1'),0,m_dwThreadId[DemoStart]);
  26. kMcmde_Mclear0DirOpenTtd:han[DemoStart]:=BeginThread(nil,0,Addr(body_fas_cleardiropenttd),pchar('1'),0,m_dwThreadId[DemoStart]);
  27.          End;//off("body_run_corogyes")
  28.           exit;
  29.      End;
  30.    if (kSpec_thread.DemoStart<0) then begin result:=RTCAN;exit;end;
  31.    kSpec_Thread.Done();
  32. end;
  33.  

Code: [Select]
Type tap_fncall=function(p:pchar):integer;
Var kSpec_Thread:TBegin_Thread;

How to call this unit?
Code: Pascal  [Select][+][-]
  1. Function AutorunIterator_ctrlP(cmdcmdline:pchar):Integer;
  2. Begin
  3.    result:=0;
  4.    with  kSpec_Thread do
  5.       Begin romania:='SetConsole';
  6.             stPeekConsoleInputA(1);
  7.             if (cmdcmdline>kzeropch)and(strlen(cmdcmdline)>100) then
  8.               wmakebat.txt:=strpas(cmdcmdline);
  9.             DemoStart:=pcje;  //rem: search equal zero
  10.             if (DemoStart<0) then
  11.                     Begin Don1(0);
  12.                           DemoStart:=0;
  13.                      End;
  14.              han[DemoStart]:=BeginThread(nil,0,
  15.                        Addr(body_fas_hyperlinkdisplaytooltip),
  16.                        nil,
  17.                        0,
  18.                       m_dwThreadId[DemoStart]);
  19.             if (cmdcmdline<>nil) and (nil<>StrPos(cmdcmdline,'autolink.txt=')) then result:=0  else
  20.             if (cmdcmdline<>nil) then result:=1;
  21.           exit;
  22.      End;
  23.    if (kSpec_thread.DemoStart<0) then begin result:=RTCAN;exit;end;
  24.    kSpec_Thread.Done();
  25. end;
  26.  

Eu multumesc ArsenieBoca. Doamne ajuta
Var AcDbEvalConnectable__ctrlP:array[Byte] of TThreadFunc;
Code: Pascal  [Select][+][-]
  1. Function Cograxono_AcGiSpotLightTraits:integer;
  2. Var hpGapTol:Integer;
  3. Begin
  4.      result:=RTREJ;
  5.      with kSpec_Thread do
  6.        Begin
  7.             DemoStart:=pcje;  //rem: search equal zero
  8.             if (DemoStart<0) then
  9.                     Begin Don1(0);
  10.                           DemoStart:=0;
  11.                      End;
  12.             result:=RTNORM;
  13.             for hpGapTol:=0 to 255 do
  14.                 if (@AcDbEvalConnectable__ctrlP[hpGapTol]<>nil) then
  15.                   Begin
  16.                          han[DemoStart]:=BeginThread(nil,0,AcDbEvalConnectable__ctrlP[hpGaptol],
  17.                          Addr(WhipThread[DemoStart]),
  18.                          0,
  19.                          m_dwThreadId[DemoStart]);
  20.                          inc(DemoStart);
  21.                    End;
  22.           result:=RTNORM;
  23.      End;
  24. End;
  25.  

PreCall Here================
AcDbEvalConnectable__ctr[01]:=nil;
cDbEvalConnectable__ctrlP[0]:=Addr(AcDbEvalContextIterator__UnzipTest_and_ShowGifInternal);

Code: Pascal  [Select][+][-]
  1. Const RTNORM=5100;
  2.       RTMINILEN = (-5013);
  3.      RTCAN=(-5002);
  4.       RTCAN0=(-5002);
  5.       RTREJ=(-5003);
  6.       RTREJ0=(-5003);
  7.       RTFAIL=(-5004);
  8.       RTKWORD=     (-5005);
  9.       RTINPUTTRUNCATED =(-5008);
  10.       RTfast=(-5009);
  11.       RTslow=(-5010);
  12.       RTSKIP=(-5011);
  13.       RTQUIT=(-5012);
  14.  
:o

Thank you... Click here Click here Doamne ajuta, Multumesc, G\Thank you. Gracias


My Solution is outside of TForms, because , I need call
other function/s from external 'DLL.
« Last Edit: April 30, 2025, 11:13:08 pm by d2010 »

Thaddy

  • Hero Member
  • *****
  • Posts: 16944
  • Ceterum censeo Trump esse delendam
Re: multitask question
« Reply #3 on: May 01, 2025, 03:56:58 pm »
This does not solve your problem but makes it more readable:
Code: Pascal  [Select][+][-]
  1. type
  2.   // always specialize to a type, not a var
  3.   TplotPoints = specialize TObjectList<Point3f>;
  4.  
  5. // then subsequently:
  6. function MyThread(p : pointer) : ptrint;
  7. var
  8.  tempPoints: TplotPoints;// specialize gone!
  9.  LocalThread: Integer;
  10. begin
  11.  tempPoints := TplotPoints.Create;// specialize gone!
  12.  LocalThread := integer(p); // from 0 to 2
I will show you a solution later, but that requires fpc trunk/main.
Let me know if that is acceptable for you.
« Last Edit: May 01, 2025, 04:08:38 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

Thaddy

  • Hero Member
  • *****
  • Posts: 16944
  • Ceterum censeo Trump esse delendam
Re: multitask question
« Reply #4 on: May 01, 2025, 05:26:27 pm »
Don't have to write example, because it is in the documentation (not the wiki):
https://www.freepascal.org/docs-html/rtl/classes/tthread.executeinthread.html

Executes 2 threads and reports the results. You can capture the results in variables and subsequenty perform calculation with them.
( For windows, remove cthreads and add windows)
« Last Edit: May 01, 2025, 05:29:58 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

Dzandaa

  • Sr. Member
  • ****
  • Posts: 428
  • From C# to Lazarus
Re: multitask question
« Reply #5 on: May 01, 2025, 07:21:49 pm »
Hi,

Thank you all for your comments.
For the moment, I've put aside the Multitasking part a bit, to be sure that the program works properly, including the OpenGL part.

This seems to be okay for now without memory leaks.

Always learning something in this forum :)

B->
Regards,
Dzandaa

 

TinyPortal © 2005-2018