2

How many threads can a Windows or Linux system have?

I'm writing a multithreaded portable code which should check for the maximum number of threads in the system.

Marco A.
  • 43,032
  • 26
  • 132
  • 246
  • 2
    See http://stackoverflow.com/questions/344203/maximum-number-of-threads-per-process-in-linux and http://stackoverflow.com/questions/481900/whats-the-maximum-number-of-threads-in-windows-server-2003 for Linux and Windows respectively. – Nikos C. Aug 20 '13 at 08:26
  • It probably varies with the OS, version, resources available on each box, linker settings for stack and other stuff. My Vista 64 box can stagger up to ~7000 threads then refuses to make any more. I found this out by actually making do-nothing, blocked threads in a loop until it didn't work anymore, then terminating them. You really need to know this figure for what reason? :) – Martin James Aug 20 '13 at 08:27
  • 1
    Btw, how many threads are we talking about here? If you intend to have less than one or two dozen threads, then you're fine. No need to check for a limit. What you should do instead is simply check whether spawning a thread succeeded or not. You don't really need to know beforehand. – Nikos C. Aug 20 '13 at 08:32
  • 3
    If you're expecting to get close to the limit then your design is probably wrong... – Sean Aug 20 '13 at 08:36
  • Also remember that the most effective number of threads is number of processor cores. Having less will make some cores to stay unused by your application, having more will make your threads to wait for their turn, and you will loose time for context switching – SpongeBobFan Aug 20 '13 at 09:32
  • @SpongeBobFan incorrect. You missed out the word 'ready'. – Martin James Aug 20 '13 at 09:46
  • 1
    @SpongeBobFan: Well, not always. If your threads are doing any blocking IO, then the CPU core is idle waiting for the blocking IO to complete. Using asynchronous IO for example, would allow the OS to execute a different thread. – Skizz Aug 20 '13 at 10:52
  • 3
    Questions like this should automatically have a link to [If you have to ask, you're probably doing something wrong](http://blogs.msdn.com/b/oldnewthing/archive/2007/03/01/1775759.aspx) added to them! – Skizz Aug 20 '13 at 10:54
  • Have you ever thought that maybe I need to check for consistency of a user's input? Have you? – Marco A. Aug 20 '13 at 11:43

3 Answers3

3

I'm pretty certain that if you actually "need to know this", your design is bad. All modern OS's can support several thousand threads.

The limits are typically more about available memory and CPU resources than "what number of threads can you create in system X" - in other words, if your threads actually do something, and actually use more than a smidgeon of memory, the system will run out of memory before the theoretical maximum number of threads have been created.

For example, on my machine /proc/sys/kernel/threads-max is over 250000 - my machine has 16GB of ram, so per thread there is about 64KB. And that is if ALL the memory is actually available for threads... Some of it will be kernel code, filesystem buffers, etc.

So in any practical terms, memory is going to be your limit, not the theoretical number of threads that the system can support.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
2

For Windows, TechNet has an article by Mark Russinovich Pushing the Limits of Windows: Processes and Threads. The article also links to the testlimit tool, which lets you experiment on your system.

The TL;DR version is that it depends on Windows version, whether you're running 32- or 64-bit Windows, and whether your application is 32- or 64-bit, and for a 32-bit program whether you're linking your program as "large-address aware" (and the /3GB NTLDR option). It may also depend on ASLR. And it depends on whether you use the default thread stack reserve size, or adjust it.

A 32-bit non-large-adress-aware process running on a 32bit system without the /3GB switch, and the default 1meg stack reserve, will be limited to at most 2048 threads, but you'll very likely hit memory limits before that.

For a 64bit process with an adjusted stack size, you can probably expect hitting 40-50k threads with 2GB ram.

At any rate, if you're even close to hitting the 32-bit limits, you're Doing Things Wrong(TM). You don't want to be using blocking I/O with a thread per request - look into async I/O and thread pools (and all the fancy abstractions built on top of those).

snemarch
  • 4,958
  • 26
  • 38
0

Linux, it's pretty easy. cat /proc/sys/kernel/threads-max

Windows, less so. Basically it depends on how much ram you have / how much you allocate on each thread's stack - see What's the maximum number of threads in Windows Server 2003?; but basically <1000

Community
  • 1
  • 1
Oliver Matthews
  • 7,497
  • 3
  • 33
  • 36
  • `/proc/sys/kernel/threads-max` is the global thread limit and can't be treated as a per-process limit. For example, if it says 20000 and there's currently 19999 threads globally, your process can't spawn any threads at all other than the main one. – Nikos C. Aug 20 '13 at 08:28
  • <1000? That doesn't sound all that many. The desktop Vista box I'm using to post this has 1070 threads ATM. – Martin James Aug 20 '13 at 08:31
  • @NikosC. Yes, but he did ask specifically about system limits. – Oliver Matthews Aug 20 '13 at 08:58
  • @MartinJames A fair point, the referenced link is from 2009 and the underlying point is that (practical) thread count is ram limited more than anything else. – Oliver Matthews Aug 20 '13 at 08:59