gabr42 / OmniThreadLibrary

A simple and powerful multithreading library for Delphi

Home Page:http://www.omnithreadlibrary.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

orthopteroid opened this issue · 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.

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