Whenever we run programs on our computers, we start up something called a process. A process is an instance of a running program that is being executed. within a single process, we can have multiple things called threads and we can picture a thread like a todo list that consists of instructions to be executed by the CPU and the CPU will attempt to run every program on it one by one from top to bottom.
A single process can have multiple threads and one of the most important things about studying threads is called scheduling. Scheduling refers to the process where your CPU decides which process to run at any given point in time. Remember your computer has a limited amount of resources available to it and the CPU can only process a limited amount of instructions per seconds this becomes relevant when we have so many active threads in a process and each of these threads may have urgent instructions that need to be processed so the OS Scheduler has to look at all these threads and figure out how to do some amount of work on them and ensuring that they don't stay too long. We want to make sure that urgent threads don't have to wait too long to be executed for example, if the thread responsible for moving a mouse around the screen has to wait for like 10s to be executed the cursor will not move during that time
and the user will think that the computer is broken which is not good.
There are a couple of strategies that are used to increase the rate at which threads can be processed and I will focus on 2 which will end up very relevant to us in the Nodejs world.
In other for CPU to processed more than one threads at any given point in time, we can:
- Add more additional CPU Cores to our machine. So if we have more than one core in our CPU then we can easily process multiple threads at the same time.
Technically, one core can process more than one thread at a time through a process called multithreading (which is the ability of a central processing unit (CPU) to provide multiple threads of execution concurrently) but for now, let just say that one core can process a single thread at a time.