Context switching rapidly moves a single CPU among multiple threads, creating the appearance of simultaneous execution. Modern multicore systems can also execute threads in parallel on different CPUs. section 16.1
Dinner-party analogy
Doing the cleaning, cooking, and table setting yourself while moving among tasks models concurrency. Having friends handle separate tasks models parallelism; coordinating shared pans and utensils illustrates mutual exclusion. section 16.1
Account for these consequences when designing multithreaded applications:
- Protect shared resources and synchronize threads. section 16.1
- Expect runtime scheduling to produce timing-dependent, irreproducible behavior. section 16.4
- Balance additional threads against mutex, condition-variable, and context-switching costs. section 16.4
Figure 16.1 A multithreaded application with three threads. The threads share the application’s internal resources of code and data in memory and external resources such as printers and files. The runtime system gives each thread its own runtime stack and the appearance of having its own set of machine registers.
Visualizes threads sharing code, data, printers, and files while receiving separate runtime stacks and the appearance of separate machine registers. section 16.1








