The Building Blocks of Modern Computing

ZenvilaZenvila
3 min read

What is a Thread?

Before diving into the concept of threads, let’s consider a common example. Imagine a company named X with four employees. Each employee is assigned their own task, and they upload their completed work to a shared database. These tasks might range from creating an entire application to completing a small part of it. At the end of the day, all their contributions combine to form the final application. In this analogy, each employee represents a thread, while the company represents the process.


Key Terms in Threading

  1. Single-threaded Process:
    In a single-threaded process, one thread executes the entire sequence of operations sequentially.

  2. Multi-threaded Process:
    In a multi-threaded process, multiple threads work together, with each thread handling a specific part of the sequence of operations. The combined effort of these threads results in the completion of the task.


Shared Resources in Threads

All threads within the same process share certain resources, such as code or heap memory. This concept is referred to as shared resources.


Program vs. Process

  • Program: A program is a set of instructions written to perform a task.

  • Process: A process is the actual execution of a program. Multiple processes can be associated with the same program.


Understanding Parallelism

You may have heard terms like quad-core or octa-core processors. These terms indicate the number of threads a processor can handle simultaneously:

  • A quad-core processor can run four threads in parallel.

  • An octa-core processor can run eight threads in parallel.

Although it seems like multiple threads are running at the same time, in reality, the processor activates only a few threads at any given moment. This creates the illusion of parallel execution.


Parallel vs. Concurrent Threads

  • Parallel Threads: These threads run simultaneously on separate CPU cores.

  • Concurrent Threads: These threads share resources, such as memory or files. When one thread needs a shared resource that another thread is using, it must wait for the resource to become available. This waiting is referred to as concurrency.


Role of the Scheduler

To minimize the waiting time in concurrent threads, the operating system scheduler ensures efficient distribution of processing time among threads. The scheduler assigns priority levels and manages the execution of threads according to a set of predefined rules, thereby optimizing performance.


Let's move how to check in manjaro linux:-

Check Total Threads (CPU Threads):

This shows the number of threads per core.

The total number of CPU threads available:

Check active threads:

Using htop: Install htop if not already installed:

Look at the CPU meters to see active threads.

To see the load on each thread:

Counts the number of threads running across all processes:

Conclusion

  • A thread is the smallest sequence of instructions that can be managed independently by a scheduler.

  • Multi-threading allows multiple threads within a single process to share resources such as memory, while processes do not share resources.

  • Threads communicate through inter-thread communication, while processes use inter-process communication.

  • Threads generally execute faster than processes because they share the same resources, reducing overhead.

By understanding the basics of threads and their management, you can better appreciate how modern computing achieves efficiency and speed.

P.S. If you spot any mistakes, please don't hesitate to point them out. We're all here to learn together!

Haris
FAST (NUCES)
BS Computer Science | Class of 2027

0
Subscribe to my newsletter

Read articles from Zenvila directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Zenvila
Zenvila

I'm Haris aka Zen, currently in my 4th semester of Computer Science at FAST-NUCES and a member of COLAB (Research Lab) in Tier 3. I'm currently exploring AI/ML in its early stages, and also focusing on improving my problem-solving techniques. 🐧 Proud user of Arch Linux | Command line is my playground. I'm interested in Automation & Robotics Automation enthusiast on a mission to innovate! 🚀 Passionate about turning manual tasks into automated brilliance.