Skip to content

Commit

Permalink
#2955: Ensure minimum thread pool size (v2)
Browse files Browse the repository at this point in the history
  • Loading branch information
bradwilson committed Jun 22, 2024
1 parent 5fe01ff commit b6dabde
Showing 1 changed file with 6 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -318,18 +318,20 @@ private void SetupParallelSemaphore(int maxParallelThreads)
var minThreads = (int)args[0];
var minIOPorts = (int)args[1];

if (minThreads < maxParallelThreads)
var threadFloor = Math.Min(4, maxParallelThreads);
if (minThreads < threadFloor)
{
var setMethod = type.GetRuntimeMethod("SetMinThreads", new[] { typeof(int), typeof(int) });
if (setMethod is null)
throw new InvalidOperationException("Cannot find method: System.Threading.ThreadPool.SetMinThreads");

setMethod.Invoke(null, new object[] { maxParallelThreads, minIOPorts });
setMethod.Invoke(null, new object[] { threadFloor, minIOPorts });
}
#else
ThreadPool.GetMinThreads(out var minThreads, out var minIOPorts);
if (minThreads < maxParallelThreads)
ThreadPool.SetMinThreads(maxParallelThreads, minIOPorts);
var threadFloor = Math.Min(4, maxParallelThreads);
if (minThreads < threadFloor)
ThreadPool.SetMinThreads(threadFloor, minIOPorts);
#endif
}

Expand Down

0 comments on commit b6dabde

Please sign in to comment.