# Threads vs Processes: How Your Computer Does Two Things at Once

To put it simply, a process is a running program with its own private memory, and a thread is a worker inside that program who shares the memory with the other workers beside it. Your browser is a process. Your music player is another process. Inside your browser, one thread draws the page while another downloads a file in the background, and both reach into the same memory without asking permission. That difference — private memory versus shared memory — is the whole story.

Think of a process as a house. Each house has its own kitchen, its own fridge, its own pantry shelves. A thread is a housemate in that house. The housemates share the one kitchen: one chops vegetables while another stirs the pot, and both can see the same cutting board. One house never walks into another house and raids its fridge. Processes cannot touch each other's memory. Threads can, because they live in the same house.

![](https://cdn.hashnode.com/uploads/covers/6a12efab551486ce6cffb0f8/c45149e2-3365-4615-9ef7-1e2eb737da5e.png align="center")

## A process carries its own private memory

When you double-click an app, your operating system starts a process for it. It sets aside a chunk of memory, notes which files the program may touch, and tracks how much CPU time it has used. If the process crashes, the system cleans up after it and the other processes keep going.

That isolation is why one frozen app does not take down the whole machine. You force-quit the frozen one and everything else carries on. Starting one costs real work, though — fresh memory, loaded code, opened files. That cost is where threads come in.

## A thread borrows the memory of its process

A thread is a single line of execution — one sequence of instructions, one after another — running inside a process. It keeps its own small scratch pad for the function it is running right now, but it shares everything else with the other threads in the same process: the same variables, the same open files, the same heap where objects live.

This is why your editor keeps the cursor blinking while it saves a 400-kilobyte file in the background. One thread watches your keystrokes. The other writes the file. Neither waits for the other. Threads are the right tool when the work is mostly waiting: waiting for a file, waiting for a network reply, waiting for you to type.

## Sharing memory is fast, and it is also the danger

Two threads can read the same variable in the same instant, and that is fine. Trouble starts when both write to it. Picture two housemates reaching for the same jar at the same moment. One puts it back on the wrong shelf. The other cannot find it. In code this is called a race condition, and it is miserable to debug because it only shows up sometimes.

I once spent a whole afternoon chasing a counter that was supposed to reach 10,000 and kept landing on 9,997. Two threads were adding one to the same number. Every now and then both threads read the same value before either had written, so one addition simply vanished. The fix was a lock — a rule that only one thread may touch the counter at a time. A lock is a "one at a time" sign on the kitchen door. Shared memory means shared responsibility.

## Python's GIL puts one lock on the whole kitchen

The standard version of Python, CPython, has the Global Interpreter Lock — the GIL. It is a single lock that lets only one thread run Python code at a time inside a process. That sounds fatal for threads. It is not, quite. Most of the time a thread spends waiting — waiting for a file to download, waiting for a database to answer — it is not running Python code at all. It hands the GIL back while it waits, so another thread can work.

Where the GIL hurts is number crunching. If two threads are both doing heavy arithmetic, they take turns on the one lock instead of running side by side on two cores. For that job, Python programmers reach for processes instead. Since Python 3.13 there is an optional free-threaded build without the GIL, but the default build most people install still has it. So the practical rule has not changed: threads for waiting, processes for calculating.

## Threads win when the job waits, processes win when the job calculates

|  | Threads | Processes |
| --- | --- | --- |
| Memory | Shared with the process | Private to each process |
| Starting cost | Cheap, a few microseconds | Expensive, fresh memory and setup |
| Talking to each other | Direct, just read the same variable | Needs pipes, queues or files |
| One crash | Can bring down the whole process | Only that process dies |
| Best for | Waiting on files, network, user input | Heavy maths, using several CPU cores |

A web server handling 500 visitors at once leans on threads, because each visitor mostly waits. A video encoder splitting a film across 8 cores leans on processes, because each chunk is pure calculation.

![](https://cdn.hashnode.com/uploads/covers/6a12efab551486ce6cffb0f8/7154f184-5223-4e12-983d-7df53ab21f4a.png align="center")

## A tiny example you can run right now

Here is the smallest version that works:

```python
import threading
import time

def fetch_page(name):
    time.sleep(1)  # pretend we are waiting for a download
    print(f"{name} is done")

thread_a = threading.Thread(target=fetch_page, args=("Page A",))
thread_b = threading.Thread(target=fetch_page, args=("Page B",))
thread_a.start()
thread_b.start()
thread_a.join()
thread_b.join()
print("Both pages fetched")
```

The two threads each wait one second, but because they wait at the same time, the whole script finishes in about one second instead of two. The `join()` calls simply tell the main program to wait until both threads have finished. Save it as `two_threads.py` and run it five times — the order of the two lines may swap. That swapping is the whole idea in miniature.

## Pick the tool that fits the job

Processes are separate houses, threads are housemates in one house. Houses are safe and slow to build. Housemates are fast and need manners. When your program spends its time waiting, threads are the easy win. When it spends its time calculating, spread the work across processes and let every core earn its keep. And when a threaded program misbehaves, look for the sharing first.

- A process is a running program with private memory; a thread is a worker inside it that shares that memory.
- Threads are cheap and great for waiting — files, networks, user input — but shared memory means race conditions are possible, so use a lock where two threads write the same thing.
- Processes cost more to start and cannot see each other's memory, which makes them safer and the right choice for heavy calculation across several cores.
- In default CPython the GIL lets only one thread run Python code at a time, so threads speed up waiting work, not number crunching; the free-threaded build in 3.13+ is optional and still catching on.
- When in doubt, start with threads for waiting jobs and processes for calculating jobs, and test with real numbers before you optimise.

![](https://cdn.hashnode.com/uploads/covers/6a12efab551486ce6cffb0f8/ab7ea6b1-096c-4180-9afe-97e17bdb956f.png align="center")

## References and further reading

- [Thread (computing) — Wikipedia](https://en.wikipedia.org/wiki/Thread_\(computing\))
- [Process (computing) — Wikipedia](https://en.wikipedia.org/wiki/Process_\(computing\))
- [threading — Thread-based parallelism — Python documentation](https://docs.python.org/3/library/threading.html)
- [Concurrency model and the event loop — MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Event_loop)

— Min SiThu Software Engineer & Tech Content Creator <https://minsithu.org>
