Lazarus

Announcements => Third party => Topic started by: mr-highball on April 15, 2020, 06:05:41 am

Title: ezthreads - thread pool support
Post by: mr-highball on April 15, 2020, 06:05:41 am
added thread pool support to ezthreads, hope this helps someone out there,

some features:

basic usage:
Code: Pascal  [Select][+][-]
  1. (*
  2.   test that shows two simple tasks completing in parallel
  3.   by an ezthread pool
  4. *)
  5. procedure TestTwoTasks;
  6. var
  7.   LPool : IEZThreadPool;
  8.   LJobOneFinished,
  9.   LJobTwoFinished: Boolean;
  10.  
  11.   //simple parallel job
  12.   procedure JobOne(const AThread : IEZThread);
  13.   begin
  14.     //do some work
  15.     Sleep(100);
  16.     LJobOneFinished := True;
  17.   end;
  18.  
  19.   //simple parallel job
  20.   procedure JobTwo(const AThread : IEZThread);
  21.   begin
  22.     //do some work
  23.     Sleep(100);
  24.     LJobTwoFinished := True;
  25.   end;
  26.  
  27. begin
  28.   //flags for checking if both jobs finished
  29.   LJobOneFinished := False;
  30.   LJobTwoFinished := False;
  31.  
  32.   //init a pool with two workers
  33.   LPool := NewEZThreadPool(2);
  34.  
  35.   //work two jobs
  36.   LPool
  37.     .Queue(JobOne, nil, nil)
  38.     .Queue(JobTwo, nil, nil)
  39.     .Start; //start the pool
  40.  
  41.   //wait until both jobs finish
  42.   Await(LPool);
  43.  
  44.   //write status
  45.   WriteLn(Format('TestTwoTasks::[success]:%s', [BoolToStr(LJobOneFinished and LJobTwoFinished, True)]));
  46. end;
  47.  

more details found on this github issue post:
https://github.com/mr-highball/ezthreads/issues/4#issuecomment-613802662

initial ezthread post:
https://forum.lazarus.freepascal.org/index.php/topic,42421.msg296134.html#msg296134
Title: Re: ezthreads - thread pool support
Post by: schuler on April 16, 2020, 05:57:51 am
 :) happy threading with happy pascal coding!!!  :)
Title: Re: ezthreads - thread pool support
Post by: jiaxing2 on May 14, 2020, 08:21:35 am
Could this work with the normal FPC mode?
Title: Re: ezthreads - thread pool support
Post by: PascalDragon on May 14, 2020, 09:37:15 am
You'll probably need to use at least mode ObjFPC. The compiler's default mode does not support classes.
Title: Re: ezthreads - thread pool support
Post by: mr-highball on May 14, 2020, 03:40:43 pm
Thanks @Pascaldragon, I wasn't sure on this since I normally use mode delphi, but this does work fine for ObjFpc too as you pointed out
TinyPortal © 2005-2018