Maximum Number of Threads in a .Net App

Maximum number of threads in a .NET app?

There is no inherent limit. The maximum number of threads is determined by the amount of physical resources available. See this article by Raymond Chen for specifics.

If you need to ask what the maximum number of threads is, you are probably doing something wrong.

[Update: Just out of interest: .NET Thread Pool default numbers of threads:

  • 1023 in Framework 4.0 (32-bit environment)
  • 32767 in Framework 4.0 (64-bit environment)
  • 250 per core in Framework 3.5
  • 25 per core in Framework 2.0

(These numbers may vary depending upon the hardware and OS)]

C# Maximum Number of Threads

Yes, you can (and likely should) create more threads than there are cores, because the operating system scheduler will interupt to swap among active threads, even if nothing else is waiting on that core. However, you also don't want to create 1000 separate threads. Instead, create a number of threads and divide the work among them, so that each thread handles more than one item from the full job.

I've found that a good rule of thumb is to go for two threads per logical core (this counts hyper-threaded cores... if you have an 8-core cpu with hyperthreading for 16 logical cores, create 32 threads). The idea is you want as few scheduler interupts/context swaps as possible, but at the same time keep all of the cores busy working mainly on your task. Given scheduler interupts will still occur, even if nothing else is active on the logical core, having two active threads for that core means it's likely for the scheduler to just put in the idle thread from your program. Even if other stuff is active for that core, it's now still likely to be your thread chosen for execution. Going higher than this can encourage more context switches than is necessary and hurt performance.

The short version is the cost of a second thread per core is low (because the context switches still happen), but the payoff is potentially high (entire scheduler blocks where the cpu is working on your app instead of something else). As you add more threads per core, you start to increase the costs and decrease the potential benefit.

But that's just my (very limited) experience. It's extremely generalized, and not good for much more than a starting point. You really need to profile how your app behaves with different thread numbers to get an idea of how to tune this for best performance.

Finally, in the .Net world it's worth mentioning the ThreadPool and async Tasks. This isn't the best place to get into a full tutorial on those subjects, but reading up on them is well worth your time.

Maximum number of Threads available to Tasks

So, it turns out that the issue I was seeing had to do with the threadpool size. This is apparently initially set to the number of cores of the machine (https://msdn.microsoft.com/en-us/library/system.threading.threadpool.getminthreads%28v=vs.110%29.aspx).

It can be increased, and doing so means that more of the tasks are initially run simultaneously (https://msdn.microsoft.com/en-us/library/system.threading.threadpool.setminthreads%28v=vs.110%29.aspx)

Maximum number of threads in a Metro App

I think that there is no limit. The maximum number of threads is determined by the amount of resources available.

And, as Henk said, if you need to ask what the maximum number of threads is, you are probably doing something wrong.

Recommended number of threads in a Winforms application?

There is no limit in the system or in the .NET Framework.

But what are your goals ? often, you use threads because you want your program works during idle time of the main thread (a background thread). So, in this case, use a GUI thread and a background worker thread.
Il you want to do a lot of things at the same moment, use threads, but play with their priority (http://msdn.microsoft.com/fr-fr/library/system.threading.thread.priority.aspx)

Clarification on thread pool max threads

I have looked at source code and have found that default value for MaxWorkerThreads is set to 100

private static readonly ConfigurationProperty _propMaxWorkerThreads = new ConfigurationProperty("maxWorkerThreads", typeof (int), (object) 100, (TypeConverter) null, (ConfigurationValidatorBase) new IntegerValidator(1, 2147483646), ConfigurationPropertyOptions.None);

This field is added to properties collection in static constructor

ProcessModelSection._properties.Add(ProcessModelSection._propMaxWorkerThreads);

In property definition they do set default value to 20

[IntegerValidator(MaxValue = 2147483646, MinValue = 1)]
[ConfigurationProperty("maxWorkerThreads", DefaultValue = 20)]
public int MaxWorkerThreads

But this obviously give no effect. Maybe it's some kind of legacy implementation. By the way it behaves this way only if autoConfig is set to false. When it's set to true I have 32K worker threads in my application. Probably this behavior depends on IIS version.

c# Threadpool - limit number of threads

I would use Parallel.For and set MaxDegreeOfParallelism accordingly.

Parallel.For(0, 1000, new ParallelOptions { MaxDegreeOfParallelism = 10 },
i =>
{
GetPage(pageList[i]);
});


Related Topics



Leave a reply



Submit