Labs ICT
โญ Pro Login

Threads

Lightweight processes and why concurrency matters.

What is a Thread?

A thread is a unit of execution within a process. If a process is a restaurant, threads are the individual employees โ€” the chef, the waiter, the cashier. They all work within the same restaurant (process), share the same kitchen (memory), and coordinate to get the job done.

Every process has at least one thread โ€” the main thread. But a process can have multiple threads, all running concurrently within the same address space. This is different from having multiple processes, which each have their own separate memory.

Process vs. Thread

The key difference between processes and threads comes down to what they share:


  PROCESS                          THREADS
  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”         โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  โ”‚ Process 1           โ”‚         โ”‚ Process 1           โ”‚
  โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚         โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
  โ”‚ โ”‚ Thread 1        โ”‚ โ”‚         โ”‚ โ”‚ Code  โ”‚ Data   โ”‚ โ”‚
  โ”‚ โ”‚ Thread 2        โ”‚ โ”‚         โ”‚ โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค โ”‚
  โ”‚ โ”‚ Thread 3        โ”‚ โ”‚         โ”‚ โ”‚ T1    โ”‚ T2  T3 โ”‚ โ”‚
  โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚         โ”‚ โ”‚ Stack โ”‚ Stack  โ”‚ โ”‚
  โ”‚ Code  Data  Heap   โ”‚         โ”‚ โ”‚ Regs  โ”‚ Regs   โ”‚ โ”‚
  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜         โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
                                  โ”‚ Shared: Code, Data, โ”‚
  Each process has its own:       โ”‚       Heap, Files   โ”‚
  โ€ข Memory space                  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
  โ€ข Files
  โ€ข State                        Each thread has its own:
                                 โ€ข Stack
                                 โ€ข Registers
                                 โ€ข Program counter
  • Processes are isolated. Each has its own memory space, its own files, its own everything. Communication between processes requires special mechanisms (pipes, sockets, shared memory).
  • Threads share the same memory, the same files, and the same address space. They can read and write to the same variables directly. But they each have their own stack, registers, and program counter.

This makes threads much lighter than processes. Creating a thread is faster, switching between threads is faster, and communication between threads is simpler. The trade-off is that threads can accidentally interfere with each other โ€” one thread can overwrite a variable that another thread is reading.

Why Use Multiple Threads?

Threads solve several practical problems:

  • Responsiveness โ€” A web browser can load a page in one thread while keeping the user interface responsive in another. If loading froze the entire browser, users would think it crashed.
  • Resource Sharing โ€” Threads naturally share memory, making it easy to divide work without copying data between address spaces.
  • Economy โ€” Creating and switching between threads is much cheaper than creating and switching between processes.
  • Scalability โ€” On multi-core processors, multiple threads can truly run in parallel โ€” one per core โ€” dramatically speeding up computation.

User Threads vs. Kernel Threads

There's an important distinction in where threads are managed:

  • User-level threads โ€” Managed by a thread library in user space, without the OS knowing about them. They're fast to create and switch, but if one thread makes a blocking system call, the entire process blocks โ€” even the other threads.
  • Kernel-level threads โ€” Managed by the OS itself. The kernel is aware of each thread and can schedule them independently. If one thread blocks, others can continue running.

Modern operating systems use kernel threads. Linux, Windows, and macOS all manage threads at the kernel level, giving the OS full control over scheduling and resource allocation.

The Multithreading Model

Different operating systems map user threads to kernel threads differently:

  • Many-to-One โ€” Many user threads map to one kernel thread. Simple but limiting โ€” if one thread blocks, all block.
  • One-to-One โ€” Each user thread maps to a kernel thread. Most flexible โ€” threads can run in parallel and block independently. Used by Linux and Windows.
  • Many-to-Many โ€” Many user threads map to many kernel threads. Allows multiplexing โ€” you can have more user threads than kernel threads. Used by some older systems.

The one-to-one model dominates today because it gives the best balance of flexibility and performance.

๐Ÿงช Quick Quiz

What is the key difference between a process and a thread?