Part 1 of 7 · 1 min read
Why this page comes fifth
In this module you will
- Explain why starting one thread per task does not scale.
- Build a thread pool with a work queue, a mutex and condition variables.
- Choose a pool size for work that computes and for work that waits.
- Show with a measurement how much a pool saves over thread-per-task.
- Describe an event loop built on
polland say when it beats threads.
Page 2 started a thread for each job, which is fine for a handful of jobs. A server may have thousands of requests a second, and the cost of a new thread for each one adds up, as does the memory of all those stacks. This page shows two answers that real servers use: a fixed group of worker threads fed from a queue, and a single thread that watches many connections at once. The pool needs a way for threads to wait for work, which is the first use of condition variables. Level 4 explains them fully. The main sources are "Operating System Concepts" (OSC), chapter 4, "The Linux Programming Interface" (TLPI), chapters 30 and 63, and "Operating Systems: Three Easy Pieces" (OSTEP), chapters 30 and 33. All programs were compiled with gcc -Wall -Wextra -pthread and run on Linux.
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.