In a Nutshell: Understanding Thread

Nayanda Haberty
Nerd For Tech
Published in
7 min readOct 9, 2023

Let’s talk about computers, those amazing machines that can do a zillion things super quickly. Ever wondered how they pull off the magic of doing lots of stuff at once, especially in the old days when they had just one core to work with? Today, we’re gonna chat about CPUs (you know, the brains of your computer) and the threads. Don’t worry; we’ll keep it chill, so it’s easy to get.

Nature of CPU (Central Processing Unit)

Let’s start with the star of the show, the CPU — the brains behind your computer’s operations. In the past, the CPU could only handle one task at a time, which worked well when it came to simple jobs like crunching numbers or playing music. However, as computers grew more powerful and began juggling tasks such as playing music while you typed up a document and keeping your screen responsive, a clever solution was needed. Enter threads.

Its start with a Process

Think of a computer program as the main character in a story, and this character is the “process.” But here’s the fun part: this main character can have buddies, which are like threads. These buddies help out with different parts of the story, and they all hang out together in the same world, like characters in a book.

Process

These processes are like separate little worlds. They don’t mess with each other’s stuff, and they even have their own secret names (we call them Process IDs or PIDs) to tell them apart. They get their own piece of the computer’s brain (CPU), memory, and all the tools they need to work correctly.

When a program starts, a process is born, and when it’s done doing its job, the process goes to sleep. So, these processes are the unsung heroes that make your computer run all those programs at once!

Threads: The Building Blocks of Multitasking

Threads are like the MVPs of multitasking in your computer. They’re the ones that let it do a bunch of things at once. These little champs have unique IDs, personalities, brainy spots (registers), and a magical backpack (stack) to keep things rolling.

Thread

Thread Attributes:

Thread attributes are like the special skills in a video game. Each thread gets its own ID, a priority level (like a player’s skill), and a mood (state) saying if it’s busy, waiting, or chilling.

Registers:

Registers are like a thread’s brainy core. They jot down what’s next (Program Counter), where they’re at (Stack Pointer), and have some spots for math and data magic. There’s even a mood ring (Status Register) for decision-making.

Stack:

A thread’s stack is like a magical backpack. It keeps track of where it’s been, what it’s carrying (local stuff), and how to get back home (return addresses). Just don’t stuff it too much, or it might burst (stack overflow).

The CPU and OS: The Powerhouse and Organizer

Think of your computer as a team with two key players: the CPU (the powerhouse) and the OS (the organizer).

  • The CPU can do lots of things but relies on the OS to decide which task to do first.
  • The OS keeps a list of tasks and decides who gets priority.
  • When it’s time to start a new task, the OS tells the CPU what to do.
  • The OS ensures everyone gets a fair turn and steps in if a task goes haywire.
  • Sometimes tasks need to share stuff, and the OS helps them do it smoothly.

It’s like teamwork between a powerful player (the CPU) and a smart coach (the OS) to keep things running smoothly.

Computer Interrupts: Your Computer’s Secret Helpers

Interrupts are like the referees in the world of computers, making sure everything’s fair and square:

Hardware Interruptions

These are your computer’s way of saying, “Hey, pay attention!” when you click your mouse, type, or do stuff with hardware.

Software Interruptions

Sometimes, your software wants to chat with the operating system. So, it throws a “software interrupt” party to request help, like when a program asks to read a file or do memory tricks.

Timer Interruptions

Ever wonder how your computer keeps time so well? Timer interrupts are like alarms, waking up the CPU at regular intervals to keep tasks in check. Timer interrupts are the clock-keepers, ensuring that your CPU switches between tasks fairly and never gets stuck on one thing for too long.

Other Interruptions

You’ve got some other interruptions in the mix, like Exceptional Interruptions, CPU-to-CPU Calls (IPIs), and No-Nonsense Non-Maskable (NMIs). They’re like the computer’s chill squad, making sure it keeps its cool when things get hectic.

How Your Computer Juggles Tasks: The Mouse Interrupt Story

Imagine your computer system as a well-organized party, with the CPU acting as the master of ceremonies (MC), responsible for managing various tasks or threads — akin to playing different songs at the party. Guests at the party, representing programs or devices, can request songs (create threads) they’d like to hear. However, the MC can only play one song (thread) at a time. To decide which song to play next, the MC relies on the party’s organizer — the operating system (OS).

Let’s explore an example involving a mouse interrupt:

Mouse Interruption: A Guest’s Special Request

Picture a guest at the party (a program or device) moving a mouse, generating a mouse interrupt request — similar to a guest with an important request (a new thread) to make.

Interrupt Handling: The OS Takes the Reins

The OS steps in as a specialized party planner, managing unique requests. Here’s how the OS handles the situation:

  • Interrupt Handling: The OS temporarily pauses the current song (the CPU’s current thread) to address the mouse request (the new thread).
  • Thread Creation: The OS creates a new thread dedicated to managing the mouse interrupt request. This thread oversees tasks like processing mouse movements and updating the cursor’s position on the screen.
  • Priority Assessment: The OS evaluates the priority of the new thread. If the mouse movement is time-sensitive and crucial, such as when you’re about to click on something, the mouse thread receives a high priority. For less urgent situations, it might have a lower priority.
  • Dispatching the Thread: The OS instructs the MC (the CPU) to execute the instructions of the new thread. Consequently, the CPU shifts its focus from its previous task to effectively address the mouse request.
Thread execution

Imagine you’re engrossed in playing a video game (representing the ongoing party) and suddenly move your mouse to aim at a target. The mouse interrupt request triggers, and the OS promptly dispatches a new thread dedicated to updating the cursor’s position on the screen. In essence, this action temporarily interrupts the current song (thread) being played, allowing for the special request (the mouse thread) to be fulfilled before returning to the game (the previous thread).

Playing a games

In summary, the OS plays a pivotal role in handling and directing threads, ensuring that critical tasks, such as responding to mouse movements, are swiftly addressed while maintaining overall computer performance.

This happens so seamlessly that you often don’t even notice it.

Multi-Core CPU, a true parallel processing

Picture a multi-core CPU like a superhero squad. Each core has its own superpowers and can handle tasks independently. When there are lots of tasks, the operating system steps in as the team manager, giving tasks to the cores like delegating missions to superheroes.

Parallel processing

Here’s where the magic kicks in: each core can work on its task simultaneously. It’s like a well-organized team, with each superhero doing their thing without getting in each other’s way.

This teamwork makes your computer super-efficient. While one core calculates, another handles graphics, and yet another manages your internet — all at the same time. And the manager, the operating system, keeps things fair. If one core’s too busy while another’s slacking off, it redistributes tasks to keep everyone working.

Conclusion

So, there you have it — computers are like a well-coordinated squad with the CPU as the powerhouse, the OS as the organizer, and multi-core CPUs as the true multitasking heroes. Together, they ensure your computer runs smoothly, even when you’ve got a zillion things going on. It’s like witnessing a behind-the-scenes magic show, executed by unsung tech heroes who make it all happen effortlessly.

--

--

Nayanda Haberty
Nerd For Tech

I love programming and learning. Expert in iOS, but also did Android, Backend, and Web Dev. Programming is fun and I enjoy exploring different tech stacks.