C # Threading, GetAvailableThreads ()

I have a problem with threads,

I set it ThreadPool.SetMaxThreads(maxThreads, System.Environment.ProcessorCount)

to 10.

But when I check how many of them are available ThreadPool.GetAvailableThreads()

, it says there is (maxThreads - 1), so 9, but then keeps using 10 threads.

Any ideas why this is?

Thanks for the help.

+1


source to share


3 answers


In an ideal world, we would have 1 thread / physical core. This way we no longer have context switches, which are quite expensive operations. But as long as we have processors with hundreds of cores, this will not be practical.



In any case, as Mark suggested, you shouldn't mess with the ThreadPool parameters unless you REALLY know what you are doing. The implementation logic of the .Net ThreadPool implementation is pretty good and can handle most scenarios successfully.

+1


source


Are you queuing up streams with something similar?

ThreadPool.QueueUserWorkItem(callback, obj)

      

and then do something like:



WaitHandle.WaitAll(WaitHandle)

      

I am assuming there is a problem with how you actually calculate the threads that work, but without a more specific explanation of what you are doing, this is just an assumption.

+1


source


One of the threadpool threads controls the wait operations of other threads in the thread pool. This should explain the result of maxthreads - 1.

I'm not sure why it will use all 10 threads. Maybe he uses 9 for your work, 1 for monitoring and queuing others.

0


source







All Articles