Threads Primer: A Guide to Multithreaded Programming
Today, there are three primary sets of multithreading (MT) libraries: the "standards-based" libraries (all of the UNIX® implementations, which will move to POSIX upon ratification), the OS/2® library, and the Windows NT(TM) library.(The NT and OS/2 libraries are fairly similar, which should not be too surprising.  NT did start life as OS/2, version 2, after all.) Although the APIs and implementations differ significantly, the fundamental concepts are the same. The ideas in this book are valid for all three; the details of the APIs differ.All the specific discussion in this book focuses on the Solaris(TM) 2 and POSIX multithreading models, with comparisons to OS/2 and NT throughout. We concentrate on the Solaris implementation in greatest detail. We do so because the Solaris threads library is currently the most advanced and robust implementation available, has the maximum exposure, the best thread-aware tool suite, and conforms most closely to the POSIX standard. (The fact that we both work for Sun has nothing to do with it.)A frank note about our motivation is in order here. We have slaved away for countless hours on this book because we're propeller-heads who honestly believe that this technology is a superb thing and that the widespread use of it will make the world a better place for hackers like ourselves.  Sun's motivation for assisting us in the project is to promote the usage of MT, because (a) this means you can write better, faster programs on Solaris, increasing sales of the operating system, and (b) Sun is far ahead of everybody else in both MT and multiprocessor (MP) machines, so it should leverage hardware sales, too.Your motivations for writing MT programs? You can write your programs better and more easily, they'll run faster, and you'll get them to market more quickly, they'll have fewer bugs, you'll have happier programmers, customers, and higher sales. The only losers in this game are the competitors, who lag behind Sun in MP hardware and MT software and will lag behind you in application speed and quality.MT is here today. It will soon be ubiquitous. As a professional programmer, you have an obligation to understand this technology. It may or may not be appropriate for your current project, but you must be able to make that conclusion yourself. This book will give you what you need to make that decision.Welcome to the world of the future!Who Should Use This BookThis book aims to give the programmer or technical manager a solid, basic understanding of threads-what they are, how they work, why they are useful, and some of the programming issues surrounding their use. As an introductory text, it does not attempt a deep, detailed analysis. After reading this book you should have a solid understanding of the fundamentals, be able to write credible, modestly complex, threaded programs and have the understanding necessary to analyze your own programs and determine the viability of threading them.This book is written with the experienced C/UNIX programmer in mind. A non-UNIX programmer will find a few of the details unfamiliar, but the concepts clear.A non-C programmer will find the code fragments and API descriptions mildly challenging, though possible to decipher, while the concepts should be clear. A technically minded nonprogrammer should be able to follow most of the concepts and understand the value of threads. A nontechnical person will not get much from this book.How This Book Is OrganizedChapter 1, Introduction - In which we present the motivation for creating thread libraries, discuss the advent of shared memory multiprocessors, and the interactions between threads and SMP machines.Chapter 2, Concepts - In which the reader is introduced to the basic concepts of multitasking operating systems and of multithreading as it compares to other programming paradigms. The reader is shown a set of reasons why multithreading is a valuable addition to programming paradigms, and a number of examples of successful deployment are presented.Chapter 3, Foundations - In which the reader is introduced to the underlying structures upon which threads are built, the construction of the thread itself, and the operating system support that allows an efficient implementation.Chapter 4, Scheduling - In which we explain the myriad details of the different scheduling models. After the reader is given a firm grounding in the fundamental concepts, we explain the various alternative choices that could have been made.  Finally, the reader is treated to a comprehensive explanation of the intricacies in the life of a thread.Chapter 5, Synchronization - In which the reader is led on a hunt for the intimidating synchronization variables and discovers that they are not actually as frightening as had been thought. After the hardware and software issues surrounding them are explained, the reader is shown the trade-offs involved in selecting the proper one to use. The chapter concludes with an explanation of thread-specific data.Chapter 6, Operating System Issues - In which we explore a variety of operating systems issues that bear heavily upon the usability of the threads library in actual programs. We also examine a set of general operating system functions and their value.Chapter 7, POSIX Threads (pthreads) - In which the details of the POSIX concepts are explained and contrasted to those of Solaris. A brief consideration of the issues of moving from Solaris threads to POSIX threads concludes the chapter.Chapter 8, Programming Tools - In which we consider the kinds of new tools that a reader would want when writing an MT program. Details of the Solaris tool set are discussed in the context of working with actual programs.Chapter 9, Programming With Threads - In which some pointers on programming with threads are given. Differences between single- threaded thinking and multithreaded thinking are emphasized.Chapter 10, Examples - In which several example programs are presented, and their  details and issues surrounding the way they use threads are discussed.Recommended ReadingAs of this writing, there are no other books specifically on the topic of multithreading concepts, although we know that several will be coming out within a year of this publication. Many of the operating system books and programming guides contain listings of the APIs but do not attempt to deal with the concepts of MT.Related BooksAdvanced Windows NT: The Developer's Guide to the Win32 Application Programming Interface. Jeffrey Richter. Microsoft Press, 1994. ISBN 1- 55615-567-0. This book contains about 200 pages that cover the NT threads API and its usage. It covers the API well, contains a good amount of code, but very little theory.Multithreaded Computer Architecture: A Summary of the State of the Art. Edited by Robert A. Iannucci. Kluwer Academic Publishers, 1994. ISBN 0-7923-9477-1. This book is a collection of papers dealing with hardware design considerations for building specialized machines that can support multithreaded programs.Programming with Threads. Steve Kleiman, Devang Shah, Bart Smaalders. Sun Microsystems Press/Prentice Hall PTR, Publication date late fall 1995. ISBN 0-13-172389-8. This book will be the definitive guide to developing multithreaded programs on UNIX, going into great depth on theory and practice.Real-World Programming for OS/2 2.1. Derrel R. Blain, Kurt R. Delimon, Jeff English. Sams Publishing/Prentice Hall PTR, 1993. ISBN 0-672- 30300-0.  This book contains about 50 pages that cover the OS/2 threads API and its usage. It covers the API well, contains one nice example but very little theory.Solaris Multithreaded Programming Guide. Sun Microsystems Press/Prentice Hall PTR, 1995.  ISBN 0-13-160896-7. This is the documentation that comes with Solaris and contains all the information about the API. It is also available as part of the Solaris AnswerBook® and via the threads page on the WWW (see Appendix\x11C, Threads on the Net).Using Multi-C: A Portable Multithreaded C Programming Library. Prentice Hall PTR, 1994. ISBN 0-13-606195-8. This book describes the API and use of the MIX Multi-C library, which is a proprietary library providing similar kinds of functionality to POSIX threads.Related PapersThere are a number of papers from technical conferences that go into great detail of the different aspects of multithreading. You can find those that we felt were most interesting on the thread's home page. (See Appendix C,  Threads on the Net.)