CSE Roadmap

Core tier · about 4 hours · 6 parts

Part 1 of 6 · 1 min read

Why this page comes second

In this module you will

  • Create a thread with pthread_create, wait for it with pthread_join, and read its result.
  • Pass an argument to a thread safely, and avoid the classic bug of passing the address of a loop variable.
  • Split a computation across several threads and combine the answers.
  • Explain the difference between joinable and detached threads.
  • Predict what happens to other threads when main returns and when it calls pthread_exit.

Page 1 explained what a thread is and why a program uses one. This page shows the C calls that create and collect threads, using the POSIX threads library, usually called pthreads. POSIX is the standard that fixes the system calls and library functions of Unix-like systems, and pthreads is its thread part. It runs on Linux, macOS and others. Page 3 explains how the kernel runs these threads, and page 4 explains how to keep their shared data correct. The main sources are "The Linux Programming Interface" (TLPI), chapters 29 and 30, "Operating Systems: Three Easy Pieces" (OSTEP), chapter 27, and "Operating System Concepts" (OSC), chapter 4. All programs were compiled with gcc -Wall -Wextra -pthread and run on Linux. The flag -pthread is needed so that the compiler and the linker include the thread library.

About this example About this example

Real: The C programs, commands and outputs on this page were compiled with gcc -Wall -Wextra -pthread and run on a Linux test machine (2 processors, kernel 6.18, glibc 2.39).

Scripted: The worked traces use small numbers chosen so they can be followed by hand.

Not real: The page simplifies real systems. Actual kernels add more cases, limits and tuning than a short page can show.