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?
Asked
Active
Viewed 4.0k times
30
-
Have you tried https://www.google.de/#q=What+is+the+maximum+number+of+threads+a+process+can+have+in+windows already? – Sebastian Mach May 23 '13 at 09:58
-
1I tested this. My 64-bit Vista ultimate refuses to create any more than ~7000 threads, cannot remember exact figure. – Martin James May 23 '13 at 11:53
-
16This 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? – omatai Mar 14 '17 at 00:12
2 Answers
25
There is no limit that I know of, but there are two practical limits:
- 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
inCreateThread
or option/STACK
in the linker command). If you use a 64-bits system this limit practically dissapears. - 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.

rodrigo
- 94,151
- 12
- 143
- 190
-
5Well, 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. – Martin James May 23 '13 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 '13 at 12:57
-
1IIRC, 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? – Martin James May 24 '13 at 09:28
-
..however, not tried it with ready threads. I will try to do so :) – Martin James May 24 '13 at 09:30
-
NOt tried it with 'Sleep 1000'loops either, IIRC. Again - will try it on my Vista 64. – Martin James May 24 '13 at 09:32
-
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: https://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 '20 at 06: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

Mats Petersson
- 126,704
- 14
- 140
- 227
-
4Archive link (original is 404) : https://web.archive.org/web/20121117072019/http://blogs.technet.com/b/markrussinovich/archive/2009/07/08/3261309.aspx – SushiHangover Jul 26 '20 at 10:14