16 Designing multithreaded programs

 

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.

16.1 How do things happen simultaneously?

 
 

16.2 A mutex enforces mutual exclusion

 
 
 

16.2.1 Protect the integrity of a shared resource

 

16.2.2 The classic reader–writer problem

 
 
 

16.3 Condition variables synchronize threads

 
 

16.3.1 How condition variables synchronize threads

 
 
 

16.3.2 The classic producer–consumer problem

 
 

16.4 A final note on multithreading

 
 
 

Summary

 
 
 
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest