Recent

Author Topic: ezthreads - thread pool support  (Read 778 times)

mr-highball

  • Full Member
  • ***
  • Posts: 175
    • Highball Github
ezthreads - thread pool support
« on: April 15, 2020, 06:05:41 am »
added thread pool support to ezthreads, hope this helps someone out there,

some features:
  • works with await (entire pool or specific group id)
  • thread pool is itself an IEZThread, so all events are supported
  • safe capturing of success / error cases for individual threads
  • global arguments passed to pool carry to workers

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

schuler

  • Full Member
  • ***
  • Posts: 162
Re: ezthreads - thread pool support
« Reply #1 on: April 16, 2020, 05:57:51 am »
 :) happy threading with happy pascal coding!!!  :)

jiaxing2

  • Full Member
  • ***
  • Posts: 164
Re: ezthreads - thread pool support
« Reply #2 on: May 14, 2020, 08:21:35 am »
Could this work with the normal FPC mode?

PascalDragon

  • Hero Member
  • *****
  • Posts: 1694
  • Compiler Developer
Re: ezthreads - thread pool support
« Reply #3 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.

mr-highball

  • Full Member
  • ***
  • Posts: 175
    • Highball Github
Re: ezthreads - thread pool support
« Reply #4 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