Threads & Concurrency Models

a thread vs a process

Think of a process as a whole apartment with its own front door, its own furniture, and its own locked rooms — nothing inside is visible from the neighbor's apartment. A thread is one person living in that apartment. You can have several people (threads) sharing one apartment (process): they all walk the same hallways, use the same kitchen, and read the same bookshelf. To talk to someone in another apartment you have to knock and pass things through the door (that is inter-process communication); to talk to a roommate you just speak across the room (that is shared memory).

Concretely, a process is a running program together with its own private address space and its own resources: its code, data, heap, the list of open files, and so on. The OS isolates one process's memory from another's, so a bug or a crash in one process normally cannot corrupt another. A thread is a flow of execution inside a process. All threads of a process share that one address space and those resources, while each thread keeps only its own stack, registers, and program counter. So the big difference is what is private: a process owns a whole separate world, a thread owns just enough to remember where it is in the work.

This explains the trade-offs people care about. Creating a thread is much cheaper than creating a process, because there is no new address space to set up. Switching the CPU between two threads of the same process is cheaper than switching between processes, because the expensive memory-mapping state stays the same. Communication between threads is fast (shared memory) but unsafe by default; communication between processes is safer (isolation) but slower (it goes through the kernel). The honest caveat: that same isolation that makes processes safer is exactly what makes threads risky — one thread's wild memory write can corrupt the data of every other thread in the process.

Opening two separate browser windows as two processes means a crash in one window does not kill the other — they are isolated. But two tabs implemented as two threads in one process can corrupt each other if a bug runs wild, which is one reason modern browsers actually put each site in its own process.

Process isolation buys safety; thread sharing buys speed.

A common confusion: more threads are not always better. They are cheaper and faster to coordinate via shared memory, but they sacrifice the fault isolation that separate processes give you.

Also called
thread versus process執行緒對行程