Thread

This article is not assessed by the IB but may be helpful to deepen your understanding. Plus, I think it's cool.

The Big Idea

Modern operating systems must manage thousands of tasks at once. To do this efficiently, they break programs into threads — smaller units of execution that can run independently or concurrently. Understanding what a thread is, and how it differs from a process, is essential for explaining multitasking and concurrency in computer systems.

Definition: What is a Thread?

A thread is the smallest sequence of programmed instructions that can be managed independently by an operating system’s scheduler. Threads exist within processes, which are containers for program execution. Every process has at least one thread (the “main thread”), and some have many.

Each thread:

  • Has its own program counter, registers, and stack.
  • Shares the same memory space and resources (such as open files and heap memory) with other threads in the same process.

Example:
When a web browser opens multiple tabs, each tab might run as a thread. All tabs share the same memory for caching and cookies, but each tab’s thread executes independently (so one tab can load while another waits for network data).

A thread is not a physical object stored in memory; it is a logical structure — a set of data maintained by the operating system so the CPU can execute a stream of instructions independently. Therefore, a thread’s “size” depends on the resources it needs (registers, stack space, etc.), and this varies by system and design.

Components of a Thread

Each thread typically includes:

ComponentDescriptionTypical Size
Program Counter (PC)Holds the address of the next instruction to execute.A few bytes (4–8 bytes on most systems).
RegistersStore working data and addresses during execution.A few dozen bytes (depends on CPU architecture).
StackStores local variables, function parameters, and return addresses. Each thread has its own stack.Commonly 256 KB to 2 MB per thread, configurable.
Thread Control Block (TCB)OS structure that stores thread state, priority, CPU usage, etc.Around 1–10 KB, depending on OS.

So the major contributor to a thread’s “size” in memory is the stack.


Example: Typical Thread Memory Footprint

Let’s take a common operating system:

  • Linux (x86-64): default thread stack size = 8 MB

  • Windows: default thread stack size = 1 MB

  • Java Virtual Machine (JVM): often defaults to 1 MB per thread, adjustable with the -Xss option

  • Embedded systems: stack sizes might be as small as 4–16 KB

So if a program creates 100 threads on a system with a 1 MB stack per thread, it could consume about 100 MB of memory just for stacks.


3. Why Thread Size Matters

Each thread consumes system resources:

  • Memory: for its stack and control block

  • Scheduler overhead: the OS must track and switch between threads

If you create too many threads, your system can suffer from:

  • Memory exhaustion (out of address space for stacks)

  • Context-switch overhead, which reduces CPU efficiency

For this reason, high-performance systems often use thread pools or asynchronous I/O instead of spawning a new thread per task.

Explain: How Threads Work

The operating system manages threads using a scheduler, which decides when and where each thread runs on the CPU.

Each thread passes through states:

  1. New – The thread is created.
  2. Ready – The thread is waiting for CPU time.
  3. Running – The CPU is executing the thread’s instructions.
  4. Waiting/Blocked – The thread is paused, waiting for an event (e.g. input/output).
  5. Terminated – The thread has finished execution.

Threads within the same process can communicate quickly through shared memory, because they use the same address space. However, this also means they must be carefully synchronized (using mutexes, semaphores, or locks) to avoid data corruption.

 

Multithreading and Concurrency

Multithreading means that multiple threads of the same process run concurrently — either:

  • Truly in parallel (on different CPU cores), or
  • Virtually in parallel (on a single core through rapid context switching).

This allows programs to perform multiple operations at once, such as:

  • Downloading a file while updating the user interface.
  • Handling multiple client connections in a server application.

Example:
In a video game:

  • One thread handles graphics rendering,
  • One handles user input, and
  • Another manages physics calculations.

All run “at once,” making the game smooth and responsive.

 

Compare: Threads vs Processes

FeatureThreadProcess
Memory SpaceShares with other threads in same processIndependent
OverheadLightweightHeavyweight
CommunicationFast (shared memory)Slow (requires inter-process communication)
FailureA crash can affect other threads in same processUsually isolated to one process

 

Why Threads Matter

Threads improve efficiency and responsiveness, especially in multi-core architectures. By distributing work across cores, a system achieves true parallelism, reducing execution time. However, poor synchronization between threads can lead to race conditions, deadlocks, or inconsistent data, making correct design critical.

 

Summary

Threads are independent paths of execution within a process that allow concurrent operations. They are fundamental to efficient multitasking in both single-core and multi-core systems. Understanding threads provides insight into how operating systems schedule and manage work, enabling the creation of responsive and scalable applications.