views:

13036

answers:

5

What is the maximum number of threads that can be created by a process under Linux?

How (if possible) can this value be modified?

+4  A: 

To retrieve it:

cat /proc/sys/kernel/threads-max

To set it:

echo 123456789 > /proc/sys/kernel/threads-max

123456789 = # of threads

Vincent Van Den Berghe
What's in /proc depends on the kernel, not the distro.
Paul Tomblin
Thanks, I removed the distro stuff. So I'm guessing this works on all 2.x kernels?
Vincent Van Den Berghe
+7  A: 

Linux doesn't have a separate threads per process limit, just a limit on the total number of processes on the system (threads are essentially just processes with a shared address space on Linux) which you can view like this:

cat /proc/sys/kernel/threads-max

The default is the number of memory pages/4. You can increase this like:

echo 100000 > /proc/sys/kernel/threads-max

There is also a limit on the number of processes (an hence threads) that a single user may create, see ulimit/getrlimit for details regarding these limits.

Robert Gamble
+2  A: 

In practical terms, the limit is usually determined by stack space. If each thread gets a 1MB stack (I can't remember if that is the default on Linux), then you a 32-bit system will run out of address space after 3000 threads (assuming that the last gb is reserved to the kernel).

However, you'll most likely experience terrible performance if you create more than a few dozen threads. Sooner or later, you get too much context-switching overhead, too much overhead in the scheduler, and so on.

What are you doing where this limit is even relevant?

jalf
1MB per thread for stack is pretty high, many programs don't need anywhere near this much stack space. The performance is going to be based on the number of *runnable* processes, not the number of threads that exist. I have a machine running right now with 1200+ threads with a load of 0.40.
Robert Gamble
the performance depends on what the the threads are doing. you can go much higher than a few dozen if they don't do much and hence less context-switching.
Corey Goldberg
default stacksize of thread in linux x86 is 2 Mb
osgx
+4  A: 

It probably shouldn't matter. You are going to get much better performance designing your algorithm to use a fixed number of threads (eg, 4 or 8 if you have 4 or 8 processors). You can do this with work queues, asynchronous IO, or something like libevent.

twk
A: 

use nbio non-blocking i/o library or whatever, if you need more threads for doing I/O calls that block

wefeqfw