Multithreading

11 min read2,132 words

Multithreading

What is Multithreading?

Multithreading is the ability of a process or program to execute multiple threads concurrently.

In simple words:

Multithreading = Multiple threads working within the same process.

A process can have one or many threads:

Single-threaded: Process | └── Thread 1
Multithreaded: Process | ├── Thread 1 ├── Thread 2 ├── Thread 3 └── Thread 4

The threads generally share the process's:

  • Code
  • Data
  • Heap
  • Open files
  • Other resources

But each thread has its own:

  • Program counter
  • Registers
  • Stack
  • Execution state

Why Do We Need Multithreading?

Imagine a program that needs to perform these tasks:

1. Download a file 2. Process data 3. Update the user interface 4. Save results

With only one thread:

Download ↓ Process ↓ Update UI ↓ Save

If downloading takes a long time, the rest of the application may have to wait.

With multiple threads:

Process / | \ ↓ ↓ ↓ Thread 1 Thread 2 Thread 3 ↓ ↓ ↓ Download UI Processing

The application can make progress on different tasks concurrently.

Multithreading vs Multiprocessing

These concepts are often confused.

Multithreading

Multiple threads exist within a process.

Process ├── Thread 1 ├── Thread 2 └── Thread 3

Multiprocessing

Multiple processes execute.

Process 1 Process 2 Process 3

Comparison

MultithreadingMultiprocessing
Multiple threadsMultiple processes
Threads share process resourcesProcesses generally have separate address spaces
Communication can be relatively easyIPC is generally more involved
Lower overhead in many casesGenerally higher resource overhead
Less isolationStronger isolation
Useful for concurrent tasks inside one applicationUseful for isolation and independent workloads

Concurrency and Parallelism

Multithreading can provide concurrency, and on multi-core systems it can also provide parallel execution.

Concurrency

Multiple tasks make progress during overlapping time periods.

Time → Thread A: ████ ████ Thread B: ████ ████

The tasks don't necessarily execute at exactly the same instant.

Parallelism

Multiple threads actually execute simultaneously on different CPU cores.

Core 1 → Thread A █████████ Core 2 → Thread B █████████

Therefore:

Multithreading does not automatically mean parallel execution.

Parallel execution requires suitable hardware and OS/runtime support.

Types of Multithreading

There are two broad approaches:

  1. User-Level Multithreading
  2. Kernel-Level Multithreading

1. User-Level Multithreading

User-level threads are managed primarily by a user-space library or runtime.

Application | ↓ Thread Library | +── Thread 1 +── Thread 2 +── Thread 3

The kernel may have limited awareness of these user-level threads depending on the implementation.

Advantages

  • Fast creation
  • Fast switching
  • Thread management can happen in user space
  • Less kernel involvement

Disadvantages

  • A blocking operation can be problematic in pure user-level models
  • Parallel execution may be limited unless user threads are mapped to multiple kernel execution entities
  • Scheduling is handled outside the kernel

2. Kernel-Level Multithreading

The operating system kernel directly manages the threads.

Process | +── Thread 1 ──┐ +── Thread 2 ──┼── Kernel Scheduler +── Thread 3 ──┘ ↓ CPU

Advantages

  • Better integration with OS scheduling
  • A blocked thread does not necessarily block the whole process
  • Can utilize multiple CPU cores
  • Kernel can schedule threads independently

Disadvantages

  • More overhead
  • Thread creation and management can involve kernel operations

Modern general-purpose operating systems commonly support kernel-managed threads.

Multithreading Models

Classically, thread implementations are discussed using these mapping models:

Many-to-One

T1 ──┐ T2 ──┼──→ K1 T3 ──┘

Many user threads map to one kernel thread.

Problem: One blocking kernel operation can block the entire group, and there is no true parallel execution through that single kernel thread.

One-to-One

T1 ──→ K1 T2 ──→ K2 T3 ──→ K3

Each user thread maps to a kernel thread.

Advantages:

  • Better parallelism
  • Independent scheduling
  • One blocked thread doesn't necessarily block others

Disadvantage:

  • More kernel resources are required.

Many-to-Many

T1 ──┐ T2 ──┼──→ K1 T3 ──┤ T4 ──┼──→ K2 T5 ──┘

Many user threads are mapped onto multiple kernel threads.

This provides flexibility but is more complicated to implement.

Benefits of Multithreading

1. Responsiveness

One thread can handle the user interface while another performs a long-running task.

Example:

UI Thread ↓ User interaction Worker Thread ↓ Large calculation

The UI can remain responsive.

2. Resource Sharing

Threads belonging to the same process share many resources.

Process │ ├── Shared Code ├── Shared Data ├── Shared Heap │ ├── Thread 1 ├── Thread 2 └── Thread 3

This makes communication between related threads relatively efficient.

3. Lower Overhead

Creating a thread is generally less expensive than creating a completely independent process because threads share many resources.

Similarly, switching between threads of the same process can require less work than switching between separate processes, although the exact cost depends on the OS and hardware.

4. Better CPU Utilization

While one thread waits for I/O, another thread may execute.

Thread 1 → Waiting for network ↓ Thread 2 → CPU ↓ Thread 3 → CPU

This can improve overall utilization.

5. Parallelism

On a multi-core processor:

Thread 1 → Core 1 Thread 2 → Core 2 Thread 3 → Core 3 Thread 4 → Core 4

Multiple threads can execute simultaneously.

This is particularly useful for CPU-intensive workloads that can be divided into independent tasks.

Multithreading Example

Suppose you have a video-processing application.

Without multithreading:

Read Video ↓ Decode Video ↓ Process Video ↓ Encode Video ↓ Save Video

With multiple threads, some work can overlap:

Video Process / | \ ↓ ↓ ↓ Thread 1 Thread 2 Thread 3 ↓ ↓ ↓ Read Process Encode

The actual design depends on the dependencies between stages.

Multithreading in a Web Server

A web server may receive many requests:

Client 1 ──┐ Client 2 ──┤ Client 3 ──┼──→ Web Server Client 4 ──┤ Client 5 ──┘

A threaded design could use workers:

Web Server | +── Worker Thread 1 → Client 1 +── Worker Thread 2 → Client 2 +── Worker Thread 3 → Client 3 +── Worker Thread 4 → Client 4

This allows multiple requests to be processed concurrently.

Thread Pool

Creating a new thread for every small task can be expensive.

A thread pool creates a collection of worker threads in advance.

Thread Pool +-------------------+ | T1 | T2 | T3 | T4 | +-------------------+ ↑ ↑ ↑ ↑ +----+----+----+ Tasks

When a task arrives, an available thread handles it.

After completing the task, the thread returns to the pool.

Advantages

  • Reduces thread creation overhead
  • Controls the number of active threads
  • Useful for servers and task-processing systems

Synchronization in Multithreading

Because threads share resources, multiple threads can access the same data.

Example:

Shared Variable | +--+--+ ↓ ↓ Thread A Thread B

This can create a race condition.

Example

Suppose:

counter = 100

Two threads execute:

Thread A → counter = counter + 1 Thread B → counter = counter + 1

You might expect:

102

But without proper synchronization, the result can incorrectly become:

101

because both threads may read the old value before either writes the new value.

Synchronization Mechanisms

Operating systems and programming environments provide mechanisms such as:

Mutex

Allows only one thread at a time to enter a protected critical section.

Thread A → LOCK → Critical Section → UNLOCK Thread B → waits

Semaphore

A synchronization mechanism based on a counter that controls access to resources.

Condition Variable

Allows threads to wait until a particular condition becomes true.

Read-Write Lock

Allows multiple readers but restricts concurrent writers.

These mechanisms are especially important when threads share mutable data.

Critical Section

A critical section is a part of code that accesses shared data or resources and must be protected from unsafe concurrent access.

Example:

Thread ↓ LOCK ↓ Critical Section ↓ UNLOCK

Only the appropriate number of threads should enter the protected region according to the synchronization mechanism.

Race Condition

A race condition occurs when the result depends on the timing or ordering of concurrent operations.

Example:

Shared Data | +--+--+ ↓ ↓ T1 T2 | | Read Read | | Write Write

Because the operations overlap unpredictably, the final result may be incorrect.

Deadlock in Multithreading

Threads can also experience deadlocks.

Example:

Thread A | | Holds Lock 1 ↓ Waiting for Lock 2 ↑ | Lock 2 | | Thread B | | Holds Lock 2 ↓ Waiting for Lock 1

Neither thread can continue.

This creates:

Deadlock

Deadlocks will be covered in detail later in your OS roadmap.

Thread-Safe Programming

A piece of code is considered thread-safe if it behaves correctly when accessed concurrently by multiple threads under its intended usage.

For example, shared data may need:

  • Mutexes
  • Atomic operations
  • Locks
  • Immutable data
  • Proper synchronization

Multithreading in Programming Languages

Many programming languages provide threading or concurrency facilities.

Python

Python provides:

import threading

Example:

import threading def task(): print("Task running") t = threading.Thread(target=task) t.start() t.join()

C++

C++ provides:

#include <thread>

Example:

#include <iostream> #include <thread> void task() { std::cout << "Task running\n"; } int main() { std::thread t(task); t.join(); }

The exact performance and parallelism characteristics depend on the language runtime, implementation, and workload.

Important: Python and Multithreading

A common misconception is:

"Python threads can never run simultaneously."

That's too broad.

In CPython, the Global Interpreter Lock (GIL) historically limits simultaneous execution of Python bytecode by multiple threads in the standard build. However, threads are still useful for many I/O-bound workloads because a thread can run while another waits for I/O.

Modern Python also has configurations and developments around free-threaded execution, so the exact behavior depends on the Python version/build.

For general OS understanding, remember:

I/O-bound workload → threads can be very useful CPU-bound workload → parallel execution depends on runtime and implementation

Multithreading Advantages and Disadvantages

AdvantagesDisadvantages
Better responsivenessMore complex programming
Resource sharingRace conditions
Lower overhead than processes in many casesSynchronization required
Better CPU utilizationDeadlocks possible
Can provide parallelismDebugging is harder
Good for concurrent workloadsToo many threads can cause overhead

Multithreading vs Multiprocessing

FeatureMultithreadingMultiprocessing
UnitThreadProcess
MemoryShared within processSeparate address spaces normally
CommunicationRelatively easy through shared memoryUsually requires IPC
IsolationLowerHigher
Creation costGenerally lowerGenerally higher
Failure isolationWeakerStronger
ParallelismPossible on multi-core systemsPossible on multi-core systems
SynchronizationVery importantAlso important for shared/IPC resources

Simple Real-Life Analogy

Imagine a restaurant.

Restaurant = Process

Workers are threads:

Cook → Thread 1 Cashier → Thread 2 Waiter → Thread 3 Cleaner → Thread 4

They share:

Kitchen Equipment Ingredients Tables

But each worker performs a different task.

That is similar to:

Process | +── Thread 1 → Task A +── Thread 2 → Task B +── Thread 3 → Task C

Exam Definition

Multithreading is the concurrent execution or management of multiple threads within a single process. Threads share the process's resources such as code, data, and heap while maintaining their own execution context, including stack, registers, and program counter.

Quick Revision

Multithreading ↓ Multiple threads ↓ Same process ↓ Shared resources + Individual execution context ↓ Concurrency / possible parallelism

Remember these 5 points

  1. Process can contain multiple threads.
  2. Threads share code, data, heap, and many resources.
  3. Each thread has its own stack, registers, and program counter.
  4. Multithreading improves responsiveness and can improve CPU utilization.
  5. Shared data creates synchronization, race-condition, and deadlock challenges.

Keep learning. Keep building. Keep growing.

© 2026 Ugenz Academy