c# – Maximum Number of Simultaneous Threads

Question:

What is the maximum number of threads that can be created that run concurrently?

Answer:

There are several "concepts" for a process/thread.

Physically – no more than the number of processor cores. Those. if the processor is 4-core, it cannot execute more than 4 threads at the same time. There is a Hyper-threading technology, the essence of which is the optimization of "switches" between processes, i.e. Hyper-Threading 4-8 does not mean that 8 are running at the same time, but means that switching between threads is organized in such a way that it seems that there are more cores.

Logically (OS level) – until the handle memory runs out (i.e. a lot). Your question boils down to the max number of handles (because each thread needs to be assigned a handle) then the answer is up to 10000 (minus 300 pieces is used by the system). OS "transforms" active handles into tasks, and the processor selects by hardware decision which task to execute now at the hardware level. Handles can be – files, pipes, event, mailslot and other OS objects, if the program actively uses handles (for example, a file is opened for each thread), then the handles will end twice as fast, because. the total number of hanls must not exceed 10,000.

DOT NET level. In c#, a Thread is an object that may or may not have a real Thread Handle behind it. In addition, handles are pooled (for reuse). An active Thread is always backed up by the OS's Thread Handle, but inactive ones can be created – until the Heap memory runs out. If a certain number of active Thread is exceeded, we will get an error that it is not possible to create a handle.

hardware level. TSS or Task Gate Descriptor For 32 bit processors, there is a GDT catalog – a table of tables with 8192 cells. Each can store a reference to 8192 LDT elements, one of which can be a handle to the Task process (part of Thread without which the processor will not be able to switch Thread hardware. That is, 67 108 864 is the "theoretically" limit of hardware places for threads, but … you need to take into account that memory descriptors also need to be placed in this table, i.e. minus memory descriptors … it will come out from a million to 60 million. But in reality, there is a limit above which the processor will be "nothing else busy except switching processes, "that's why … they don't use so much. The processor distributes these tasks between the cores with a hardware solution. What the OS will do when they run out – most likely the OS has a built-in constant limit on the number of tasks, since if there are too many of them the processor will start to lose performance.

Hardware links:

Scroll to Top