## DEV Community π©βπ»π¨βπ» is a community of 963,864 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Valentin Sawadski (he/him)

Posted on

# Hardware 101 - CPUs, Cores and Threads

In this post, I'm going to give you a beginner friendly introduction into how modern CPUs (Intel, Apple, AMD, ARM, etc.) work.

## CPUs

Let's start with the CPU. The CPU is often referred to as the "Brain" of your computer, but what does this mean? The CPU does most of the computation on your computer. It runs your Web browser, Office etc. Basically everything except Graphics & Machine Learning, this usually runs on the GPU. The difference between the CPU and GPU is that the CPU is "good" for a generic computing. The GPU is a highly specialized chip which is GREAT for certain tasks like Graphics & Machine Learning, but also much more expensive. Other then than the Brain, neither the CPU nor the GPU actually store any data. They just fetch it from memory, process it (e.g. math calculations) and write it back to memory.

A CPU has multiple parts,

• a "Core" that does the computation
• a "Cache" which you can imagine like super small but super fast RAM
• and a Memory I/O so that me CPU can read and write from RAM and your hard drive.

## Cores

In the beginning each CPU had only one "Core" but modern CPUs have multiple cores. For example, I'm writing this post on an 2 GHz Intel i5 CPU with 4 cores. Other CPUs like the AMD Ryzen 7950x have 16 cores!

But what does it mean if a CPU has more cores? Can it then "do more" as well? Yes, and No. Assume we have two identical Computers, one with 16 cores and one with 4 cores. Are there programs that the 4 core CPU can't run? No. If there are more tasks then cores, your OS will "time share". "Time Share" is when two or more programs share the use of a Core. Time slots are super small, < 1ms per program, so that programs switch all the time, making it seem to humans as if they are running in parallel, when in fact they are running sequentially. This is a normal thing to do for computers. E.g. My computer has 4 cores, but I have 7 programs running. (+ 500 more processes running in the background). So more cores will not allow you to do "more" things. They just allow you to run more things truly parallel.

So how many cores do you need? This depends on the type of Software you are using. Computationally intensive application like gaming, or math simulations etc. may run faster if you CPUs with more Cores. Still CPUs with less cores do fine most of the time. As I said, my laptop has only 4 cores, and they are idle 80% of the time while I write this Post.

## Threads

Modern Cores have a special feature called "Multi-Threading". To explain this we first need to understand what "Processes" and "threads" are.

Examples of Processes are your Music Player App and your Email-App. Processes can run in parallel and should be strictly separated by the Operating System and Hardware. We don't want the Email-App to mess up the Music Player or the other way around.

But looking at the Music player there is "parallel" computing going on in the Music player itself. You can keep listening the the music while the User Interface reacts to your input while you browse your library. This is done by using multiple "Threads" which run inside the Music Player process. One thread keeps playing the music, while the other thread takes care of the user interface. Inside the Music Player we want Threads to be able to exchange information easily, so the player starts playing a new song as soon as you double click it.

As threads are quite common, modern CPU Cores have Hardware Support for it, called Multithreading.

## More Cores & Threads != More Performance

Multi-core CPUs and Multithreading can increase performance of your computer, but only if you use them right.

Imagine a highway

• CPU clock speed = speed limit
• Cores/Threads = Amount of Lanes

Adding a core/thread potentially allows more cars to go through in parallel. But itβs not going to make the individual car go faster. The overall throughput then depends on your βtrafficβ. Have loads of individual cars? More cores can help. Need to get one single car from A to B? Then no.

By default processes start with a single thread. It's up to the programmer to write programs that create child processes or multiple threads so they can take advantage of multi-core and multithread CPUs.

Once you have implemented threading in your program, a piece of the Operating System called the "Scheduler" will take over. Each modern operating system has a "Scheduler" which decides which process and thread gets executed next, based on available CPU resources and their priority. For example with our Music player app, we need the Thread playing the music to have high priority, otherwise the sound comes out choppy. For the User Interface however we could use a lower priority, because worst case it takes a few milliseconds longer to load, but users will not mind this as much.

## Summary

I hope this little summary helps you to understand the differences between CPUs, Cores and Threads! If you still have some questions or would like to learn more drop me a question in the comments!

This a longer, more readable version of a Thread I posted on Twitter recently. I don't always find the time to post all of my Twitter threads on Dev.To, so follow me on Twitter @_vsaw if you don't want to miss out on anything!

## π Friends don't let friends browse without dark mode.

Sorry, it's true.