Categories

Archives

Is It An Issue To Schedule Too Many Tiny Threads?

A few days ago somebody asked me this question on Quora. The answer was simple and short, but I thought it might be useful to others, so I am publishing it also here, with some additional information.

First of all, let’s provide a definition of thread. The short answer is that a thread is a piece of a process, or a program, that is capable of running independently from the rest of the application. It is basically a section of an application that can run in parallel with the rest of the application. But, does it really run in parallel? Well, on a single CPU that has only one core, the answer is: no. But, on a CPU with multiple cores, the answer is: sometimes, maybe often. The issue here is that, for a thread to actually run in parallel with other programs or with other threads of the same program, it needs to have available resources that it can use: memory, CPU time, I/O (if needed).

The operating system, and in particular the scheduler, takes care of handling the execution of different thread and programs. When there are several threads to execute, it distributes them across the available cores and assigns to each one of them a certain amount of memory to use. Also, when the scheduler suspends a thread to make another one run in its place, so that all the threads get a fair share of run time, it might decide to temporarily swap to disk the memory used by the thread being suspended and extract from the swap file the memory that was saved when the thread that is about to be awaken was using when it was suspended. How the swap file is used depends on the operating system. Different operating systems use different algorithms. But all the operating systems use the swap file when they decide that the RAM is not sufficient to handle all the activities, so some sections of the RAM need to be stored on disk to be able to use that freed RAM for another process or thread.

Based on this information, it is now clear that, in a computer with limited resources, the more threads are being executed, the more memory is used, and so the more are the chances that the swap file is used. Also, if there are more threads than cores, the OS will need to alternate the threads execution to give to each one of them a fair share of execution time.

And with this background, here is the response I gave to the initial question.

How many is too many? It all depends on the number of cores and the amount of memory that is available. Each thread will take away a certain amount of memory from the system. Each thread will try to use a core.

If there are more threads than cores, it will become more difficult for the OS to assign cores to threads. With too many threads, the OS will start having real trouble assigning cores to the threads in a fair way. That’s because the OS will have to run so much to keep switching the threads that it will end up being the one program that uses the most of the CPU. Things may slow down so much that the system could come to a halt.

If there are enough threads to exhaust the memory, the system will start swapping to disk and it will become slower. If there are too many threads, even the swapping space will be exhausted and the system will crash.

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

  

  

  

This site uses Akismet to reduce spam. Learn how your comment data is processed.