Part 1 of 7 · 1 min read
Why this page comes fourth
In this module you will
- Explain why a mutex alone cannot make a thread wait for a condition.
- Use
pthread_cond_wait,pthread_cond_signalandpthread_cond_broadcastcorrectly. - Always write the wait inside a loop that tests the real condition, and explain why.
- Show a lost wakeup and how a flag prevents it.
- Describe a monitor and write a small one.
Page 3 gave exclusion: one thread at a time in a critical section. Many programs need something else: a thread must wait until another thread has made a condition true, such as data in a queue, or funds in an account. Polling in a loop wastes the processor, and polling with the lock held blocks everyone. This page gives the standard tool, which Level 2, page 5 used without explanation in the thread pool. The sources are "Operating Systems: Three Easy Pieces" (OSTEP), chapter 30, "Operating System Concepts" (OSC), chapter 6, "Modern Operating Systems" (MOS), chapter 2, and the manual page pthread_cond_wait(3). All programs were compiled with gcc -O2 -Wall -Wextra -pthread and run on a two-processor Linux machine.
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.