This chapter covers
- Multiple threads of execution
- Protecting shared resources
- Synchronizing multiple threads of execution
Knowing how to design and develop multithreaded applications allows us to take advantage of a computer’s ability to handle multiple threads of execution. A thread is a path the computer takes through the code as it executes your program. Multithreading means the computer is running multiple paths at the same time.
If we design multithreaded applications properly, they can take better advantage of today’s multicore computers (i.e., computers with multiple CPUs). Some applications inherently require simultaneous operations, and we can only design them to be multithreaded.

This chapter introduces designing multithreaded applications. We’ll start with a simple printing example that demonstrates the need to use mutexes, which are software objects that use mutual exclusion to protect data shared by allowing only one thread at a time to modify the data. We’ll progress to using mutexes in different ways to solve an example of the classic reader–writer problem. Then, we’ll solve an example of the classic producer–consumer problem with other software objects called condition variables to more finely synchronize the execution of multiple threads.