Chapter 08 of 38

Threads & Multithreading

Think about a web browser. While one part of it downloads an image, another part draws the page, another responds to your mouse, and another runs JavaScript. If the browser could do only one thing at a time, the whole window would freeze every time it waited for the network.

One way to do several things at once is to create several processes, as we did with fork() in Process Creation (fork & exec). But processes are heavy: each has its own memory, and sharing data between them needs special mechanisms. Often what we really want is several flows of execution inside the same program, working on the same data.

That is exactly what a thread is.

A thread is the basic unit of CPU utilization: a single sequence of instructions within a process, with its own program counter, registers and stack, but sharing the process's code, data, heap and open files with the other threads of that process.

A process with one thread does one thing at a time. A multithreaded process has several threads that can run concurrently, and on a multi-core CPU, truly in parallel.


What Threads Share and What They Don't

   Single-threaded process          Multithreaded process (3 threads)

  ┌──────────────────────┐         ┌──────────────────────────────────┐
  │ code │ data │ files  │         │ code      │ data      │ files    │  shared
  │      │ heap │        │         │           │ heap      │          │  by all
  ├──────────────────────┤         ├───────────┬───────────┬──────────┤
  │ registers  │ stack   │         │ registers │ registers │ registers│  private
  │ PC         │         │         │ PC        │ PC        │ PC       │  to each
  │            │         │         │ stack     │ stack     │ stack    │  thread
  └──────────────────────┘         └───────────┴───────────┴──────────┘
         one thread                   thread 1    thread 2    thread 3

Shared by all threads of a process

Private to each thread

Code (text) section

Thread ID

Global and static variables (data section)

Program counter

Heap (memory from malloc/new)

CPU registers

Open files and sockets

Stack (local variables, function calls)

Signal handlers, current directory, user ID

Scheduling state and priority; signal mask; errno

Why must each thread have its own stack? Because each thread calls different functions at different times. The stack records "which function am I in, and what are its local variables". Two threads sharing one stack would overwrite each other's function calls immediately.

Why must each thread have its own program counter and registers? Because each thread is at a different point in the code. These are exactly the things a context switch saves, which is why the kernel keeps a small per-thread control block (a TCB, thread control block) in addition to the process's PCB.


Why Use Threads?

  1. Responsiveness. One thread can wait for slow I/O while another keeps the user interface responsive.

  2. Resource sharing. Threads share memory automatically. No pipes or shared-memory setup is needed (but access must be synchronized, as we will see).

  3. Economy. Creating a thread and switching between threads is cheaper than creating and switching processes, because there is no new address space to set up or switch.

  4. Scalability. On a multi-core CPU, the threads of one process can run on different cores at the same time, so one program can use all cores.

Typical examples: a web server that handles each request in a separate thread, a game with separate threads for rendering, physics and audio, a word processor that spell-checks in the background while you type.


Concurrency vs Parallelism

These two words are often used interchangeably, but they mean different things.

  • Concurrency: several tasks are in progress during the same period. On a single core, the OS interleaves them with context switches.

  • Parallelism: several tasks are executing at the same instant, which needs multiple cores.

Concurrency on 1 core:      core 0:  T1 T2 T1 T3 T2 T1 T3 ...  (interleaved)

Parallelism on 3 cores:     core 0:  T1 T1 T1 T1 ...
                            core 1:  T2 T2 T2 T2 ...
                            core 2:  T3 T3 T3 T3 ...

You can have concurrency without parallelism (single core), but parallelism always involves concurrency.


Your First Threads with POSIX Threads (pthreads)

On Linux and other Unix systems, the standard thread API is POSIX threads, or pthreads. The two most important calls are:

  • pthread_create(&tid, attr, function, arg) → start a new thread that runs function(arg)

  • pthread_join(tid, &result) → wait for that thread to finish (the thread version of wait())

#define _GNU_SOURCE
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>

int shared = 100;                      /* global: ONE copy for all threads */

void *worker(void *arg) {
    int id = *(int *)arg;              /* each thread gets its own id */
    int local = id * 10;               /* local: lives on THIS thread's stack */
    printf("Thread %d: PID=%d TID=%d shared=%d &shared=%p &local=%p\n",
           id, getpid(), gettid(), shared, (void *)&shared, (void *)&local);
    return NULL;
}

int main(void) {
    pthread_t t[2];
    int ids[2] = {1, 2};

    printf("main    : PID=%d TID=%d\n", getpid(), gettid());
    for (int i = 0; i < 2; i++)
        pthread_create(&t[i], NULL, worker, &ids[i]);
    for (int i = 0; i < 2; i++)
        pthread_join(t[i], NULL);      /* wait for both threads */
    printf("main    : both threads finished\n");
    return 0;
}

Compile with -pthread and run:

gcc -std=c17 -Wall -Wextra -pthread thr1.c -o thr1
./thr1

Output (one run):

main    : PID=15076 TID=15076
Thread 1: PID=15076 TID=15077 shared=100 &shared=0x6554dfb1a010 &local=0x71e3e39fee70
Thread 2: PID=15076 TID=15078 shared=100 &shared=0x6554dfb1a010 &local=0x71e3e31fde70
main    : both threads finished

What this shows:

  • All three threads report the same PID (15076): they are in the same process.

  • Each thread has a different TID (thread ID). On Linux the main thread's TID equals the PID.

  • All threads see the same address for shared: one variable, shared by everyone.

  • The address of local is different in each thread, because each thread has its own stack.

  • Thread 1 and Thread 2 may print in either order on another run. Thread scheduling is not deterministic.

Threads Really Share Memory

Compare this with the fork example in the previous chapter, where the child's change to x was invisible to the parent:

#include <pthread.h>
#include <stdio.h>

int x = 10;

void *change(void *arg) {
    (void)arg;
    x = x + 5;                         /* same x that main() sees */
    printf("Thread: x = %d\n", x);
    return NULL;
}

int main(void) {
    pthread_t t;
    pthread_create(&t, NULL, change, NULL);
    pthread_join(t, NULL);
    printf("Main  : x = %d\n", x);
    return 0;
}

Output:

Thread: x = 15
Main  : x = 15

This time the main thread sees the change, because there is only one x. This is both the power and the danger of threads.

Passing Work to Threads and Getting Results

A common pattern is to split a big job into pieces, give each piece to a thread, then combine the results. Here two threads each sum half of an array:

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

#define N 8                       /* array size   */
#define T 2                       /* thread count */

int data[N] = {1, 2, 3, 4, 5, 6, 7, 8};

struct job { int start, end; long sum; };

void *partial_sum(void *arg) {
    struct job *j = arg;
    j->sum = 0;
    for (int i = j->start; i < j->end; i++)
        j->sum += data[i];
    return NULL;
}

int main(void) {
    pthread_t tid[T];
    struct job jobs[T];
    int chunk = N / T;

    for (int i = 0; i < T; i++) {
        jobs[i].start = i * chunk;
        jobs[i].end   = (i + 1) * chunk;
        pthread_create(&tid[i], NULL, partial_sum, &jobs[i]);
    }

    long total = 0;
    for (int i = 0; i < T; i++) {
        pthread_join(tid[i], NULL);
        printf("Thread %d summed data[%d..%d] = %ld\n",
               i, jobs[i].start, jobs[i].end - 1, jobs[i].sum);
        total += jobs[i].sum;
    }
    printf("Total = %ld\n", total);
    return 0;
}

Output:

Thread 0 summed data[0..3] = 10
Thread 1 summed data[4..7] = 26
Total = 36

Each thread gets a pointer to its own struct job, so the threads never write to the same memory, and the main thread reads the results only after pthread_join() guarantees the thread has finished. This careful design is why this program is correct without any locks.

Seeing Threads from the Outside

Linux tools can show the threads of a process. ps -L lists one line per thread (LWP = light-weight process, Linux's name for a thread; NLWP = number of threads):

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

void *sleeper(void *arg) { (void)arg; sleep(2); return NULL; }

int main(void) {
    pthread_t t[3];
    for (int i = 0; i < 3; i++)
        pthread_create(&t[i], NULL, sleeper, NULL);

    char cmd[64];
    snprintf(cmd, sizeof cmd, "ps -L -o pid,lwp,nlwp,cmd -p %d", getpid());
    system(cmd);                     /* list the threads of THIS process */

    for (int i = 0; i < 3; i++)
        pthread_join(t[i], NULL);
    return 0;
}

Output:

    PID     LWP NLWP CMD
  19129   19129    4 ./thr5
  19129   19130    4 ./thr5
  19129   19131    4 ./thr5
  19129   19132    4 ./thr5

One PID, four thread IDs (the main thread plus three created ones). The same information appears as the Threads: line in /proc/<pid>/status and as directories in /proc/<pid>/task/.


How Much Cheaper Is a Thread?

This program creates and waits for 2,000 processes, then creates and joins 2,000 threads, and compares the average time:

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>

#define COUNT 2000

static void *nothing(void *arg) { return arg; }

static double now(void) {
    struct timespec t;
    clock_gettime(CLOCK_MONOTONIC, &t);
    return t.tv_sec + t.tv_nsec / 1e9;
}

int main(void) {
    double s = now();
    for (int i = 0; i < COUNT; i++) {          /* create + wait processes */
        pid_t p = fork();
        if (p == 0) _exit(0);
        waitpid(p, NULL, 0);
    }
    double proc = (now() - s) / COUNT * 1e6;

    s = now();
    for (int i = 0; i < COUNT; i++) {          /* create + join threads */
        pthread_t t;
        pthread_create(&t, NULL, nothing, NULL);
        pthread_join(t, NULL);
    }
    double thr = (now() - s) / COUNT * 1e6;

    printf("fork + wait          : %6.1f microseconds each\n", proc);
    printf("pthread_create + join: %6.1f microseconds each\n", thr);
    printf("threads were about %.0fx cheaper\n", proc / thr);
    return 0;
}

Output (one run):

fork + wait          :  104.2 microseconds each
pthread_create + join:   40.6 microseconds each
threads were about 3x cheaper

On this machine (Ubuntu on WSL2), creating a thread was roughly 2 to 3 times cheaper than creating a process, even though Linux's fork() is already fast thanks to copy-on-write. The exact numbers vary a lot between machines and runs; for processes with large memory, the gap grows, because fork must copy bigger page tables.

Aspect

Process

Thread

Memory

Own address space

Shares the process's address space

Creation cost

Higher

Lower

Context switch

Slower (switch page tables, TLB effects)

Faster within the same process

Communication

Needs IPC (pipes, shared memory, ...)

Directly through shared variables

Isolation

A crash usually affects only that process

A crash (e.g. segmentation fault) kills the whole process, all threads

Protection

Processes cannot touch each other's memory

Any thread can overwrite any other thread's data

That last pair of rows explains why Google Chrome runs each site in a separate process rather than a thread: if one tab crashes or is attacked, the others are protected.


User-Level Threads vs Kernel-Level Threads

Who manages the threads: a library in user space, or the kernel itself?

User-Level Threads (ULT)

A thread library inside the process creates, schedules and switches threads without the kernel knowing. The kernel sees just one ordinary process.

Kernel-Level Threads (KLT)

The kernel knows about every thread, keeps a control block for each, and schedules each thread independently on the CPUs.

Feature

User-Level Threads

Kernel-Level Threads

Managed by

User-space library

Operating system kernel

Kernel aware of threads?

No

Yes

Creation and switching

Very fast (no system call)

Slower (needs kernel involvement)

One thread makes a blocking system call

Whole process blocks

Only that thread blocks

Use multiple cores

No (kernel gives the process one CPU at a time)

Yes

Scheduling policy

Can be customised by the application

Decided by the kernel


Multithreading Models

Systems that have both kinds map user threads onto kernel threads in one of these ways:

Many-to-One

user threads:    U1   U2   U3   U4
                   \   |    |   /
kernel thread:          K1

All user threads run on one kernel thread. Fast switching, but one blocking call blocks everyone, and there is no parallelism. "Green threads", a library on Solaris that early versions of Java also used, worked this way. Pure many-to-one is rare today.

One-to-One

user threads:    U1   U2   U3   U4
                  |    |    |    |
kernel threads:  K1   K2   K3   K4

Each user thread has its own kernel thread. True parallelism, and a blocking call blocks only one thread. The cost is that every thread is a kernel object, so creating huge numbers of them is expensive. Linux (with the NPTL pthreads library) and Windows use this model.

Many-to-Many

user threads:    U1   U2   U3   U4   U5   U6
                   \    \  |    |   /    /
kernel threads:        K1    K2    K3

Many user threads are multiplexed onto a smaller or equal number of kernel threads. You can create thousands of cheap user threads and still get parallelism. It is harder to implement well. A variation called the two-level model also allows a user thread to be bound to its own kernel thread.

The many-to-many idea is very much alive in modern language runtimes: Go's goroutines are scheduled by the Go runtime onto a small pool of OS threads, and Java's virtual threads (final since JDK 21) are mounted onto a small pool of carrier OS threads. This lets a server handle hundreds of thousands of concurrent tasks.


Threads in Linux: Everything Is a Task

Linux does not have a separate "thread" object. Both processes and threads are tasks, each with its own task_struct (the PCB from Process Control Block & Context Switching). The difference is only in what they share.

Both are created by the clone() system call, whose flags say what the new task shares with its creator:

  • fork() → clone with almost nothing shared (copy-on-write memory, copied file table) → a new process

  • pthread_create() → clone with CLONE_VM (share memory), CLONE_FILES (share open files), CLONE_SIGHAND (share signal handlers), CLONE_THREAD (same thread group, so the same PID) and more → a new thread

That is why, in our output, each thread had its own TID: every thread is a schedulable task. What getpid() returns is the thread group ID (the Tgid line in /proc/<pid>/status), which is the same for all threads of the process.


Thread Pools

Creating a thread for every task (for example, every incoming web request) has two problems: creation takes time, and an unlimited number of threads can exhaust memory and CPU.

A thread pool creates a fixed number of worker threads at startup. Tasks are put in a queue, and idle workers pick them up:

incoming tasks ──▶ [ task queue: T7 T6 T5 T4 ] ──▶ worker 1 (running T1)
                                                ──▶ worker 2 (running T2)
                                                ──▶ worker 3 (running T3)

Benefits: no creation cost per task, and a hard limit on how many threads exist. Java's ExecutorService, Python's concurrent.futures.ThreadPoolExecutor and most web servers use this idea.


How Much Faster Can Threads Make a Program? Amdahl's Law

If we have N cores, will a program run N times faster? Only if all of it can run in parallel. Most programs have a part that must run serially (reading input, combining results).

Amdahl's Law: if a fraction P of a program can be parallelized and the rest (1 − P) is serial, the maximum speedup on N cores is: Speedup = 1 / ((1 − P) + P / N).

Worked example: the table below shows the maximum speedup for programs that are 50%, 90% and 95% parallel (P = 0.5, 0.9, 0.95).

Cores (N)

Speedup with P = 0.5

Speedup with P = 0.9

Speedup with P = 0.95

2

1.33

1.82

1.90

4

1.60

3.08

3.48

8

1.78

4.71

5.93

16

1.88

6.40

9.14

Infinite

2.00

10.00

20.00

For P = 0.9 and N = 4: 1 / (0.1 + 0.9/4) = 1 / (0.1 + 0.225) = 1 / 0.325 ≈ 3.08. Even with infinite cores, the speedup can never exceed 1 / (1 − P) = 1 / 0.1 = 10. The serial part becomes the bottleneck.


Threading Issues to Be Aware Of

Shared Data Needs Protection

When two threads update the same variable at the same time (for example, both do counter++), updates can be lost, because counter++ is really three steps: read, add, write. This is called a race condition. It is the single biggest source of bugs in multithreaded programs, and the next block of chapters is devoted to it, starting with Process Synchronization & Race Conditions.

fork() in a Multithreaded Program

If one thread of a multithreaded process calls fork(), does the child get all the threads? In POSIX, no: the child contains only a copy of the thread that called fork. That is fine if the child immediately calls exec, but dangerous otherwise, because a lock held by another thread at fork time stays locked forever in the child.

Thread Cancellation

Stopping a thread before it finishes can be asynchronous (stop it immediately, which can leave data half-updated) or deferred (the thread checks at safe points whether it should stop). pthreads uses deferred cancellation by default.

Signals

A signal sent to a multithreaded process (for example from kill) is delivered to one thread that has not blocked it. Signals caused by a specific thread, such as a segmentation fault, go to that thread.

Thread-Local Storage

Sometimes each thread needs its own copy of a "global" variable. C11 provides _Thread_local (or thread_local) for this. The C library's errno is a classic example: each thread has its own errno, so one thread's error does not overwrite another's.

A Note on Python

In the standard CPython interpreter, the Global Interpreter Lock (GIL) lets only one thread execute Python bytecode at a time, so threads help with I/O-bound work but not with CPU-bound work. Since Python 3.13 there is an optional "free-threaded" build without the GIL, and Python 3.14 made it officially supported, but the default build still has the GIL.


Common Mistakes

  • "Threads have their own copy of global variables." No. Globals and the heap are shared. Only the stack (local variables) and registers are private.

  • Returning a pointer to a local variable from a thread function. That variable lives on the thread's stack, which disappears when the thread ends.

  • Passing the address of a loop variable to several threads. All threads would read the same changing variable. Give each thread its own argument (like the ids[] array and jobs[] array in our examples).

  • Forgetting to join. If main() returns, the whole process exits and all other threads are killed, even if they have not finished.

  • "More threads always means faster." Beyond the number of cores, CPU-bound threads only add switching overhead. And Amdahl's law limits the gain.

  • Forgetting -pthread when compiling on older systems, which can cause link errors.


Interview Questions

What is the difference between a process and a thread?

A process is a program in execution with its own address space and resources. A thread is a unit of execution inside a process; threads of the same process share code, data, heap and open files but have their own stack, registers and program counter. Threads are cheaper to create and switch, but less isolated.

What do threads of the same process share? What is private?

Shared: code, global/static data, heap, open files, signal handlers. Private: thread ID, program counter, registers, stack, signal mask and thread-local variables such as errno.

Why is a thread context switch faster than a process context switch?

Threads of one process share the address space, so the page table does not change and the TLB and caches remain useful. Only registers and the stack pointer must be switched.

What happens if a user-level thread makes a blocking system call?

In a pure user-level (many-to-one) implementation, the kernel sees only one thread, so the whole process blocks, including all other user threads.

Which multithreading model does Linux use?

One-to-one. Each pthread is a kernel task created with clone(); the kernel schedules every thread independently.

What is the difference between concurrency and parallelism?

Concurrency means multiple tasks make progress over the same period, possibly by interleaving on one core. Parallelism means multiple tasks execute at the same instant on multiple cores.

State Amdahl's law. What is the maximum speedup if 80% of a program is parallel?

Speedup = 1 / ((1 − P) + P/N). With P = 0.8, the limit as N grows is 1 / 0.2 = 5, no matter how many cores you add.

If one thread crashes with a segmentation fault, what happens to the other threads?

The signal terminates the whole process by default, so all threads die. This is a key reason to use processes instead of threads when isolation matters.


Summary

  • A thread is a unit of execution inside a process with its own PC, registers and stack, sharing code, data, heap and files with other threads.

  • Benefits: responsiveness, easy resource sharing, lower cost than processes, use of multiple cores.

  • pthreads: pthread_create() starts a thread, pthread_join() waits for it.

  • User-level threads are fast but block together and cannot use multiple cores; kernel-level threads are scheduled independently by the OS.

  • Models: many-to-one, one-to-one (Linux, Windows), many-to-many (ideas used by Go goroutines and Java virtual threads).

  • In Linux, processes and threads are both tasks created by clone(); threads share more.

  • Amdahl's law limits speedup to 1 / (serial fraction).

  • Shared data between threads must be synchronized, or race conditions occur.

Processes give you isolation; threads give you sharing. Choose based on which one your program needs more.