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
Comments
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: |
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. |
Give me a day or so and I'll put something together and post back with a sample |
@mr-highball Thank you |
-starting to work on pool support for #4
-continued work for thread pool support reported in #4 -currently there is a race condition... need to figure this out
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. |
@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
There are tests located at found here, 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; 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 |
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. |
Does ezthreads support thread pool? If yes, is there any sample code for me to get started?
The text was updated successfully, but these errors were encountered: