multithreading · high

Process vs Thread

A process owns a private virtual address space — its own heap, stack, globals, and file descriptors — making processes isolated by default, while threads run inside a process and share that single address space (same heap, globals, and fds) with only a private stack and register set each. That distinction drives the rest: creating a process is heavy (cloning the address space and page tables) whereas a thread is lightweight, and switching between threads of one process is cheaper than between processes, which also flush the TLB. Communication differs accordingly — threads share memory directly, which is fast but requires synchronization via mutexes or atomics, while processes must use explicit IPC such as pipes, shared memory, or sockets. The defining trade-off is fault isolation: a bug in one thread (a segfault or heap corruption) can crash the entire process and every thread in it, whereas a process crash is contained and its resources are reclaimed by the OS. Choose multiple processes for isolation, security, and robustness (Chrome's process-per-tab, nginx workers) and threads for low-overhead parallelism over shared in-memory state; many real systems combine both.

🔑 Key line

Process = private (isolated) address space, the OS resource unit; thread = a flow of execution sharing its process's address space with a private stack. Threads are cheaper and share memory directly (needs sync); processes need IPC and give fault isolation — one thread crash kills the whole process.

The code

// PROCESS: own virtual address space (isolated)
pid_t pid = fork(); // child = a copy of the address space
// THREAD: shares the parent process's address space
std::thread t(work); // same heap, globals, file descriptors
// processes -> talk via IPC (pipe/shm/socket)
// threads -> share memory directly (but must synchronize)

What this lesson walks through

  1. 01Address space: isolated vs shared
  2. 02Cost, communication, switching
  3. 03Gotcha — one thread crash kills the process
  4. 04Fault isolation — when to pick which

A process owns a private virtual address space — its own heap, stack, globals, and file descriptors — so processes are isolated by default. Threads live INSIDE a process and share that one address space: same heap, same globals, same fds. That single fact drives every other difference.

See it animated — step by step, at your own pace

Unlock the full interactive walkthrough of Process vs Thread and 100+ animated C++ interview lessons.

← Previous
std::future, promise & async
Next →
Lock-Free SPSC Ring Buffer