What is the maximum number of threads that can be created by a process under Linux?
How (if possible) can this value be modified?
What is the maximum number of threads that can be created by a process under Linux?
How (if possible) can this value be modified?
To retrieve it:
cat /proc/sys/kernel/threads-max
To set it:
echo 123456789 > /proc/sys/kernel/threads-max
123456789 = # of threads
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.
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?
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.
use nbio non-blocking i/o library or whatever, if you need more threads for doing I/O calls that block