What is a Thread?
You'll learn: What threads are and how they differ from processes as lightweight units of execution.
The Core Idea
A thread is a lightweight execution unit that runs within a process. While a process is like a full program with its own private memory space, a thread is more like an independent path of execution inside that program. Multiple threads can exist within the same process, all sharing the process's memory and resources.
Think of it this way: if a process is like a restaurant kitchen, then threads are the individual cooks working in that same kitchen. They all share the same equipment, ingredients, and workspace (the process's memory), but each cook can work on different tasks simultaneously.
Key Characteristics
Unlike separate processes (which each have their own isolated memory via the Process Control Block), threads within the same process share:
- The same address space (code, data, and heap memory)
- The same open files and I/O resources
- The same process ID
But each thread maintains its own:
- Program counter (tracking which instruction it's executing)
- Stack (for local variables and function calls)
- Register set (CPU register values)
Why Use Threads?
Threads are "lightweight" because creating and switching between them requires much less overhead than creating new processes or performing context switches between processes. Since threads share memory, they can communicate more easily—no need for special inter-process communication mechanisms.
For example, a web browser might use one thread to handle user input, another to render graphics, and another to download files—all within the same browser process.
Key Takeaway: A thread is a lightweight execution unit within a process that shares the process's address space and resources but has its own execution state (program counter, stack, and registers).