开发者

How to find the max no of threads spawned by one system?

Is there a way / program to find out the maximum no of threads a system can spawn ? i am creating a application and i am in a dilemma whether to go with event looping model or multi threaded model . so wanted to test the systems 开发者_如何学JAVAcapabilities on how many threads it can handle ?


The "maximum number of threads" is not as useful a metric as you might think:

  • There is usually a system-wide maximum number of threads imposed by either the operating system or the available hardware resources.

  • The per-process maximum number of threads is often configurable and can even change on-the-fly.

  • In most cases the actual restriction comes from your hardware resources - rather than any imposed limit. Much like any other resource (e.g. memory) you have to check if you were successfull, rather than rely on some kind of limit.

In general, multi-threading has only two advantages when compared to event loops:

  • It can utilise more than one processor. Depending on the operating system you can also use multiple processes (rather than the more lightweight threads) to do that.

  • Depending on the operating system, it may offer some degree of privlilege separation.

Other than that multi-threading is usually more expensive in both memory and processing resources. A large number of threads can bring your system to a halt regardless if what they are doing is resource-intensive or not.

In most cases the best solution is a hybrid of both models i.e. a number of threads with an event loop in each one.

EDIT:

On modern Linux systems, the /proc/sys/kernel/threads-max file provides a system-wide limit for the number of threads. The root user can change that value if they wish to:

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

As far as I know, the kernel does not specifically impose a per-process limit on the number of threads.

sysconf() can be used to query system limits. There are some semi-documented thread-related query variables defined in /usr/include/bits/confname.h (the _SC_THREAD* variables).

getrlimit() can be used to query per-session limits - in this case the RLIMIT_NPROC resource is related to threads.

The threading implementation in glibc may also impose its own limits on a per-process basis.

Keep in mind that, depending on your hardware and software configuration, none of these limits may be of use. On Linux a main limiting factor on the number of threads comes from the fact that each thread requires memory in the stack - if you start launching threads you can easily come upon this limit before any others.

If you really want to find the actual limit, then the only way is to start launching threads until you cannot do so any more. Even that will only give you a rough limit that is only valid at the time you run the program. It can easily change if e.g. your threads start doing actual work and increase their resource usage.

In my opinion if you are launching more than 3-4 threads per processor you should reconsider your design.


On Linux, you can find the total number of threads running by checking /proc/loadavg

    # cat /proc/loadavg
    0.02 0.03 0.04 1/389 7017

In the above, 389 is the total number of threads.


it can handel as many threads as provided by OS and you never know the limit.

But as a general measures if a normal LOB application having more than 25 threads at a time can lead to problems and have serious design issue.


The current upper global limit is 4 million threads, because once reached, you have run out of PIDs (see futex.h).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜