What would be the appropriate way to setup the ThreadPool
in C#
?
ThreadPool.SetMaxThreads(int workerThreads, int completionPortThreads)
I want the number of threads to equal the number of processor cores, but I'm not sure about the async I/O threads (the second parameter).
Can I simply say something like ThreadPool.SetMaxThreads(Environment.ProcessorCount, 0)
so that the O/S deduces the second parameter itself?
The max completion port threads is used by asynchronous tasks, such as Socket.Begin*
or Socket.*Async
and other IO-related tasks. I would recommend that if you change the worker threads, then you should just check what is the current max number of completion threads prior to changing the values:
int maxWorkerThreads;
int maxCompletionThreads;
// Get the maximum number of completion threads
ThreadPool.GetMaxThreads(out maxWorkerThreads, out maxCompletionThreads);
// Set the new max worker threads, but keep the old max completion threads
ThreadPool.SetMaxThreads(someDifferentValue, maxCompletionThreads);
I think that by default the maximum number of completion threads is 1000, but that may vary from machine to machine. Just to be safe, use the value that you obtain from the GetMaxThreads
function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With