Recent

Author Topic: TTask futures Delphi equivalent under FPC  (Read 5384 times)

guest59697

  • Guest
TTask futures Delphi equivalent under FPC
« on: December 12, 2018, 01:09:42 pm »
hello,

does exists a class to do TTask/ITask async procedure under FPC as in Delphi?

I have seen DoParallel in MtProcs, but it returns when threads have completed the job, instead the TTask returns immediately and let the thread run.

THX
R.

garlar27

  • Hero Member
  • *****
  • Posts: 652
Re: TTask futures Delphi equivalent under FPC
« Reply #1 on: December 12, 2018, 03:10:24 pm »
I think there is one but I don't recall it now.

Well, meanwhile there's TApplication.QueueAsyncCall();

Quote
TApplication.QueueAsyncCall

Inserts an asynchronous call into the queue.

Declaration

Source position: forms.pp line 1462

public procedure TApplication.QueueAsyncCall(
  const AMethod: TDataEvent;
  Data: PtrInt
)
.....
type TDataEvent = procedure(
  Data: PtrInt
) of object;
Though I don't know if it is executed in a different thread than main thread...

I'll look for other solution...

guest59697

  • Guest
Re: TTask futures Delphi equivalent under FPC
« Reply #2 on: December 12, 2018, 03:14:25 pm »
Can be possible to have ProcThreadPool.DoParallel behave not blocking?

guest59697

  • Guest
Re: TTask futures Delphi equivalent under FPC
« Reply #3 on: December 12, 2018, 03:17:13 pm »
btw. I don't use LCL, it's  TApplication.QueueAsyncCall(); available?

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: TTask futures Delphi equivalent under FPC
« Reply #4 on: December 12, 2018, 03:27:21 pm »
btw. I don't use LCL, it's  TApplication.QueueAsyncCall(); available?

Nope. TApplication is part of the LCL and QueueAsyncCall is introduced in it.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

garlar27

  • Hero Member
  • *****
  • Posts: 652
Re: TTask futures Delphi equivalent under FPC
« Reply #5 on: December 12, 2018, 03:28:47 pm »
btw. I don't use LCL, it's  TApplication.QueueAsyncCall(); available?
No, is in the unit Forms.pp

If threads are needed, then I think you should use a thread pool. FPC comes with a thread pool but I never had a chance to use it (I built my own before it was available)...


guest59697

  • Guest
Re: TTask futures Delphi equivalent under FPC
« Reply #7 on: December 12, 2018, 07:01:17 pm »
thanks,
does ezthread class automatically free itself onterminate?

mr-highball

  • Full Member
  • ***
  • Posts: 233
    • Highball Github
Re: TTask futures Delphi equivalent under FPC
« Reply #8 on: December 12, 2018, 07:38:03 pm »
thanks,
does ezthread class automatically free itself onterminate?

Yes, it does as long as you don't hold on to a reference yourself (which may be useful if you want to reuse).
For example, if you were to create and assign to a private thread variable (ie. FThread: IEZThread), once the thread is terminated you would still have that reference and would have to nil it yourself when finished (destructor of object or OnDestroy in form).

Additionally, there are a lot of settings that can be configured, and callback methods for error/finish to fine tune the behavior. Linked is one sample showing some settings that may be of use to you:
https://github.com/mr-highball/ezthreads/blob/master/test/ezthreads_tester.lpr#L161

guest59697

  • Guest
Re: TTask futures Delphi equivalent under FPC
« Reply #9 on: December 12, 2018, 08:05:55 pm »
procedure A;
var B:IEZThread;
begin
B:=IEZThread.Create;
B.Start;
end;

does this works correctly and B is cleaned/free on finish also if the variable is out of scope?

guest59697

  • Guest
Re: TTask futures Delphi equivalent under FPC
« Reply #10 on: December 12, 2018, 08:48:18 pm »
or should use a pointer to correctly nil the variable through a container?

eg.

procedure A;
var B:^iezthread;
begin
New(B);
B^:=iezthread.create;
insert B in some list
in a loop check in the list if B is terminated then Dispose(B)
?

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: TTask futures Delphi equivalent under FPC
« Reply #11 on: December 12, 2018, 08:51:29 pm »
or should use a pointer to correctly nil the variable through a container?

eg.

procedure A;
var B:^iezthread;
begin
New(B);
B^:=iezthread.create;
insert B in some list
in a loop check in the list if B is terminated then Free(B)
?

No need to use a pointer, the object itself is already a reference. Unless the container needs explicitely a pointer type, that is ... in which case you should be rather using a list of objects :)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

mr-highball

  • Full Member
  • ***
  • Posts: 233
    • Highball Github
Re: TTask futures Delphi equivalent under FPC
« Reply #12 on: December 12, 2018, 08:52:45 pm »
taking your original sample, here is a program which demonstrates.

Code: Pascal  [Select][+][-]
  1. {$mode delphi}{$H+}
  2. {$ModeSwitch nestedprocvars}
  3. program test_nonblock;
  4. uses
  5.   SysUtils,
  6.   ezthreads;
  7.  
  8.  
  9. procedure A;
  10. var
  11.   B : IEZThread;//interface type
  12.  
  13.   procedure C(Const AThread:IEZThread);
  14.   begin
  15.     Sleep(1000);
  16.     WriteLn('C finished');
  17.   end;
  18.  
  19. begin
  20.   //create B using Impl class
  21.   B:=TEZThreadImpl.Create;
  22.  
  23.   //provide nested method 'C' for thread 'B' and 'Start'
  24.   B.Setup(C).Start;
  25.  
  26.   //after start is called the local reference is decremented
  27.   //but the thread will live on until method C is finished
  28.   //due to the internal design of ezthreads. to prove write to console
  29.   //that A has finished before C
  30.   WriteLn('A finished');
  31. end;
  32.  
  33. begin
  34.   A();
  35.  
  36.   //wait until thread has time to respond
  37.   Await;
  38.  
  39.   //wait for user to press something again
  40.   ReadLn;
  41. end.
  42.  

I've attached the output of the console app as well, but even the local reference has been decremented, there is an internal reference kept that isn't released until your method has completed or terminated.

mr-highball

  • Full Member
  • ***
  • Posts: 233
    • Highball Github
Re: TTask futures Delphi equivalent under FPC
« Reply #13 on: December 12, 2018, 09:12:33 pm »
Quote
...Unless the container needs explicitely a pointer type, that is ... in which case you should be rather using a list of objects :)

Also a note on what @lucamar said, if you do want to create multiple ezthreads, I've already implemented a threadsafe reference counted collection, with an easy to use interface found here:
https://github.com/mr-highball/ezthreads/blob/master/src/ezthreads.collection.pas


Code: Pascal  [Select][+][-]
  1. procedure TestCollection;
  2. var
  3.   LThread:IEZThread;
  4.   LCollection:IEZCollection;
  5. begin
  6.   LCollection:=TEZCollectionImpl.Create;
  7.   LThread:=TEZThreadImpl.Create;
  8.  
  9.   //add to collection
  10.   LCollection.Add(LThread);
  11.   WriteLn('collection count:', LCollection.Count);
  12.  
  13.   //check to see if thread is in collection
  14.   if LCollection.Exists(LThread) then
  15.     WriteLn('Exists')
  16.   else
  17.     WriteLn('does not exist');
  18.  
  19.   //remove and write count
  20.   LCollection.Remove(LThread);
  21.   WriteLn('collection count:', LCollection.Count);
  22. end;
  23.  
(didn't test this, just typed it straight, but should work... :-[)

guest59697

  • Guest
Re: TTask futures Delphi equivalent under FPC
« Reply #14 on: December 12, 2018, 09:41:00 pm »
thnx for info

 

TinyPortal © 2005-2018