OmniThreadLibrary icon indicating copy to clipboard operation
OmniThreadLibrary copied to clipboard

Parallel.ForEach<> inside Parallel.Future<> seems to leak threads

Open orthopteroid opened this issue 3 years ago • 1 comments

Symptoms:

  • 900+ threads created (only 1 per cpu expected nominally)
  • PostMessage related QUOTA errors (so, threads likely not getting enough time)

What seemed to be the problem was that threads were leaking from using code like:

bool:=Parallel.Future<boolean>(
  function(): boolean
  begin
    Parallel.ForEach<Integer>( list ).NumTasks( System.CPUCount ).Execute( delegate );
    result:=true;
  end );
while not bool.IsDone do
  begin
    Sleep(100);
    { update animated spinner }
  end;

In the end we eliminated the leak by removing the Future all-together, ie hoisting the ForEach up into the main thread, but we lose out on our nice spinner (for now).

Might it be possible that we could prevent the problem and use:

threadgroup:=Parallel.ForEach<Integer>( list ).NumTasks( System.CPUCount );

to create a context to hold the thread references (and allow the other compiler machinery to destroy them) and then use threadgroup inside the Future?

Alternatively, perhaps we will switch to a threadpool.

orthopteroid avatar Jul 11 '22 22:07 orthopteroid

See issue #194. The memory is not leaking, but it seems that the memory has not been released immediately, and repeated calls can lead to memory depletion

achinastone avatar Feb 03 '24 01:02 achinastone