#1 2024-10-14 04:07:43

zen010101
Member
Registered: 2024-06-15
Posts: 70

determine whether all Tasks in TSynThreadPool have been executed

I know that I can inherit TSynThreadPool and add a TaskCount property in the subclass, increase the Property by 1 after Push, decrement it by 1 when the Task completes, and use TLightLock to make it thread safe.

But I want to know if there is an easier way? I tried the RunningThreads attribute, but it didn’t work. It is always consistent with the parameters of Create(NumberOfThreads) and cannot be used to confirm the number of Tasks being executed.

Here is my code: https://gist.github.com/zen010101/def68 … 0b028d70d2

Offline

#2 2024-10-14 04:52:08

zen010101
Member
Registered: 2024-06-15
Posts: 70

Re: determine whether all Tasks in TSynThreadPool have been executed

When WINIOCP is not used, I can determine whether all Tasks have been executed by judging whether PendingContextCount is 0.

Offline

#3 2024-10-14 06:56:16

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,686
Website

Re: determine whether all Tasks in TSynThreadPool have been executed

Please try
https://github.com/synopse/mORMot2/commit/e46982c6

Now you should have access to a TSynThreadPool.PendingContextCount property even with IOCP.

Note: InterlockedIncrement/Decrement() are better than a lock if you just need atomic access to an integer.

Offline

#4 2024-10-14 09:23:59

zen010101
Member
Registered: 2024-06-15
Posts: 70

Re: determine whether all Tasks in TSynThreadPool have been executed

Confirm that the PendingContextCount property can also be used in IOCP mode as the condition for determining the completion of all Task executions.

Offline

#5 2024-10-14 09:47:41

zen010101
Member
Registered: 2024-06-15
Posts: 70

Re: determine whether all Tasks in TSynThreadPool have been executed

New bug:

When TSynThreadPool is destroyed in the middle of executing the tasks, the TaskAbort is not called, but in Linux, all works well.

see this, compile it for windows and linux to see the differences: https://gist.github.com/zen010101/def68 … 0b028d70d2

The possible issue might be here:

destructor TSynThreadPool.Destroy;
var
  i: PtrInt;
  endtix: Int64;
begin
  fTerminated := true; // fWorkThread[].Execute will check this flag
  try                      
    {$ifdef USE_WINIOCP}
    // notify the threads we are shutting down
    for i := 0 to fWorkThreadCount - 1 do
      IocpPostQueuedStatus(fRequestQueue, 0, nil, {ctxt=}nil);
      // TaskAbort() is done in Execute when fTerminated = true
    {$else} 

Offline

#6 2024-10-14 12:51:37

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,686
Website

Re: determine whether all Tasks in TSynThreadPool have been executed

The behavior did not change. Previous code did the same.

So it is not a regression, it is a long-standing bug. wink

Edit:
Should be fixed with
https://github.com/synopse/mORMot2/commit/3db4b9e4

Offline

#7 2024-10-14 14:23:16

zen010101
Member
Registered: 2024-06-15
Posts: 70

Re: determine whether all Tasks in TSynThreadPool have been executed

Confirm fixed. smile

Last edited by zen010101 (2024-10-14 14:23:36)

Offline

#8 2024-10-14 19:17:42

zen010101
Member
Registered: 2024-06-15
Posts: 70

Re: determine whether all Tasks in TSynThreadPool have been executed

New bug:

When creating a thread pool and passing a larger thread value in IOCP mode, TSynThreadPool.Free will enter an infinite loop and block the process.         

Here is the demo: https://gist.github.com/zen010101/def68 … 0b028d70d2

Offline

#9 2024-10-14 20:07:11

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,686
Website

Re: determine whether all Tasks in TSynThreadPool have been executed

In fact, there is no infinite loop but a 30 seconds timeout in TSynThreadPool.Destroy.

Please try with
https://github.com/synopse/mORMot2/commit/d8b5ce76

Offline

#10 2024-10-15 04:12:43

zen010101
Member
Registered: 2024-06-15
Posts: 70

Re: determine whether all Tasks in TSynThreadPool have been executed

Confirmed fixed. :-)

Offline

Board footer

Powered by FluxBB