30

In a windows process is there any limit for the threads to be used at a time. If so what is the maximum number of threads that can be used per process?

3
  • Have you tried google.de/… already? May 23, 2013 at 9:58
  • 1
    I tested this. My 64-bit Vista ultimate refuses to create any more than ~7000 threads, cannot remember exact figure. May 23, 2013 at 11:53
  • 16
    <rant>This is not just a "real question", it is a valuable one. It is not immediately obvious that the limit is not 16 or 32 or some uselessly small number. Moreover, it is not the slightest bit "difficult to tell what is being asked here", and nor is there ambiguity, vagueness, etc. I wish people closing such questions reflected a little more on the value that questions may provide to others that they do not provide to themselves. This question asked exactly what I wanted to know, and the answers were very helpful. How else were we supposed to know it - via ESP? Experience? </rant>
    – omatai
    Mar 14, 2017 at 0:12

2 Answers 2

25

There is no limit that I know of, but there are two practical limits:

  1. The virtual space for the stacks. For example in 32-bits the virtual space of the process is 4GB, but only about 2G are available for general use. By default each thread will reserve 1MB of stack space, so the top value are 2000 threads. Naturally you can change the size of the stack and make it lower so more threads will fit in (parameter dwStackSize in CreateThread or option /STACK in the linker command). If you use a 64-bits system this limit practically dissapears.
  2. The scheduler overhead. Once you read the thousands of threads, just scheduling them will eat nearly 100% of your CPU time, so they are mostly useless anyway. This is not a hard limit, just your program will be slower and slower the more threads you create.
6
  • 5
    Well, depends on thread state. 6000 blocked threads has zero effect on the operation of my machine. Also, I cannot see how, even if the threads were ready/running, it would matter much if I had 16 threads or 1600 - my 4/8 cores would be maxed out in either case. May 23, 2013 at 11:52
  • 1
    @MartinJames: Because the algorithms used by the kernel scheduler to decide what to run next scale with the size of the thread table. Undoubtedly the details vary with the Windows version, but I remember creating 2000 threads in WinXP doing just for(;;) Sleep(1000) and the CPU use was over 80%.
    – rodrigo
    May 23, 2013 at 12:57
  • 1
    IIRC, the readdt thread structure is basically a priority-ordered array of queues of thread info block pointers. If one queue has 2000 entries but there are only four cores, shuttling round the threads on a timer interrupt just requires pushing the 'old' four threads at the back and popping four 'new' ones from the front - doesn't really matter how long the queue is. That's not gonna take long? May 24, 2013 at 9:28
  • ..however, not tried it with ready threads. I will try to do so :) May 24, 2013 at 9:30
  • 1
    @rodrigo Don't know about XP but in windows 10, I've created 1500 threads and I didn't even have 2% of CPU usage: i.vgy.me/Ud7bP2.png I've also created the threads using for (int i = 0; i < 1500; ++i) new std::thread(TestThread); and void TestThread() { while (true) Sleep(1000); } I've also checked Task Manager, there's no cpu usage at all. I'm also sure that all my threads are running and are good.
    – rez
    Dec 28, 2020 at 6:07
7

The actual limit is determined by the amount of available memory in various ways. There is no limit of "you can't have more than this many" of threads or processes in Windows, but there are limits to how much memory you can use within the system, and when that runs out, you can't create more threads.

See this blog by Mark Russinovich: http://blogs.technet.com/b/markrussinovich/archive/2009/07/08/3261309.aspx

1

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.