Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# thread count

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?

like image 406
Yippie-Ki-Yay Avatar asked Apr 26 '11 22:04

Yippie-Ki-Yay


1 Answers

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.

like image 118
Kiril Avatar answered Nov 04 '22 01:11

Kiril