Make delicious recipes!

How many threads should/can be run?

Multithreading will be of no use in single-core systems unless some threads had to wait for file/network I/O, DB query completions, UI input or they had to wait for other threads to produce/consume.

This is because in a single core system, only one thread is active at a time.

In a multicore system, true parallelism is achievable because there multiple threads will really run simultaneously. However, there also, the number of parallel executions depends upon the number of cores. So a quad core system will run only 8 parallel threads.

Assuming, threads involve waiting and the system on which they run is multi-core, what is the maximum number of threads which can run on a system?

Every thread is given some specific amount of stack-size to execute (heap is shared by threads of same process, so that is not a constraint). That stack-size per thread is configurable and the default is different in different systems. The default stack-size per thread varies from 1 MB to 8 MB. If the machine on which the threads are running is a 32-bit machine, then it can access a memory space of 2^31=2GB.

So, the maximum no of threads which can be created = 2GB/8MB ~ 256 threads.

If you try to create more threads than this, then out-of-memory error will occur.

Mathematically, number of threads = total virtual memory / (stack size*1024*1024)

On linux systems, these values can be checked as:

$ ulimit -a

core file size (blocks, -c) 0

data seg size (kbytes, -d) unlimited

file size (blocks, -f) unlimited

max locked memory (kbytes, -l) unlimited

max memory size (kbytes, -m) unlimited

open files (-n) 1024

pipe size (512 bytes, -p) 8

stack size (kbytes, -s) 8192

cpu time (seconds, -t) unlimited

max user processes (-u) unlimited

virtual memory (kbytes, -v) unlimited

Changing stack-size of a thread

1. “ulimit” command, for example ulimit -s 192 will reduce the stack size from 8MB to 192kb.

2. C applications can call pthread_attr_setstacksize().

3. Java applications can use “-Xss” to set the stack size.

Remember that switching thread context is quite expensive and so adding threads to a system does not linearly scale the performance of the system.

Also, maximum no of threads depends upon the maximum no of open files. As seen above in the command ‘ulimit -a’, system places a limit on the max no of open files too. If each of your threads opens a file, then this limit can also kill the application.

Similar restrictions may be placed by other systems like maximum no of connections imposed by a database etc.

Like us on Facebook to remain in touch
with the latest in technology and tutorials!

Got a thought to share or found a
bug in the code?
We'd love to hear from you:

Email: (Your email is not shared with anybody)

Facebook comments:

Site Owner: Sachin Goyal