chapter sixteen

16 Designing multithreaded programs

 

This chapter covers

  • Designing and creating 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. Some applications inherently require simultaneous operations, and we can only design them to be multithreaded. We’ll look at a few typical examples such as an application where simultaneously multiple producer threads enter data into a queue while multiple consumer threads remove data from that queue.

16.1 How do things happen simultaneously?

16.2 A mutex enforces mutual exclusion

16.2.1 Protect the integrity of a shared printing resource

16.3 A semaphore accepts multiple threads

16.3.1 The classic reader-writer problem

16.4 Condition objects synchronize threads

16.4.1 How condition objects synchronize threads

16.4.2 The classic producer-consumer problem

16.5 A final note on multithreading

16.6 Summary