Multithreaded Programming with Win32, 1st edition
Authors: Thuan Q. Pham, Pankaj K. Garg
Venue:  (1998)

From the Book:PREFACE: Preface In this book, we describe techniques for designing and implementing multithreaded software applications. Multithreaded programming can effectively improve efficiency and performance in an application program by introducing concurrency or parallelism. Until recently, concurrent programming required multiple processes, each with a single thread of execution, running concurrently on a multitasking operating system. In the past, concurrent programming was a tool used mostly by researchers and system implementors, that is, programmers of operating systems, networking, and other systems software. Modern operating systems like Solaris, AIX, DEC-UNIX, Windows 98, Windows NT, HP-UX, and Linux, all support multiple threads of control within the same address space. With the wide availability of multiprocessor computers in the coming years, multithreaded programming will be a viable and important technique for application programmers to master. With threads, a process can have multiple instruction streams executing simultaneously with much lower overhead than concurrent processes. Threads within the same address space inherently share that process's memory, which makes communication and data sharing among threads efficient. In addition, multithreading technology is absolutely essential for domains such as real-time multimedia and distributed systems. Andrew Birrell, in his excellent tutorial, Programming with Threads, lists five main benefits of multithreaded programming: (1) greater speed from multiprocessor computers, (2) improved program efficiency by avoiding waits associated with slow-speed I/O devices, (3) multiple inputs fromauser at the same time, (4) simpler server programs, and (5) responsive programs with computations judiciously spread over time. Developing programs that exploit these benefits of multithreading, however, is a challenge; the presence of parallelism introduces a new set of problems like resource sharing, deadlocks, race conditions, and so forth. The purpose of this book is both to introduce you to the concept of multithreaded programming, and to demonstrate how some of these challenges can be overcome. We have tried to cover all the important aspects of multithreaded programming, and to create a book that you will want to read as a primer book and use later as a reference guide. Outline of Chapters Chapter 1 introduces the basic concepts, history, benefits, and challenges of multithreaded programming. To provide an immediate hands-on experience, this chapter presents a small multithreaded program for you to experiment with right away. Chapter 2 introduces the Win32 thread interface and discusses its use. Here we also discuss the concepts of processes and threads, illustrate thread management functions, describe thread priorities and scheduling, and examine synchronization objects. In Chapter 3 we discuss the fundamental techniques for synchronizing threads of a multithreaded application. Using classic concurrency problems of producer-consumer, bounded-buffer, and readers-writers, we demonstrate synchronization techniques using the threads interface described in Chapter 2. Chapters 4 and 5 introduce monitors, an important abstraction and tool for multithreaded programming. While Chapter 4 describes the structure and properties of monitors, Chapter 5 simulates monitors using a combination of C++ classes and Win32 synchronization objects. Once again, we use concurrency problems of producer-consumer, bounded-buffer, and readers-writers to illustrate and demonstrate the usefulness of monitors in these chapters. Chapter 6 covers system deadlock in detail. In this chapter, we describe conditions for deadlock and explore techniques for avoiding it. Using the classic dining philosophers' problem, we demonstrate how to apply several deadlock- handling techniques like deadlock detection and recovery, prevention, and avoidance to application programs. Chapter 7 surveys a variety of thread package architectures, including user-level threads, kernel threads, multiplexed threads, and scheduler activations. We describe the impact of each thread architecture on the design and implementation of multithreaded programs. Chapter 8 covers the important topic of how to break up an application into multiple threads. We present illustrative programming models, including work groups, manager-worker, deferred computation, pipeline, and WorkCrew. Each model is illustrated using a sample program. In Chapter 9 we describe how threads can be used in the client and server components of a distributed application. Multithreaded programming plays an important role in this domain. We show the basic steps for constructing a distributed application using Microsofts' Remote Procedure Call (RPC) and Distributed Component Object Model (DCOM). The concurrent programming techniques covered in this book are well known in the computer science research community and are discussed in technical publications and several operating system textbooks. These concepts have, however, only recently become available for application programmers. The target audience for this book are application developers who want to experiment with multithreaded programming, and who have a working knowledge of the C programming language. Some parts of the book require a knowledge of the C++ language and the Visual C++ programming environement. We do not describe the threads API of the Microsoft Foundation Classes (MFC). The examples have been implemented on Intel Pentium machines running the Windows NT 4.0 Workstation operating system. Also, for clarity and brevity of exposition, we have removed error-checking code from the listings presented in the book except where such code is essential to the content under discussion. Acknowledgments Scott Marovich provided invaluable assistance with the preparation of this book: he carefully went through several versions, pointing out numerous corrections and improvements. Scott, thank you! We thank Anthony T. Nguyen for his early discussions with us on the book. We thank Nawaf Bitar for his valuable review and feedback for this book's proposal. We thank Debbie Caswell for her review of earlier versions of the book and encouraging comments about it. We thank Mary Franz, our editor at Prentice Hall, for initially suggesting the project, for patiently guiding us through the process, and for her eternal optimism. We thank Derrick Burns, Jill Huchital, Mehdi Jazayeri, John Krystynak, Srikanth Nadhamuni, and Thu Nguyen for their comments on earlier versions of the book. We thank Craig Little, our production editor, for patiently working with us on this project, and Scott Disanno for his careful copyediting of the book. Thuan thanks his wonderful wife, Nicole Anh-Dao, for all the support, encouragement, patience, and understanding that has made this project possible. Pankaj thanks his parents, Shri Harish Chand Garg and Smt. Savitri Garg for their love, affection and care. He thanks Puna, for her caring and wonderful companionship, his sister, Priti, and brother-in-law, Ajay, and all his friends for their encouragement and support.