Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thread pool support #4

Closed
zamronypj opened this issue Apr 3, 2020 · 7 comments
Closed

Thread pool support #4

zamronypj opened this issue Apr 3, 2020 · 7 comments

Comments

@zamronypj
Copy link

Does ezthreads support thread pool? If yes, is there any sample code for me to get started?

@mr-highball
Copy link
Owner

While the concept of a thread pool isn't defined "out of the box" with ezthreads, there is a grouping mechanism (thread group id) which can be used in tandem with the provided Await(GroupID) method. Additionally there is also a thread safe collection object which can store any number of ezthreads, so a pool should be pretty easy to setup. Do you have a simple example you would like me to setup in code for you? I don't mind adding this as a gist or sample to the tester.

The thread ID is normally dynamically generated, but it can be manually set for each thread in the pool by using:
ezthread.Settings.Await.Group(aDifferentThread
(Or by using the UpdateGroupID(aCustomID) method)

@zamronypj
Copy link
Author

@mr-highball ,

Basically I need to setup one producer thread which will enqueue tasks into a queue and one or more fixed number of pooled consumer threads which will dequeue task from queue and execute task.

@mr-highball
Copy link
Owner

Give me a day or so and I'll put something together and post back with a sample

@zamronypj
Copy link
Author

@mr-highball Thank you

mr-highball added a commit that referenced this issue Apr 6, 2020
-starting to work on pool support for #4
mr-highball added a commit that referenced this issue Apr 7, 2020
-continued work for thread pool support reported in #4
-currently there is a race condition... need to figure this out
@mr-highball
Copy link
Owner

Decided to implement this as more of a core thing rather than a simple demo since I feel like this could benefit people. There are two commits showing the direction I'm going linked to this issue above, but I'm still working through some things.
Will try and finish up tomorrow.

mr-highball added a commit that referenced this issue Apr 15, 2020
- fixed race condition / AV
- added tracing define to pool / thread units
- cleaned up a few tests
@mr-highball
Copy link
Owner

@zamronypj I have completed an initial stab at thread pools, but will be adding some more features / tests and some documentation to the repo within a day or two.

basic 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
  • more...

There are tests located at found here,
ezthreads_tester_pool

but here is a small sample,

(*
  test that shows two simple tasks completing in parallel
  by an ezthread pool
*)
procedure TestTwoTasks;
var
  LPool : IEZThreadPool;
  LJobOneFinished,
  LJobTwoFinished: Boolean;

  //simple parallel job
  procedure JobOne(const AThread : IEZThread);
  begin
    //do some work
    Sleep(100);
    LJobOneFinished := True;
  end;

  //simple parallel job
  procedure JobTwo(const AThread : IEZThread);
  begin
    //do some work
    Sleep(100);
    LJobTwoFinished := True;
  end;

begin
  //flags for checking if both jobs finished
  LJobOneFinished := False;
  LJobTwoFinished := False;

  //init a pool with two workers
  LPool := NewEZThreadPool(2);

  //work two jobs
  LPool
    .Queue(JobOne, nil, nil)
    .Queue(JobTwo, nil, nil)
    .Start; //start the pool

  //wait until both jobs finish
  Await(LPool);

  //write status
  WriteLn(Format('TestTwoTasks::[success]:%s', [BoolToStr(LJobOneFinished and LJobTwoFinished, True)]));
end;

image below showing output,
image

If you get a chance to test out and let me know if you see something worth adding / bugs please reach back out to this issue, or create a new one if this one has been closed by that time

@zamronypj
Copy link
Author

@mr-highball

Thank you for your great work. I will try it ASAP. I will submit new issue if any. For now I will close this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants