Chapter 4: Threads

Operating Systems
Processes and threadsShared and private stateMultithreaded server Concurrency and parallelismAmdahl's lawUser and kernel threads 1:1, N:1, M:N modelsPthreads and JavaPools and fork-join OpenMP and GCDRace conditionsTLS, cancellation, signals
Sercan KÜLCÜ · Giresun University · Computer Engineering

Introduction

Learning Outcomes

Concept

Distinguish a thread from a process; say which state is shared and which is private to a thread.

Performance

Tell concurrency from parallelism and compute the limit on speedup with Amdahl's law.

Models

Compare user-level and kernel-level threads and the N:1, 1:1 and M:N mapping models.

Programming

Create and join threads with Pthreads and Java; recognize implicit approaches such as pools, fork-join, OpenMP and GCD.

Issues

Explain race conditions, non-reentrant code, TLS, cancellation, signals and the behavior of fork().

Real systems

Describe current implementations such as Linux clone(), Java 21 virtual threads and Go.

Basic Concepts

What Is a Thread?

  • Process: a program in execution; it owns resources such as the address space and open files.
  • Thread: a flow of execution within a process; the basic unit of CPU use and scheduling.
  • A process contains one or more threads; all share the same address space.
  • Each thread has its own PC, register set, stack and state, kept in its TCB (thread control block).
  • Tanenbaum's distinction: a process groups resources; a thread executes.
TTCB · threadtidstatePCregistersSP → stackpriorityTLS pointerPCB · processpidaddress spaceopen filessignal handlerscredentials (uid/gid)thread listmany TCBs → one PCB(both are task_struct on Linux)
The old name is lightweight process (LWP): creating and switching threads is cheap compared with processes, because the address space is not copied.

Basic Concepts

Why Multiple Threads?

  • Most modern applications are multithreaded: browsers, IDEs, game engines, databases, web servers.
  • Different tasks run in separate threads:
    • updating the display, fetching data, answering network requests, spell checking…
  • While one task waits (disk, network), the others keep making progress.
  • Creating a process is expensive; creating a thread is much cheaper.
  • Flows written like sequential code simplify the program.
  • The OS kernel is multithreaded too (Linux: kthreadd, kworker, ksoftirqd).
application processupdate displayfetch dataanswer network requestkernel (scheduler)

Basic Concepts

Single-Threaded and Multithreaded Processes

single-threaded processcodedatafilesregistersstack

A single-threaded process: one flow of execution, one PC, one register set and one stack.

single-threaded processcodedatafilesregistersstackmultithreaded process (3 threads)codedatafilesregistersstackregistersstackregistersstack

A multithreaded process: code, data and files exist once; each flow has its own registers and stack.

single-threaded processcodedatafilesregistersstackmultithreaded process (3 threads)codedatafilesregistersstackregistersstackregistersstacktop row: shared · bottom row: per threadall three flows see the same code and global data

The orange row is the shared resources: when one thread writes a global variable, the others see it at once.

Basic Concepts

Shared and Private Items

Per process (shared by all threads)
Address space: code, global variables, heap
Open files, sockets
Child processes
Pending alarms (timers)
Signal handlers
Identity and accounting information
Per thread (private)
Program counter (PC)
Registers
Stack and stack pointer
State: running, ready, waiting, terminated
Thread ID, signal mask
Thread-local storage (TLS), errno
The stack is private but not protected: since all live in one address space, a bad pointer can corrupt another thread's stack. Access to shared data needs synchronization.

Basic Concepts

Each Thread Has Its Own Stack

kernel space (inaccessible)main thread stack ↓T1 stack ↓guard pageT2 stack ↓guard pageshared libraries / mmapheap ↑ (malloc)global datacode (text)high addresslow addressT0 SPT1 SPT2 SP
  • Function calls and local variables must be kept separately for each flow: that is why every thread has its own stack.
  • On Linux/glibc a new thread's stack is allocated with mmap; the default size is usually the ulimit -s value (often 8 MiB).
  • The guard page at the end of the stack is inaccessible; an overflowing stack raises SIGSEGV instead of silently corrupting its neighbor.
  • Setting the size: pthread_attr_setstacksize, -Xss in Java.
  • The stack frames of Java virtual threads live on the heap, which allows millions of virtual threads.

Basic Concepts

Example: A Word Processor with Three Threads

document (in memory, shared)interactionformattingautosavekernelkeyboard / mousedisk
InteractionHandles key and mouse events at once; the UI never freezes.
FormattingReflows hundreds of pages in the background when a line is deleted.
AutosaveWrites the document to disk periodically; the user never waits on its I/O.

Three separate processes could not do this: all three must work on the same document. The shared address space is the key advantage here.

Basic Concepts

A Multithreaded Web Server

network connectionweb server processdispatcherworker 1worker 2worker 3web page cachekerneldiskrequest

A request arrives: the dispatcher thread reads it from the network.

network connectionweb server processdispatcherworker 1worker 2worker 3web page cachekerneldiskrequesthand off

The dispatcher picks an idle worker thread, hands it the request and wakes it up.

network connectionweb server processdispatcherworker 1worker 2worker 3web page cachekerneldiskrequesthand offfound ✓reply

Worker 1 finds the page in the cache and sends the reply; no disk access is needed.

network connectionweb server processdispatcherworker 1worker 2worker 3web page cachekerneldiskrequestmiss ✗read()worker 2 blocked;dispatcher and worker 3keep running

A new request goes to worker 2; if the page is not cached, worker 2 blocks on the disk. Meanwhile the dispatcher and worker 3 handle other requests.

Basic Concepts

Dispatcher and Worker Code

(a) dispatcher thread
while (TRUE) {
  get_next_request(&buf);  /* from net */
  handoff_work(&buf);      /* to worker */
}
(b) worker thread
while (TRUE) {
  wait_for_work(&buf);
  look_for_page_in_cache(&buf, &page);
  if (page_not_in_cache(&page))
    read_page_from_disk(&buf, &page);
  return_page(&page);
}
  • Both pieces of code are sequential and readable: a blocking read stops only that worker.
  • In a single-threaded server, a page that is not cached blocks the whole process; the CPU idles while the disk is awaited.
  • In a multithreaded server the work is handed to a worker and the server keeps accepting new requests.

Basic Concepts · Step through the code

Code Trace: A Multithreaded Web Server

/* dispatcher thread */
while (TRUE) {
  get_next_request(&buf);
  handoff_work(&buf);
}
/* worker thread */
while (TRUE) {
  wait_for_work(&buf);
  look_for_page_in_cache(&buf, &page);
  if (page_not_in_cache(&page))
    read_page_from_disk(&buf, &page);
  return_page(&page);
}
DispatcherrunningWorker W1blocked (awaiting work)Worker W2blocked (awaiting work)web cachea.htmldiskidle

The server process has three threads: the dispatcher listens on the network, the two workers are blocked in wait_for_work. Only a.html is in the cache.

DispatcherrunningWorker W1blocked (awaiting work)Worker W2blocked (awaiting work)request 1: b.htmlweb cachea.htmldiskidle

Request 1 arrives: b.html.

DispatcherrunningWorker W1readyWorker W2blocked (awaiting work)request 1 → W1web cachea.htmldiskidle

handoff_work gives the request to the idle worker, W1, and wakes it up. The dispatcher goes straight back to the network.

Dispatcherblocked (network)Worker W1runningWorker W2blocked (awaiting work)web cachea.htmldiskidle

W1 looks for the page in the cache: not there (a miss).

Dispatcherblocked (network)Worker W1blocked (disk)Worker W2blocked (awaiting work)web cachea.htmldiskreading…

W1 blocks to read from disk. Only W1 stops; the other threads in the same process keep running.

DispatcherrunningWorker W1blocked (disk)Worker W2blocked (awaiting work)request 2: a.htmlweb cachea.htmldiskreading…

While the disk works, the dispatcher takes request 2: a.html.

DispatcherrunningWorker W1blocked (disk)Worker W2readyrequest 2 → W2web cachea.htmldiskreading…

W1 is busy, so request 2 goes to W2.

Dispatcherblocked (network)Worker W1blocked (disk)Worker W2runningweb cachea.htmldiskreading…

W2 finds a.html in the cache: no need to go to disk.

Dispatcherblocked (network)Worker W1blocked (disk)Worker W2runningweb cachea.htmldiskreading…

W2 returns the page. Request 2 was answered before request 1.

Dispatcherblocked (network)Worker W1runningWorker W2blocked (awaiting work)web cachea.htmlb.htmldiskidle

The disk read is done: W1 wakes up, the page is also put in the cache and sent to client 1.

Dispatcherblocked (network)Worker W1blocked (awaiting work)Worker W2blocked (awaiting work)web cachea.htmlb.htmldiskidle

Both workers are waiting for work again. In a single-threaded server request 2 would have waited for the disk read; here each piece of code is still sequential and readable.

Basic Concepts

Three Ways to Build a Server

ModelParallelism / overlapSystem callsProgramming
Multithreaded processyesblockingsequential code, easy
Single-threaded processnoblockingeasy but slow
Finite-state machine (event-driven)yes (I/O overlaps)nonblocking + notification / interrupthard: each request's state is saved by hand
Today: the event-driven model is common in systems such as nginx, Node.js and Redis, using epoll and io_uring on Linux and kqueue on BSD/macOS. Most servers combine both: one event loop per core + a thread pool for blocking work.
multithreadedr1r1 waits for diskr1r2single-threadedr1disk: process blockedr1r2finite-state machiner1r1 state savedr1r2time → (r2 cached, r1 from disk)

Source: the comparison in Tanenbaum & Bos, Modern Operating Systems, Figure 2-10.

Basic Concepts

Benefits of Multithreading

Responsiveness

The program keeps responding even if part of it blocks or runs a long operation; especially important for user interfaces.

Resource sharing

Threads share the process's memory and files automatically; easier than setting up shared memory or message passing.

Economy

Creating and switching threads is cheaper than for processes: no address space is copied and the page tables do not change.

Scalability

A single multithreaded process can run on several cores at once; a single-threaded process uses only one core.

The oft-quoted figures “30× faster to create, 5× faster to switch” come from an old Solaris measurement. On Linux both processes and threads are created with clone(), so the gap is smaller; the real gain is not copying the memory map and keeping the TLB.

Basic Concepts

The Cost of a Context Switch

Thread → thread (same process)save registers, PC, SPload the other's registerssame address space: TLB and caches warmProcess → processsave registers, PC, SPload the other's registerschange page-table base (x86: CR3)TLB entries invalid (without PCID/ASID)caches cold: first accesses slowcheap
  • With user-level threads the switch is a library function call that never enters the kernel (no trap): cheaper still.
  • The direct cost is on the order of microseconds; the indirect cost (cold caches, TLB misses) is often larger.

Multicore Programming

Concurrency and Parallelism

ConcurrencySeveral tasks make progress over the same time interval; possible even on one core by interleaving.
ParallelismSeveral tasks execute at the same instant; needs at least two cores (or processing units).
single coreT1T2T3T4T1T2T3T4only ONE task runs at any instant; all progress over timetime →

Time sharing on one core: concurrent but not parallel.

core 1T1T3T1T3T1T3T1T3core 2T2T4T2T4T2T4T2T4TWO tasks run at the same instanttime →

On two cores T1/T3 and T2/T4 run at the same time: parallel execution.

single coreT1T2T3T4T1T2T3T4core 1T1T3T1T3T1T3T1T3core 2T2T4T2T4T2T4T2T4(concurrent)(parallel)time →

Parallelism requires concurrency; the converse is not true. The same program behaves differently on the two machines.

Multicore Programming

Challenges of Multicore Programming

Identifying tasksFinding independent activities in the application that can run in parallel.
BalanceTasks should be of similar size; dedicating a core to a tiny task gains nothing.
Data splittingThe data the tasks access must also be divided among the cores.
Data dependencyIf one task waits for another's result, access must be synchronized.
Testing and debuggingThe execution order can change on every run; bugs are hard to reproduce.
Memory effectsCores writing to the same cache line slow each other down (false sharing).
Tools: ThreadSanitizer (-fsanitize=thread), Helgrind, Java Flight Recorder; but no tool catches every race. Reducing sharing at design time is the most effective approach.

Multicore Programming

Data and Task Parallelism

Data parallelism12345678core 0sumcore 1sumcore 2sumcore 3sumsame operation · different parts of the dataTask parallelism12345678core 0mincore 1maxcore 2meancore 3std. dev.different operations · same data
  • Data parallelism: to sum an N-element array, each core sums N/4 elements and the partial sums are combined at the end (GPUs are the extreme case of this model).
  • Task parallelism: the work is split into independent tasks; the tasks may use the same or different data.
  • Real applications usually combine the two.

Multicore Programming

Amdahl's Law

speedup ≤ 1 / ( S + (1 − S) / N )

S: serial (non-parallelizable) fraction · N: number of cores · as N → ∞ the limit is 1 / S

serial 25%parallel 75%N = 1100% time1,00×speedup

Example: S = 0.25. On one core the time is 100%, the speedup 1×.

serial 25%parallel 75%N = 1100% time1,00×N = 262.5% time1,60×speedup

N = 2: 0.25 + 0.75/2 = 0.625 → speedup 1 / 0.625 = 1.60× (not 2×).

serial 25%parallel 75%N = 1100% time1,00×N = 262.5% time1,60×N = 443.75% time2,29×speedup

N = 4: 0.25 + 0.75/4 = 0.4375 → speedup 2.29×.

serial 25%parallel 75%N = 1100% time1,00×N = 262.5% time1,60×N = 443.75% time2,29×N → ∞25% time4,00×speedup

N → ∞: the parallel part shrinks toward zero and the serial 25% remains → at most 4×.

Multicore Programming

Amdahl Curves

12481632641282565121024number of cores N (log scale)05101520speedupP = 95% (limit 20)P = 90% (limit 10)P = 75% (limit 4)P = 50% (limit 2)
  • If 95% of the program is parallel, the theoretical limit on speedup is 1 / 0.05 = 20×; even on 1024 cores it is about 19.6×.
  • In practice synchronization and communication overhead push the speedup lower still. Gustafson's law: if the problem size grows with the cores, the scaled speedup is S + (1 − S)·N.

Multicore Programming · Step through the code

Code Trace: Amdahl Speedup

double S = 0.1;            /* serial fraction */
int N = 1;                 /* cores */
while (N <= 16) {
  double sp = 1 / (S + (1 - S) / N);
  printf("N=%2d  %.2fx\n", N, sp);
  N = N * 2;
}
printf("limit %.0fx\n", 1 / S);
0×5×10×124816N (cores)ideal

10% of the program is serial; 90% can be parallelized. On one core the time is 1 unit.

0×5×10×124816N (cores)ideal1,00×

N = 1: 0.1 + 0.9 / 1 = 1 → sp = 1.

0×5×10×124816N (cores)ideal1,00×

The result is printed and the core count doubles: N = 2.

0×5×10×124816N (cores)ideal1,82×

N = 2: 0.1 + 0.9 / 2 = 0.55 → sp = 1.82×.

0×5×10×124816N (cores)ideal3,08×

N = 4: 0.1 + 0.9 / 4 = 0.325 → sp = 3.08×.

0×5×10×124816N (cores)ideal4,71×

N = 8: 0.1 + 0.9 / 8 = 0.2125 → sp = 4.71×. Cores doubled; speedup rose by 53%.

0×5×10×124816N (cores)ideal6,40×

N = 16: 0.1 + 0.9 / 16 = 0.15625 → sp = 6.40×. Cores doubled; speedup rose by 36%.

0×5×10×124816N (cores)ideal

N = 32 > 16: the loop ends.

0×5×10×124816N (cores)ideallimit 1/S = 10×

As N → ∞ the parallel part shrinks to zero and the serial 10% remains: the speedup can never exceed 10×. Even 16 cores give only 6.4×.

Multithreading Models

User and Kernel Threads

(a) managed at user leveluser spacekernel spaceprocess 1run-time systemthread tableprocess 2run-time systemthread tableprocess tablekernel(b) managed by the kerneluser spacekernel spaceprocess 1process 2process tablethread tablekernel
  • User level: threads are managed in a library (the run-time system); the kernel thinks each process has one flow.
  • Kernel level: the kernel knows every thread and schedules it itself. This is how Linux, Windows and macOS work.

Multithreading Models

User-Level Threads

Advantages

  • Can be implemented as a library even on a kernel without thread support.
  • Each process keeps its own thread table: PC, registers, SP, state.
  • Switching is very fast: no trap, no kernel entry, the memory map does not change, the TLB is not flushed.
  • Each process can use its own scheduling algorithm.

Disadvantages

  • A blocking system call (e.g. read) stops the whole process, and so all its threads.
  • The workarounds are clumsy: wrappers (jackets) that test the call with select first, nonblocking calls.
  • A page fault also blocks the whole process.
  • Clock interrupts do not reach the library: if a thread never yields (yield), the others never run.
  • One process cannot use multiple cores.

Multithreading Models · Step through the code

Code Trace: thread_yield at User Level

enum { READY, RUNNING, WAITING };
struct tcb { int pc, sp, state; } T[3];
int cur = 0;               /* running */
void thread_yield(void) {  /* in library */
  T[cur].pc = PC;  T[cur].sp = SP;
  T[cur].state = READY;
  do cur = (cur + 1) % 3;
  while (T[cur].state != READY);
  T[cur].state = RUNNING;
  PC = T[cur].pc;  SP = T[cur].sp;
}
user space: thread table (library)pcspstateT00x1000x9000RUNNINGcurT10x3000xA000WAITINGT20x4800xB000READYCPU:PC=0x124 SP=0x8FF0kernel sees one flow

Three threads in one process. T0 is running (PC = 0x124 on the CPU); T1 is waiting for a lock in the library; T2 is ready. T0 voluntarily calls thread_yield().

user space: thread table (library)pcspstateT00x1240x8FF0RUNNINGcurT10x3000xA000WAITINGT20x4800xB000READYCPU:PC=0x124 SP=0x8FF0kernel sees one flow

T0's PC and SP are saved in the table. This is an ordinary function call: no trap, no kernel entry.

user space: thread table (library)pcspstateT00x1240x8FF0READYcurT10x3000xA000WAITINGT20x4800xB000READYCPU:PC=0x124 SP=0x8FF0kernel sees one flow

T0 is ready.

user space: thread table (library)pcspstateT00x1240x8FF0READYT10x3000xA000WAITINGcurT20x4800xB000READYCPU:PC=0x124 SP=0x8FF0kernel sees one flow

cur = 1.

user space: thread table (library)pcspstateT00x1240x8FF0READYT10x3000xA000WAITINGcurT20x4800xB000READYCPU:PC=0x124 SP=0x8FF0kernel sees one flow

T1 is WAITING, so it is skipped: the loop continues.

user space: thread table (library)pcspstateT00x1240x8FF0READYT10x3000xA000WAITINGT20x4800xB000READYcurCPU:PC=0x124 SP=0x8FF0kernel sees one flow

cur = 2.

user space: thread table (library)pcspstateT00x1240x8FF0READYT10x3000xA000WAITINGT20x4800xB000READYcurCPU:PC=0x124 SP=0x8FF0kernel sees one flow

T2 is READY: the search ends.

user space: thread table (library)pcspstateT00x1240x8FF0READYT10x3000xA000WAITINGT20x4800xB000RUNNINGcurCPU:PC=0x480 SP=0xB000kernel sees one flow

T2 is marked running and its registers are loaded: PC = 0x480. The return instruction now returns on T2's stack; the switch took a few instructions.

user space: thread table (library)pcspstateT00x1240x8FF0READYcurT10x3000xA000WAITINGT20x48C0xAFF8READYCPU:PC=0x48C SP=0xAFF8kernel sees one flow

After running a while (PC = 0x48C), T2 also calls thread_yield(): its state is saved, cur = 0 and T0 is ready.

user space: thread table (library)pcspstateT00x1240x8FF0RUNNINGcurT10x3000xA000WAITINGT20x48C0xAFF8READYCPU:PC=0x124 SP=0x8FF0kernel sees one flow

T0 resumes where it left off, at PC = 0x124. Had T2 never yielded, T0 would have waited forever: clock interrupts do not reach the library.

Multithreading Models

A Blocking Call: N:1 and 1:1

user spacekernel spaceU1U2U3K1run-time system (N:1)U1 running

Many-to-one (N:1): three user threads on a single kernel thread (K1); the library does the scheduling.

user spacekernel spaceU1U2U3K1run-time system (N:1)U1 called read() → K1 blocked → U2 and U3 wait too

U1 makes a blocking read() call: the kernel sees only K1 and blocks it → the whole process stops.

user spacekernel spaceU1U2U3K1K2K3blocked (I/O)core 0core 1

One-to-one (1:1): U1 blocking affects only K1; U2 and U3 run in parallel on two cores.

Multithreading Models

Kernel-Level Threads

  • The kernel keeps a thread table with the same information as at user level (registers, state…).
  • Creating and terminating threads are system calls: more expensive than at user level.
  • If a thread blocks, the kernel picks another; it need not be from the same process.
  • A page fault makes only the faulting thread wait.
  • A process's threads run truly in parallel on different cores.
  • Each thread takes kernel memory (TCB, kernel stack): their number is not unlimited.
Today

Linux (NPTL), Windows and macOS use the 1:1 model. With creation costs down, tens of thousands of kernel threads are no problem.

Limits on Linux: /proc/sys/kernel/threads-max, ulimit -u, the cgroup pids.max.

To observe them: ps -eLf, top -H, /proc/<pid>/task/.

userP1TTTP2TTP3Tkernelprocess table (3 processes)thread table (6 threads)scheduler picks

Multithreading Models

Mapping Models

user spacekernel spaceU1U2U3U4K1

Many-to-one (N:1): fast switching, but one blocking call stops the whole process and there is no parallelism. Example: Solaris green threads, GNU Portable Threads (historical).

user spacekernel spaceU1U2U3U4K1K2K3K4

One-to-one (1:1): true parallelism and independent blocking; every thread costs kernel resources. Linux, Windows, macOS.

user spacekernel spaceU1U2U3U4U5K1K2K3

Many-to-many (M:N): many user threads multiplexed onto fewer kernel threads; hard to implement.

user spacekernel spaceU1U2U3U4U5K1K2K3K4bound

Two-level / hybrid: M:N, plus a user thread can be bound to a single kernel thread.

Multithreading Models

Comparing the Models

CriterionN:11:1M:N
Switch costvery lowmedium (kernel)low
Blocking callwhole process stopsonly that threadonly that thread*
Multiple coresnot usableusedused
Number of threadsvery manylimited by kernel memoryvery many
Implementationeasy (library)easyhard (two schedulers)

* If the run-time notices the blocking call and moves to another kernel thread.

M:N was abandoned at the OS level: Solaris 9 and FreeBSD 7 moved to 1:1. But M:N is back in language run-times: Go goroutines, Erlang processes, Java 21 virtual threads.

Multithreading Models

Modern M:N: Virtual Threads

virtual threads (can be millions)V1V2V3V4V5V6V7V8V9parked: waiting for I/OJVM scheduler (M:N, ForkJoinPool)carrier 1carrier 2carrier 3carrier 4operating system (1:1)core 0core 1core 2core 3V3, V6, V8 are parked, not blocked: their carriers run other virtual threads
Java 21+ (Loom)Thread.ofVirtual().start(r); on a blocking I/O call the JVM unmounts the virtual thread from its carrier.
GoG (goroutine) · M (OS thread) · P (logical processor, GOMAXPROCS); an idle P steals work.

Multithreading Models

Pop-Up Threads

server processexisting threadsnetwork✉ messagemessage arriving…

(a) Before the message arrives: no thread in the process is waiting for it.

server processexisting threadsnetwork✉ messagepop-up threadnew, with an empty stackstarts at once

(b) When the message arrives, the system creates a new thread to handle it.

The alternative: a thread blocked in receive, whose state (registers, stack) must be restored for every message.

A pop-up thread is new, with no history to restore: low latency. It can be used inside the kernel too (interrupt handling).

/* on message arrival */
pthread_t t;
pthread_create(&t, NULL, handle, msg);
pthread_detach(t); /* no join */

Thread Libraries

Thread Libraries

POSIX PthreadsLinux, macOS, BSD; a C API. Creation, joining, mutexes, condition variables.
Windows APICreateThread, WaitForSingleObject; kernel level, 1:1.
JavaThread, Runnable, ExecutorService; the JVM uses OS threads (and, since Java 21, virtual threads).
C and C++ standardsC11 <threads.h>; C++11 std::thread, C++20 std::jthread (replacing the old Boost.Thread).
Pythonthreading; no parallelism for CPU work because of the GIL. A free-threaded (no-GIL) build: experimental in 3.13, supported in 3.14.
GoGoroutines with go f(); an M:N run-time, communication over channels.
Asynchronous threading: the parent starts a child and carries on with its own work (e.g. a server). Synchronous: the parent waits for its children to finish (join) and combines their results: fork-join.

Thread Libraries

POSIX Pthreads

  • The IEEE 1003.1c (POSIX.1c) standard: an API for thread creation and synchronization.
  • A specification, not an implementation: it says how the library behaves; it can be implemented at user or kernel level.
  • On Linux glibc NPTL (1:1); on macOS and the BSDs it also runs on kernel threads.
  • Compile with: gcc prog.c -pthread.
CallDescription
pthread_createCreates a new thread that runs the given function.
pthread_exitTerminates the calling thread.
pthread_joinWaits for a specific thread to finish.
pthread_detachIts resources are released automatically when it ends; it is not joined.
pthread_selfReturns the caller's own ID.
sched_yieldGives up the CPU. (pthread_yield is nonstandard and was deprecated in glibc 2.34.)
pthread_attr_init / _destroyInitializes / destroys an attribute object (stack size, detached state…).

Thread Libraries · Step through the code

Pthreads: Create and Join

int sum;              /* shared */
void *runner(void *param) {
  int upper = atoi(param);
  sum = 0;
  for (int i = 1; i <= upper; i++)
    sum += i;
  pthread_exit(0);
}
int main(void) {
  pthread_t tid;
  pthread_create(&tid, NULL, runner, "5");
  pthread_join(tid, NULL);
  printf("sum = %d\n", sum);
  return 0;
}
mainmain runningrunnernot yet

The process starts with one thread (main); the global sum is 0.

mainpthread_createrunnerready (eligible to run)

pthread_create creates the new thread; runner("5") will run.

mainpthread_join → waitingrunnerrunning

main blocks in join. The scheduler decides when runner starts.

mainwaitingrunnerupper = atoi("5")

runner creates the local variable upper on its own stack.

mainwaitingrunnerloop: 1+2+3+4+5

The loop updates the shared sum: 15.

mainwaking uprunnerpthread_exit(0) → done

runner terminates; join returns and main wakes up.

mainprintfrunnerdone

Thanks to join the output is the same on every run: sum = 15.

Adapted from Silberschatz Figure 4.11; the #include <pthread.h>, <stdio.h>, <stdlib.h> lines are not shown.

Thread Libraries

Multiple Threads

#define N 4
long partial[N];         /* one cell each */

void *add_range(void *arg) {
  long k = (long)arg;    /* 0 .. N-1 */
  for (long i = k*250 + 1; i <= (k+1)*250; i++)
    partial[k] += i;
  return NULL;
}
int main(void) {
  pthread_t t[N];
  for (long k = 0; k < N; k++)
    pthread_create(&t[k], NULL, add_range, (void *)k);
  long total = 0;
  for (int k = 0; k < N; k++) {
    pthread_join(t[k], NULL);
    total += partial[k];
  }
  printf("%ld\n", total);   /* 500500 */
}
  • The sum of 1..1000 is split among four threads: each adds up 250 numbers (data parallelism).
  • Each thread writes only its own partial[k] cell: no race.
  • main waits for all of them with join and combines the partial sums: the result is always 500500.
  • Passing &k as the argument would be a bug: the loop variable keeps changing.
  • Neighboring cells share a cache line: heavy writing causes false sharing; summing in a local variable and writing once at the end is better.

Thread Libraries · Step through the code

Passing Arguments and Getting Results

typedef struct { int lo, hi; } range_t;
typedef struct { long total; int count; } result_t;

void *add_range(void *arg) {
  range_t *a = arg;           /* void* → type */
  result_t *s = malloc(sizeof *s); /* on heap */
  s->total = 0; s->count = 0;
  for (int i = a->lo; i <= a->hi; i++) {
    s->total += i; s->count++;
  }
  return s;         /* = pthread_exit(s) */
}
int main(void) {
  range_t a = { 1, 100 };
  pthread_t t;  void *ret;
  pthread_create(&t, NULL, add_range, &a);
  pthread_join(t, &ret);          /* ret = s */
  result_t *s = ret;
  printf("%d numbers, total %ld\n", s->count, s->total);
  free(s);
  return 0;
}
main stackT stackheapa = {1, 100}no

The argument is the struct a on main's stack. That frame lives until main joins T, so passing its address is safe.

main stackT stackheapa = {1, 100}arg → a

There is a single void * parameter: several values go into a struct and its address is passed.

main stackT stackheapa = {1, 100}a = args → heaptotal = 0count = 0

T casts the void * to its real type. The result is allocated on the heap: T's own stack will vanish when T ends.

main stackT stackheapa = {1, 100}a = args → heapi = 101total = 5050count = 100

The loop sums 1..100: in the struct on the heap, total = 5050, count = 100.

main stackT stackheapa = {1, 100}framegonetotal = 5050count = 100

return s: the pointer is handed to whoever joins. T's local variables are now invalid, but the object on the heap lives on.

main stackT stackheapa = {1, 100}ret →s →T donetotal = 5050count = 100

pthread_join(t, &ret) writes the return value into ret through a void **; the cast is done in main.

main stackT stackheapa = {1, 100}ret →s →T done(free)

The result is printed and free(s) is called: ownership of the object passed to main through join.

Thread Libraries

A Common Bug: Returning a Result on the Stack

Wrong

void *add_range(void *arg) {
  result_t s = { 0, 0 }; /* on T’s stack */
  /* ... fill in s ... */
  return &s;            /* BUG */
}

Right

void *add_range(void *arg) {
  result_t *s = malloc(sizeof *s);
  if (s == NULL) return NULL;
  /* ... fill in s ... */
  return s;    /* on heap: lives on */
}  /* main: join, use, free(ret) */
mainT's stackpthread_join(t, &ret)waiting…add_range: s = {5050, 100}&s = 0x7f3a…e40T running: s is in add_range's stack frame

In the wrong version the result is the local variable s on T's stack, and &s is returned.

mainT's stackpthread_join(t, &ret)ret = 0x7f3a…e40frame removed,stack releasedretT returned: main still has the address, but the memory belongs to no one now

When the function returns its frame is gone; when the thread ends its whole stack is released or may be given to another thread.

mainT's stackpthread_join(t, &ret)ret = 0x7f3a…e40ret->total = ?garbage, crash or “correct”retmain reads it: undefined behavior

If main reads this address the result is undefined: a garbage value, SIGSEGV or, worst of all, a value that sometimes looks right. The heap version on the right is correct: malloc is checked for NULL and the result is freed after join.

The same rule applies to arguments: the address passed must stay alive as long as the thread uses it. gcc warns (-Wreturn-local-addr); treat the warning as an error.

Thread Libraries · Step through the code

Java: Thread, start and join

public class Sum {
  static int total = 0;
  public static void main(String[] a)
      throws InterruptedException {
    Thread t = new Thread(() -> {
      for (int i = 1; i <= 5; i++) total += i;
    });
    t.start();
    System.out.println("main: waiting");
    t.join();
    System.out.println("total = " + total);
  }
}
mainRUNNABLEtNEW (created, not started)

new Thread(lambda) only creates the object; the state is NEW.

mainRUNNABLEtRUNNABLE

start() starts a new OS thread (calling run() would not!).

mainprintlntRUNNABLE

main keeps printing; it may run at the same time as t.

mainWAITING (join)tRUNNABLE

join(): main is WAITING until t finishes.

mainwaking uptTERMINATED

t finishes the loop: total = 15, state TERMINATED.

mainprintlntTERMINATED

join also guarantees that main sees what t wrote (happens-before).

Compiled and run with JDK 21; the output is the same every time.

Thread Libraries

Java 21: Virtual Threads

import java.time.Duration;
import java.util.concurrent.*;
import java.util.stream.IntStream;

public class Virtual {
  public static void main(String[] args) {
    try (var ex = Executors.newVirtualThreadPerTaskExecutor()) {
      IntStream.range(0, 10_000).forEach(i ->
        ex.submit(() -> {
          Thread.sleep(Duration.ofSeconds(1));
          return i;
        }));
    }  // close(): waits for all tasks
    System.out.println("done");
  }
}
  • Each of the 10,000 tasks sleeps 1 s in its own virtual thread.
  • In our measurement (JDK 21) all finished in about 2 seconds: a few carrier threads were enough.
  • 10,000 platform threads would mean 10,000 kernel threads and their stacks.
  • Good fit: many tasks that wait on I/O (servers). No gain for CPU-heavy work.
  • Classic creation works too: Thread.ofVirtual().start(r), Thread.ofPlatform().

Implicit Threading

Implicit Threading

  • With hundreds of threads, managing correctness by hand gets hard.
  • The solution: the programmer identifies tasks (work that can run in parallel); compilers and run-time libraries create and manage the threads.
ApproachWhereIdea
Thread poolJava ExecutorService, Windows thread poolReady-made threads take tasks from a queue.
Fork-joinJava ForkJoinPool, parallel streamsSplit the task, solve in parallel, combine the results.
OpenMPC, C++, FortranParallel regions through compiler directives.
Grand Central DispatchmacOS, iOS (libdispatch)Blocks are placed on dispatch queues.
oneTBB (formerly Intel TBB)C++ template libraryparallel_for, task scheduling, work stealing.

Implicit Threading

Thread Pools

task queueG1G2G3G4G5G6submit(g)applicationpool (3 threads)worker 1idle, waitingworker 2idle, waitingworker 3idle, waiting

The pool creates a fixed number of threads up front; the application puts tasks in the queue.

task queueG4G5G6submit(g)applicationpool (3 threads)worker 1G1runningworker 2G2runningworker 3G3running

Idle workers take tasks from the queue: no thread-creation cost.

task queueG5G6submit(g)applicationpool (3 threads)worker 1G4runningworker 2G2runningworker 3G3running

When worker 1 finishes G1 it does not die; it takes the next task, G4, at once.

task queueemptysubmit(g)applicationpool (3 threads)worker 1idle, waitingworker 2idle, waitingworker 3idle, waiting

When the queue is empty the workers wait for the next task. The number of concurrent threads is bounded by the pool size.

Benefits: faster service, protection against too many threads, separating how a task runs (delayed, periodic) from the task itself.

Implicit Threading

Java ExecutorService

ExecutorService pool = Executors.newFixedThreadPool(2);
List<Future<Integer>> results = new ArrayList<>();
for (int g = 1; g <= 4; g++) {
  final int n = g;
  results.add(pool.submit(() -> n * n));
}
int total = 0;
for (Future<Integer> f : results)
  total += f.get();       // waits for result
System.out.println("total = " + total);
pool.shutdown();
Output (JDK 21)
total = 30
  • Four tasks (1², 2², 3², 4²) are given to a pool of two threads.
  • submit returns a Future; get() waits until the result is ready.
  • Pool size: ≈ the core count for CPU-bound work (availableProcessors()); more for I/O-bound work.
  • Other factories: newCachedThreadPool, newScheduledThreadPool, newVirtualThreadPerTaskExecutor.

Implicit Threading

Fork-Join

[3,1,4,1,5,9,2,6]task

The big task: the sum of 8 numbers. If it is above the threshold, split it.

[3,1,4,1,5,9,2,6][3,1,4,1][5,9,2,6]fork

fork: two subtasks are created; they may run on different threads in the pool.

[3,1,4,1,5,9,2,6][3,1,4,1][5,9,2,6][3,1]= 4[4,1]= 5[5,9]= 14[2,6]= 8fork + compute

The subtasks are split again; leaves at the threshold (2 elements) are computed directly: 4, 5, 14, 8.

[3,1,4,1,5,9,2,6][3,1,4,1]= 9[5,9,2,6]= 22[3,1]= 4[4,1]= 5[5,9]= 14[2,6]= 8join

join: a parent waits for its children and combines them: 4+5 = 9, 14+8 = 22.

[3,1,4,1,5,9,2,6]= 31[3,1,4,1]= 9[5,9,2,6]= 22[3,1]= 4[4,1]= 5[5,9]= 14[2,6]= 8join

Root result: 9 + 22 = 31. A typical case of synchronous threading.

Implicit Threading

Java ForkJoinPool and Work Stealing

class Sum extends RecursiveTask<Long> {
  protected Long compute() {
    if (hi - lo <= LIMIT) {       // small: do it
      long s = 0;
      for (int i = lo; i < hi; i++) s += a[i];
      return s;
    }
    int mid = (lo + hi) / 2;
    Sum left = new Sum(a, lo, mid);
    Sum right = new Sum(a, mid, hi);
    left.fork();                  // start async
    long rightSum = right.compute();
    return left.join() + rightSum;
  }
}
long s = ForkJoinPool.commonPool()
           .invoke(new Sum(a, 0, a.length));
worker 1abcdworker 2emptysteals from the head of the dequeowner takes from its own end (the tail)

Output for the array 1..100 000 (JDK 21): total = 5000050000. An idle worker steals tasks from another's deque (work stealing); the load balances itself.

Implicit Threading · Step through the code

Code Trace: Summing with Fork-Join

protected Long compute() {    // LIMIT = 2
  if (hi - lo <= LIMIT) {
    long s = 0;
    for (int i = lo; i < hi; i++)
      s += a[i];
    return s;
  }
  int mid = (lo + hi) / 2;
  Sum left = new Sum(a, lo, mid);
  Sum right = new Sum(a, mid, hi);
  left.fork();                   // to deque
  long rightSum = right.compute();
  return left.join() + rightSum;
}
27182818a[0..7][0,8)[0,4)[4,8)[0,2)[2,4)[4,6)[6,8)work deque: empty

The root task sums 8 elements. 8 − 0 = 8 > LIMIT, so it is not computed directly.

27182818a[0..7][0,8)[0,4)[4,8)[0,2)[2,4)[4,6)[6,8)work deque: [0,4)

The task is split in two. left.fork() puts [0,4) on the worker's deque; it does not run yet.

27182818a[0..7][0,8)[0,4)[4,8)[0,2)[2,4)[4,6)[6,8)work deque: [0,4)

The right half runs on the same thread, directly via compute().

27182818a[0..7][0,8)[0,4)[4,8)[0,2)[2,4)[4,6)[6,8)work deque: [0,4) [4,6)

[4,8) is split too: [4,6) goes on the deque.

27182818a[0..7][0,8)[0,4)[4,8)[0,2)[2,4)[4,6)[6,8) = 9work deque: [0,4) [4,6)

[6,8): 2 elements, at the limit. Summed directly: 1 + 8 = 9.

27182818a[0..7][0,8)[0,4)[4,8)[0,2)[2,4)[4,6) = 10[6,8) = 9work deque: [0,4)

left.join(): [4,6) is still at the end of the deque; nobody stole it. The worker pops it and runs it itself: 2 + 8 = 10.

27182818a[0..7][0,8)[0,4)[4,8) = 19[0,2)[2,4)[4,6) = 10[6,8) = 9work deque: [0,4)

[4,8) = 10 + 9 = 19, returned to the root.

27182818a[0..7][0,8)[0,4)[4,8) = 19[0,2)[2,4)[4,6) = 10[6,8) = 9work deque: [0,2)

The root calls left.join(): [0,4) is popped and run; it is split as well, and [0,2) goes on the deque.

27182818a[0..7][0,8)[0,4)[4,8) = 19[0,2)[2,4) = 9[4,6) = 10[6,8) = 9work deque: [0,2)

[2,4) is a leaf: 1 + 8 = 9.

27182818a[0..7][0,8)[0,4) = 18[4,8) = 19[0,2) = 9[2,4) = 9[4,6) = 10[6,8) = 9work deque: empty

join runs the leaf [0,2): 2 + 7 = 9; [0,4) = 9 + 9 = 18.

27182818a[0..7][0,8) = 37[0,4) = 18[4,8) = 19[0,2) = 9[2,4) = 9[4,6) = 10[6,8) = 9work deque: empty

Root: 18 + 19 = 37. If the pool had an idle worker, it could steal tasks such as [0,4) from the deque; the order changes, the result does not.

a = {2, 7, 1, 8, 2, 8, 1, 8}, a single-worker pool: new ForkJoinPool(1).invoke(new Sum(a, 0, 8)); output (JDK 21): total = 37.

Implicit Threading

OpenMP

#include <omp.h>
#include <stdio.h>

int main(void) {
  #pragma omp parallel
  printf("hello from %d\n", omp_get_thread_num());

  double a[1000], b[1000], c[1000], s = 0;
  /* ... fill a and b ... */
  #pragma omp parallel for reduction(+:s)
  for (int i = 0; i < 1000; i++) {
    c[i] = a[i] + b[i];
    s += c[i];
  }
  return 0;
}
  • Compiler directives and a library for C, C++ and Fortran, for shared-memory systems.
  • #pragma omp parallel: a parallel region; by default as many threads as cores (set with OMP_NUM_THREADS or num_threads(n)).
  • parallel for: splits the loop iterations among threads; reduction combines the partial sums safely.
  • Compile with gcc -fopenmp. The order of the “hello” lines can change on every run.

Implicit Threading

Grand Central Dispatch and oneTBB

  • Apple's run-time library and C/Objective-C language extension (blocks) for macOS/iOS: ^{ printf("I am a block"); }
  • Blocks are placed on dispatch queues; a serial queue runs them in order, a concurrent queue in parallel. QoS classes set the priority.
  • A block removed from a queue is assigned to an idle thread in the pool; thread management is entirely up to GCD.
dispatch_queue_t q = dispatch_get_global_queue(
                       QOS_CLASS_USER_INITIATED, 0);
dispatch_async(q, ^{ puts("I am a block"); });
dispatch queuesmain (serial)^^global (concurrent)^^^^system thread pool
oneTBB (the open-source successor of Intel Threading Building Blocks): tbb::parallel_for(0, n, [&](int i) { f(v[i]); }); Its own scheduler spreads tasks over threads using work stealing. In Swift, async/await has largely replaced GCD.

Threading Issues

fork() and exec() with Threads

parent processT1T2T3T2: fork()child processT2'only the thread that called fork is copiedA lock held by T1 may stay locked forever in the child!
  • Some UNIX systems offered two versions of fork (copying all threads or only the caller). POSIX: only the calling thread is copied.
  • That is right if exec is called right after fork: the new program replaces the whole address space anyway.
  • In the child, only async-signal-safe functions are safe until exec; pthread_atfork helps put locks in order. posix_spawn is often the better choice.

Threading Issues

Signal Handling

A signal notifies a process that an event has occurred. In a multithreaded process the question is: which thread should receive it?

OptionExample
To the thread the signal applies tosynchronous: SIGSEGV, SIGFPE
To every thread in the processsome termination cases
To any thread not blocking itPOSIX process signal: SIGINT
To a dedicated thread that receives all signalsa sigwait loop
  • Signal handlers are process-wide; the signal mask is per thread: pthread_sigmask.
  • To a specific thread: pthread_kill(tid, sig).
  • Good practice: block signals in all threads and let one thread wait with sigwait.
  • With user-level threads the kernel cannot address the right thread; it knows only the process.
  • Windows has no signals; the closest thing is the APC (asynchronous procedure call).
SIGINT (Ctrl+C)processT1: mask blocksT2: mask blocksT3: sigwait()

Threading Issues

Thread Cancellation

Asynchronous cancellationThe target is terminated immediately. It may die holding a lock or halfway through an update: dangerous.
Deferred cancellation (default)The target checks for a cancellation request at specific cancellation points and terminates in an orderly way.
maintarget Ttimepthread_cancel(T)request pending

main calls pthread_cancel(T); in deferred mode this is only a request, and T keeps running.

maintarget Ttimepthread_cancel(T)request pendingread()cancellation point

T reaches a cancellation point: read, sleep, pthread_cond_wait, pthread_testcancel()…

maintarget Ttimepthread_cancel(T)request pendingread()cancellation pointcleanup: unlock, free

The cleanup handlers registered with pthread_cleanup_push run: locks are released, memory is freed.

maintarget Ttimepthread_cancel(T)request pendingread()cancellation pointcleanup: unlock, free✕ donejoin → CANCELED

T terminates; pthread_join returns PTHREAD_CANCELED. Java's counterpart is the cooperative interrupt(); Thread.stop() has not worked since JDK 20.

Threading Issues

Conflicts over Global Variables

thread 1thread 2timeaccess("file") → errno = EACCEScontext switch →open("x") → errno = ENOENT← context switchread errno → ENOENT ✗errno (one copy)
  • Global variables designed for single-threaded programs (UNIX errno) clash between threads.
  • The fix: forbid the variable, or give each thread its own copy. In today's C libraries errno is thread-local.

Threading Issues

Thread-Local Storage (TLS)

T1T2int err = ?one copy: last writer wins

A plain global: int err; both threads read and write the same variable.

T1T2err = ENOENTerr = EACCESmain: —same name, separate copy per thread

_Thread_local int err; (C11) gives each thread a separate copy; T2 does not see what T1 wrote.

T1T2err = 1err = 2main: 0same name, separate copy per thread

Output of Java ThreadLocal (JDK 21): A: 1, B: 2, main: 0; main sees the initial value.

  • Unlike a local variable, it lives across function calls; like a global, but private to the thread.
  • C11 _Thread_local, C23/C++ thread_local, GCC __thread; Pthreads: pthread_key_create, pthread_setspecific.
  • Typical uses: errno, a transaction ID, a random-number generator.
static ThreadLocal<Integer> id =
  ThreadLocal.withInitial(() -> 0);

Threading Issues

Reentrant and Thread-Safe Code

  • Non-reentrant library routines keep their internal state in static variables.
  • Example: one thread puts a message in a shared buffer, a context switch occurs, another thread writes to the same buffer → the first message is lost.
  • While updating its free list, malloc leaves the list temporarily inconsistent; a thread cutting in may get an invalid pointer. Today's mallocs use internal locks and per-thread arenas.
  • Thread-safe: can be called from several threads at once (using locks, or stateless). Reentrant: uses no shared state.
UnsafeSafe version
strtokstrtok_r
localtimelocaltime_r
randrand_r / local generator
asctime, ctimestrftime
Java SimpleDateFormatDateTimeFormatter
T1: strtok("a,b,c", ",") → "a"T2: strtok("x;y", ";") → "x"static char *lastone copyT1: strtok(NULL, ",") → "y" ✗strtok_r: state kept by the caller

Threading Issues

Race Condition: counter++

LOAD  R, counter
ADD   R, 1
STORE counter, R

counter++ is one line, but on the machine it is three steps (load, add, store). Initially counter = 5; two threads increment it once each. Expected result: 7.

T1 (core 0)T2 (core 1)counter = 5shared memoryLOAD R1 ← counter → R1 = 5time

T1 loads the value into its register: R1 = 5.

T1 (core 0)T2 (core 1)counter = 5shared memoryLOAD R1 ← counter → R1 = 5LOAD R2 ← counter → R2 = 5time

T2 cuts in (or runs at the same time on the other core) and also reads 5.

T1 (core 0)T2 (core 1)counter = 6shared memoryLOAD R1 ← counter → R1 = 5LOAD R2 ← counter → R2 = 5ADD R2, 1 · STORE → counter = 6time

T2 increments and stores: counter = 6.

T1 (core 0)T2 (core 1)counter = 6shared memoryLOAD R1 ← counter → R1 = 5LOAD R2 ← counter → R2 = 5ADD R2, 1 · STORE → counter = 6ADD R1, 1 · STORE → counter = 6 ✗time

T1 continues from its old value: it stores 5 + 1 = 6. T2's increment is lost (a lost update).

T1 (core 0)T2 (core 1)counter = 7shared memorylock · LOAD 5 · ADD · STORE 6 · unlocklock · LOAD 6 · ADD · STORE 7 · unlockthe critical section runs one at a time: result 7 ✓C: pthread_mutex_lock / atomic_fetch_add · Java: synchronized / AtomicInteger

The fix: load-add-store must be atomic: a lock or an atomic operation.

Threading Issues

Observing the Race (Java)

public class Race {
  static int counter = 0;
  public static void main(String[] a) throws Exception {
    Runnable job = () -> {
      for (int i = 0; i < 1_000_000; i++) counter++;
    };
    Thread t1 = new Thread(job), t2 = new Thread(job);
    t1.start(); t2.start();
    t1.join();  t2.join();
    System.out.println("counter = " + counter);
  }
}
Three separate runs (JDK 21)
counter = 1456465
counter = 1544210
counter = 1910165
  • Expected 2,000,000; every run gives a different and smaller result.
  • The result is nondeterministic: it depends on scheduling, the number of cores and the JIT. That is why the output cannot be traced step by step.
  • Fixes: a synchronized block, AtomicInteger.incrementAndGet(), LongAdder.
  • Best of all: each thread counts in a local variable and the counts are combined at the end (the fork-join idea).

Threading Issues

Threads on Linux: clone()

fork(): copytask_struct Atask_struct Bmm (memory)mm (memory) ′filesfiles ′fsfs ′sighandsighand ′clone(CLONE_VM | CLONE_FILES | …): sharingtask_struct Atask_struct Bmm (memory)filesfssighand
  • Linux does not distinguish “process” from “thread”: all are tasks (task_struct).
  • The clone() flags decide what is shared; with no flags it behaves like fork.
  • pthread_create ≈ clone(CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND | CLONE_THREAD | …).
  • CLONE_THREAD: the same thread group; getpid() is the same for all (TGID), gettid() differs.

Summary

Advantages and Disadvantages

Advantages

  • Responsiveness: when one thread blocks, another handles user I/O.
  • Resource sharing: memory, open files, sockets.
  • Speed: creation and switching are cheaper than for processes.
  • Hardware parallelism: one process uses many cores.
  • Parallelism even with blocking calls (web server), an easy programming model.

Disadvantages

  • Synchronization: access to shared data must be controlled; risk of races and deadlock.
  • Dependence: threads of one process are not independent; if one crashes, the process crashes.
  • No memory protection: one thread can overwrite another's stack.
  • Debugging and testing are hard; bugs depend on timing.
Choose a process when isolation is needed (browser tabs, security boundaries), a thread when you need tight data sharing and low cost. Chrome uses both.

Summary

Review Questions (1/2)

  1. Which of register values, heap, global variables and stack are shared among the threads of a process?
  2. Is there concurrency on a single-core system? Is there parallelism?
  3. 80% of a program can be parallelized. What is the speedup on 8 cores, and what is the theoretical upper limit?
  4. In the many-to-one model, what happens if a thread blocks in read()? Why can it not benefit from multiple cores?
Answers
  1. The heap and global variables are shared; registers and the stack are private to each thread.
  2. Concurrency yes (time sharing), parallelism no.
  3. 1 / (0.2 + 0.8/8) = 1 / 0.3 ≈ 3.33×; limit 1 / 0.2 = 5×.
  4. The single kernel thread blocks and the whole process stops; since the kernel sees one flow, the process can run on only one processor core at a time.

Summary

Review Questions (2/2)

  1. Two threads each increment counter, initially 0, once. What final values are possible?
  2. Give two advantages of a thread pool over creating new threads.
  3. What is the difference between deferred and asynchronous cancellation? Why is deferred preferred?
  4. In POSIX, how many threads does the child have after fork() in a three-threaded process?
Answers
  1. 2, or (with a lost update) 1.
  2. Faster service with ready threads; a bound on the number of concurrent threads.
  3. Asynchronous cancellation kills at once (locks/data may be left inconsistent); with deferred cancellation the thread ends safely at a cancellation point.
  4. One: a copy of the thread that called fork.

Summary

References

  • A. Silberschatz, P. B. Galvin, G. Gagne, Operating System Concepts, 10th ed., Wiley, 2018 — Chapter 4: Threads & Concurrency.
  • A. S. Tanenbaum, H. Bos, Modern Operating Systems, 4th ed. (2014) / 5th ed. (2023), Pearson — Section 2.2: Threads.
  • R. H. Arpaci-Dusseau, A. C. Arpaci-Dusseau, Operating Systems: Three Easy Pieces (OSTEP), ostep.org — “Concurrency: An Introduction”, “Thread API”.
  • POSIX.1-2017 (IEEE Std 1003.1); the pthreads(7) and clone(2) Linux man pages.
  • JEP 444: Virtual Threads (Java 21); the OpenMP 5.2 specification.

The End

Chapter 4: Threads
Previous: Chapter 3: Processes · Next: Chapter 5: CPU Scheduling
1 / 1