Parallel.ForEach<> inside Parallel.Future<> seems to leak threads
orthopteroid opened this issue · comments
John Howard commented
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.