Using Delphi 10.3.2 I've encountered the following problem (specifically on Windows, not sure about other platforms):
After using TParallel or TTask, not all threads in the thread pool return to idle as expected. I've prepared the following simple example:
procedure TForm.Button1Click(Sender: TObject);
var
SW:TStopWatch;
procedure Load;
begin
TParallel.For(0, 99999999, procedure(i: Integer)
var
T:Single;
begin
T:=Sin(i/PI);
end);
end;
begin
Memo1.Lines.Add('PPL Test ---------------');
Memo1.Lines.Add('Before: '+TThreadPoolStats.Current.Formatted);
SW:=TStopWatch.StartNew;
Load;
Memo1.Lines.Add('Finished in '+SW.Elapsed.ToString);
Memo1.Lines.Add('After: '+TThreadPoolStats.Current.Formatted);
Memo1.Lines.Add('------------------------');
end;
Repeatedly pressing the button results in the following output:
PPL Test ---------------
Before: 0 Worker Threads (56 .. 1400) / 0 Idle / 0 Retired / 0 Suspended / Avg. CPU = 0 / Curr. CPU = 0
Finished in 00:00:09.8423686
After: 56 Worker Threads (56 .. 1400) / 43 Idle / 0 Retired / 0 Suspended / Avg. CPU = 30 / Curr. CPU = 100
------------------------
PPL Test ---------------
Before: 56 Worker Threads (56 .. 1400) / 43 Idle / 0 Retired / 0 Suspended / Avg. CPU = 36 / Curr. CPU = 0
Finished in 00:00:04.7841342
After: 65 Worker Threads (56 .. 1400) / 50 Idle / 0 Retired / 0 Suspended / Avg. CPU = 43 / Curr. CPU = 50
------------------------
PPL Test ---------------
Before: 65 Worker Threads (56 .. 1400) / 50 Idle / 0 Retired / 0 Suspended / Avg. CPU = 42 / Curr. CPU = 0
Finished in 00:00:04.7090980
After: 74 Worker Threads (56 .. 1400) / 55 Idle / 0 Retired / 0 Suspended / Avg. CPU = 42 / Curr. CPU = 48
------------------------
PPL Test ---------------
Before: 74 Worker Threads (56 .. 1400) / 55 Idle / 0 Retired / 0 Suspended / Avg. CPU = 38 / Curr. CPU = 0
Finished in 00:00:02.3904253
After: 74 Worker Threads (56 .. 1400) / 50 Idle / 0 Retired / 0 Suspended / Avg. CPU = 57 / Curr. CPU = 98
------------------------
PPL Test ---------------
Before: 74 Worker Threads (56 .. 1400) / 50 Idle / 0 Retired / 0 Suspended / Avg. CPU = 51 / Curr. CPU = 0
Finished in 00:00:05.2865123
After: 84 Worker Threads (56 .. 1400) / 55 Idle / 0 Retired / 0 Suspended / Avg. CPU = 44 / Curr. CPU = 41
------------------------
PPL Test ---------------
Before: 84 Worker Threads (56 .. 1400) / 55 Idle / 0 Retired / 0 Suspended / Avg. CPU = 36 / Curr. CPU = 0
Finished in 00:00:06.8032435
After: 96 Worker Threads (56 .. 1400) / 64 Idle / 0 Retired / 0 Suspended / Avg. CPU = 36 / Curr. CPU = 32
------------------------
PPL Test ---------------
Before: 96 Worker Threads (56 .. 1400) / 64 Idle / 0 Retired / 0 Suspended / Avg. CPU = 0 / Curr. CPU = 0
Finished in 00:00:07.7921758
After: 111 Worker Threads (56 .. 1400) / 77 Idle / 0 Retired / 0 Suspended / Avg. CPU = 27 / Curr. CPU = 23
------------------------
PPL Test ---------------
Before: 111 Worker Threads (56 .. 1400) / 77 Idle / 0 Retired / 0 Suspended / Avg. CPU = 20 / Curr. CPU = 0
Finished in 00:00:02.3559000
After: 111 Worker Threads (56 .. 1400) / 77 Idle / 0 Retired / 0 Suspended / Avg. CPU = 48 / Curr. CPU = 100
------------------------
PPL Test ---------------
Before: 111 Worker Threads (56 .. 1400) / 77 Idle / 0 Retired / 0 Suspended / Avg. CPU = 48 / Curr. CPU = 0
Finished in 00:00:06.6491982
After: 122 Worker Threads (56 .. 1400) / 87 Idle / 0 Retired / 0 Suspended / Avg. CPU = 33 / Curr. CPU = 100
------------------------
Even after the first run, only 43 threads in the pool seem to have returned to idle. Instead, with almost every further run, the total number of worker threads is increased to make up the difference. Despite this, the total CPU usage is going down slowly.
If I wait for the inactive threads to be automatically retired and run the test again, I get something like this:
PPL Test ---------------
Before: 0 Worker Threads (56 .. 1400) / -36 Idle / 0 Retired / 0 Suspended / Avg. CPU = 0 / Curr. CPU = 0
Finished in 00:00:09.5530676
After: 56 Worker Threads (56 .. 1400) / 20 Idle / 0 Retired / 0 Suspended / Avg. CPU = 30 / Curr. CPU = 100
------------------------
A negative number of idle threads? And after the run, only 20 threads are stated as idle again. Further runs result in the behavior described above.
If someone could shed some light on all this for me, I would highly appreciate it! Am I doing something wrong here? What do I need to do to get 100% CPU usage on every run, without the thread pool ballooning?
Thanks!