Part 1 of 7 · 1 min read
Why this page comes last in the level
In this module you will
- Measure how a shared counter behaves with one, two and four threads under a mutex, an atomic and a per-thread design.
- Build and explain an approximate (sloppy) counter, and state what it gives up.
- Build a hash table with one lock and with a lock for each bucket, and compare them.
- Explain why more threads can make a locked structure slower.
- Choose among the tools of this level for a given job.
Pages 1 to 8 gave the tools one by one. This page uses them on two small data structures that real programs need, and measures each design on the test machine. The method is the one to use in practice: write the simple correct version, measure it, find where the threads wait for each other, and change only that. The sources are "Operating Systems: Three Easy Pieces" (OSTEP), chapter 29, "Operating System Concepts" (OSC), chapters 6 and 7, and "The Linux Programming Interface" (TLPI), chapters 29 to 33. All programs were compiled with gcc -O2 -Wall -Wextra -pthread and run on a machine with two processors. Each timing is the median of five runs (three for the hash table), and runs varied by about 10 to 30 per cent.
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.