DEV Community

loading...

The Basics of Threads

cpuram1 profile image CALVIN JOB PURAM Updated on ・3 min read

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.

Alt Text

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

Alt Text

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.

Alt Text

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.

  • The second thing we can do is to look closely at each work that the threads do this is where things become more relevant to us in the javascript world. Let's imagine that we have two threads that are competing for processing power the first thread read a file from the hard disk and count the number of letters in it which usually takes time so during this process, the CPU will be waiting on the hard drive to read some files and return context and during that time thread 1 has nothing to do just waiting for the hard drive. The **OS Scheduler has the ability to detect this downtime between instructions and can decide to put the first thread on pause and then execute the second thread and then as soon as thread 2 is completed it can then go back to the first thread and continue waiting for the hard drive to return context. Now that we understand we basics of threads next we look at Nodejs single thread and the thread pool.

Discussion (0)

Forem Open with the Forem app