简体   繁体   中英

Windows and linux max threads

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.

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.

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).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM