Skip to content

Process vs Thread


Task: Run the same work twice, once as processes and once as threads

TWO PROCESSESONE PROCESS, TWO THREADScode + globalsheapcount = 1stackthreadProcess Acode + globalsheapcount = 0stackthreadProcess Bcode + globalsheapcount = 1stackthread 1stackthread 2One processboth threads read the same heap

Start with a program

Analogy

  • A program sitting on your disk is just a file. It does nothing.
  • Double click it and the operating system loads it into memory and starts running it.
  • That running copy is a process.
  • Open the same app twice and you get two processes. Same file, two separate runs.

Open your task manager right now and you are looking at a list of processes.


What a process is given

When the operating system starts a process, it hands it a block of memory that belongs to that process and nothing else.

Codethe instructions, never changed while runningGlobalsvalues that live for the whole programHeapcreated while running, freed when done withStackone per thread: locals and the call historyshared by every threadone each

Four parts, and it is worth knowing their names because everything else in this chapter refers to them.

Note

The important word is own. Another process cannot read this memory. If it tries, the operating system stops it. That isolation is not a bonus feature, it is the main thing a process is for.


So what is a thread?

The memory is just storage. Something has to actually walk through the code and run it.

Definition

A thread is a single line of execution inside a process.

It has its own stack, so it can keep track of which function called which, and its own place in the code.

Every process has at least one thread. That is the one that starts when the program starts.

So a process is not the thing that runs. A process is the box. A thread is the thing running inside the box.

Watch out

You will often see "a thread is a lightweight process". It is a rough analogy and it causes more confusion than it clears up. A thread is not a small process. It is a runner inside one.


Doing two things at once

Say you want your program to do two jobs at the same time. You have two options.

Option one: run the program twice. Two processes, two separate boxes of memory.

Option two: one process, two threads. One box, two runners inside it.

The animation at the top of this page walks through both. The difference is not speed. The difference is memory.

TWO THREADS, ONE PROCESSShared between themCodeGlobalsHeapOpen filesNetwork connectionsOne of each, per threadStackRegistersProgram counter

Two threads in one process share everything the process owns. The code, the globals, the heap, even open files and network connections. The only things each thread keeps to itself are its stack and its place in the code.


Try it

Two workers, each adding 1 to a counter three times. Press as threads to switch between the two arrangements and watch the answer change.

Try it yourself

Two workers, three additions each. The memory decides the answer

TWO PROCESSEScode + globalsheapcount = 0stackworker AProcess Acode + globalsheapcount = 0stackworker BProcess BONE PROCESS, TWO THREADScode + globalsheapstackthread 1stackthread 2One process
count = 0one counter each, in separate memory
0 of 6 additions

As two processes, each worker has its own counter and both end at 3.

As two threads, there is one counter between them and it ends at 6.

Same program. Same number of additions. The only thing that changed is whether the memory was shared.

Key takeaway

Processes have separate memory. Threads in one process share memory. Almost every other difference between them follows from that one fact.


What that buys, and what it costs

Sharing memory makes threads fast. Two threads pass data by writing to the same variable, which takes no time at all. Two processes have to copy data across a boundary the operating system polices.

Sharing memory also makes threads dangerous.

TWO PROCESSESProcess AcrashedProcess Bstill runningB never noticesTWO THREADSThread 1crashedThread 2crashedthe whole process goes down

If one process crashes, the other keeps running. It never even notices. If one thread crashes badly enough, it usually takes the whole process down, and every other thread with it, because they were all standing in the same memory.

Note

There is a second danger, and it is the more common one. When two threads write to the same variable at the same time, they can tread on each other and lose an update. That is called a race condition, and it is what locks exist to prevent. It is a chapter of its own.


Side by side

AspectProcessThread
MemoryIts own, isolatedShared with other threads in the process
Own stackYesYes
Starting oneSlower, needs new memoryFaster, memory already exists
Sharing dataNeeds help from the OSJust use the same variable
If it crashesOthers keep runningUsually takes the process down

Notice that both have their own stack. That trips people up. A thread shares plenty, but never its stack, because a stack is a record of where that particular runner is up to.


When to reach for which

Use separate processes

When the jobs are independent, and one failing must not take the others with it. Your browser does this: each tab is its own process, so one bad page cannot close the window.

Use threads

When the jobs are part of one piece of work and need the same data. A game drawing frames while loading the next level is one process with several threads.

A text editor keeps typing responsive while it saves a large file in the background.

  1. Are the two jobs better as threads or processes?
  2. Why?
Show answer
  1. Threads.
  2. Saving needs the document that typing is editing. As threads they share it directly. As processes the document would have to be copied across, and the copy would be out of date the moment you typed another character.

Quick recap

  • A process is a running program with memory of its own
  • A thread is one line of execution inside a process
  • Every process has at least one thread
  • Threads share the code, globals and heap of their process
  • Every thread keeps its own stack
  • Separate processes are safer, threads are faster and closer together

Key takeaway

A process is the box of memory. A thread is what runs inside it. Two processes cannot see each other's memory; two threads in one process share almost all of theirs.