Home > Store > Programming > General Programming/Other Languages

Multithreaded Programming With PThreads

Register your product to gain access to bonus material or receive a coupon.

Multithreaded Programming With PThreads


  • Your Price: $27.96
  • List Price: $34.95
  • Usually ships in 24 hours.



  • Focuses on the POSIX multithreading model, making comparisons to OS/2 and Win32 throughout.
    • Contrasting POSIX API to the OS/2 and Win32 APIs gives the programmer a feeling for what kind of porting issues might be involved when writing to more than one of these libraries.

  • Promotes MT programming as a means for writing better programs more easily, and for having programs that run faster with fewer bugs.
  • Provides an introduction to the underlying structures upon which threads are built, the construction of the threads themselves, and the operating system support that allows efficient implementation.
  • Details the different scheduling models and the various alternative choices that could be made.
    • Describes context switching in detail.

    • Delves into the details of the various POSIX options and parameters.

  • Discusses hardware — cache architectures, interconnect topologies, atomic instructions, invalidation techniques.
  • Includes several complete programs which are presented as examples.
    • Details and issues of the threads used are discussed.

    • References to other programs on the net are made.

  • Covers the cancellation of unwanted threads.


  • Copyright 1998
  • Dimensions: 7" x 9-1/4"
  • Pages: 432
  • Edition: 1st
  • Book
  • ISBN-10: 0-13-680729-1
  • ISBN-13: 978-0-13-680729-2


Based on the best-selling Threads Primer, Multithreaded Programming with Pthreads gives you a solid understanding of Posix threads: what they are, how they work, when to use them, and how to optimize them.

It retains the clarity and humor of the Primer, but includes expanded comparisons to Win32 and OS/2 implementations. Code examples tested on all of the major UNIX platforms are featured along with detailed explanations of how and why they use threads.

In addition to scheduling, synchronization, signal handling, etc., special emphasis is placed on:

  • Cancellation: What does it mean to do bounded-time cancellation? What are the numerous details the programmer must deal with, and how do you avoid using cancellation at all?
  • Error expeditions: What do they all mean and what should you do about them?
  • Performance: How fast can a program run, and what are the limiting factors? Theoretical issues are considered and a case study for NFS is presented.
  • Hardware: A number of machine designs are examined (Sun, SGI, and DEC), focusing on issues of performance, synchronization instructions, bus design, and invalidation techniques.
  • Languages: The use of threads in other languages (Lisp, Eiffel, Smalltalk, etc.) is examined, with special emphasis on C++ and Java.
  • References: Pointers to the newsgroup, FAQs, web pages, code examples, other books, freeware tools, and commercial products are included.
  • More than anything else this is a practical book-it tells you what can and cannot be done with threads and why. In short, everything you need to know to build faster, smarter, multithreaded applications.

Sample Content

Table of Contents

1. Introduction.

2. Concepts.

Background: Traditional Operating Systems. What Is a Thread? Kernel Interaction. The Value of Using Threads. What Kind of Programs to Thread? What About Shared Memory? Threads Standards. Performance.

3. Foundations.

Implementation vs. Specification. Thread Libraries. The Process Structure. Lightweight Processes. Solaris Multithreaded Model. System Calls. Signals.

4. Lifecycle.

Thread Lifecycle.

5. Scheduling.

Different Models of Kernel Scheduling. Thread Scheduling. Context Switching. When Should You Care About Scheduling?

6. Synchronization.

Synchronization Issues. Synchronization Variables. A Stoppable Producer/Consumer Example.

7. Complexities.

Complex Locking Primitives. Other Synchronization Variables. Message Queues. Cross-Process Synchronization Variables. Initialization and Destruction. Synchronization Problems.

8. TSD.

Thread-Specific Data. Global Variables, Constants, and Cheating.

9. Cancellation.

What Cancellation Is. A Cancellation Example. Using Cancellation. Cancellation Safety. Simple Polling.

10. Signals.

Signals in UNIX. The Solaris Implementation of Signal Handling. Don't Use Signal Handlers!

11. Details.

Attribute Objects. POSIX Thread Ids. Win32 Thread IDs and Thread Handles. Initializing Your Data: pthread_once(). POSIX Namespace Restrictions. Return Values and Error Reporting. Constants Comments. Pthread Futures. Pthread Extensions. Comparing the OS/2, Win32, and POSIX Libraries.

12. Libraries.

The Threads Library. Multithreaded Kernels. Are Libraries Safe? Stub Functions in libc. New Semantics for System Calls. Are Libraries Safe? Threads Debugger Interface. Mixing Solaris Pthreads and UI Threads. Comparisons of Different Implementations.

13. Design.

Making Libraries Safe and Hot. Manipulating Lists. Program Design.

14. Languages.

C. C++. Java. Fortran. Ada. Pascal. Smalltalk. Lisp. Eiffel. Commercial Products. Public Pthreads Implementations.

15. Tools.

Static Lock Analyzer. Using a Thread-Aware, Graphical Debugger. Debug Mutexes. Protocol. TNFview.

16. Performance.

Optimization: Objectives and Objections. CPU Time, I/O Time, Contention, Etc. Limits on Speedup. Benchmarks and Repeatable Testing. The Lessons of NFS.

17. Hardware.

Types of Multiprocessors. Bus Architectures. Memory Systems.

18. Examples.

Threads and Windows. Socket Server (Master/Slave Version). Socket Server (Producer/Consumer Version). Other Programs on the Web.

Appendix A. Internet.

Threads Newsgroup. Code Examples. Vendor's Threads Pages. Threads Research. The SPILT Package for UI/POSIX Compatibility. Freeware Tools. Other Pointers. The Authors on the Net.

Appendix B. Books.

Threads Books. Related Books.

Appendix C. Timings.

Appendix D. Mistakes.

Appendix E. APIs.

Function Descriptions. Pthread Functions. Pthread Attributes Objects. POSIX Realtime Scheduling. Mutexes. Mutex Attributes Objects. Condition Variables. Condition Variable Attributes Objects. Cancellation Functions. Thread-Specific Data Functions. Semaphores. Signal Functions. Stdio.





Today, there are three primary sets of multithreading (MT) libraries: the “standards-based” libraries (all of the UNIX( implementations and VMS, which are moving to POSIX), the OS/2( library, and the Win32 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 POSIX multithreading model, with comparisons to OS/2 and Win32 throughout.

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.

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 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 Book

This 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. (At present, there are no deep, detailed analyses!) After reading this book the reader should have a solid understanding of the fundamentals, be able to write credible, modestly complex, threaded programs and have the understanding necessary to analyze their 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.

This book does not attempt to explain the usage of the Win32 or OS/2 APIs. It does contrast them to the POSIX API in order to give the POSIX programmer a feeling for what kind of porting issues might be involved when writing to more than one of these libraries.

How This Book Is Organized

  • Chapter 1, Introduction—In which we discuss the motivation for creating thread libraries, 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 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 we introduce 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, Lifecycle—In which the reader is treated to a comprehensive explanation of the intricacies in the life of a thread—birth, life, and death. Even death by vile cancellation. A small program which illustrates all of these stages concludes the chapter.
  • Chapter 5, Scheduling—In which we explain the myriad details of the different scheduling models and the various alternative choices that could be made, describe context switching in detail, and delve into gruesome detail of the various POSIX options and parameters. There is a light at the end of the tunnel, however.
  • Chapter 6, Synchronization—In which the reader is led on a hunt for the intimidating synchronization variable and discovers that it is not actually as frightening as had been thought. Programs illustrating the basic use of the POSIX primitives are shown.
  • Chapter 7, Complexities—In which a series of more complex synchronization variables and options are presented and the trade-off between them and the simpler ones are discussed. Synchronization problems and techniques for dealing with them conclude the chapter.
  • Chapter 8, TSD—In which an explanation of thread-specific data is provided, its use and some of the implementation details. We note a few places where use of TSD could be made more efficient and a few other optimizations that can be made.
  • Chapter 9, Cancellation—In which we describe the acrimonious nature of some programs and how unwanted threads may be disposed of. The highly complex issues surrounding bounded time termination and program correctness are also covered. A simple conclusion is drawn.
  • Chapter 10, Signals—In which we deal with the various aspects of handling asynchronous events in a multithreaded program. The definitions are given, alternative designs are discussed, and a program illustrating the most complex case is shown.
  • Chapter 11, Details—In which the details of actually writing and compiling an MT program are reviewed. The defined constants are described and methods of dealing with errors are proposed. We note which vendors have made extensions to Pthreads, and where POSIX is headed.
  • Chapter 12, Libraries—In which we explore a variety of operating systems issues that bear heavily upon the usability of the threads library in actual programs. We examine the status of library functions and the programming issues facing them. We look at some design alternatives for library functions.
  • Chapter 13, Design—In which we explore some designs for programs and library functions. Making both programs and individual functions more concurrent is a major issue in the design of these functions. We look at a variety of code examples and the trade-offs between them.
  • Chapter 14, Languages—In which the use of threads in various programming languages is considered and a few minor notes about special features and requirements are made. A number of public Pthreads libraries and some commercial products exist and are noted.
  • Chapter 15, Tools—In which we consider the kinds of new tools that a reader would want when writing a threaded program. An overview of the Solaris tool set is given, as representative of what should be looked for.
  • Chapter 16, Performance—In which we make things faster, look at general performance issues, political performance issues, and thread specific performance issues. We conclude with a discussion of the actual performance of multithreaded NFS.
  • Chapter 17, Hardware—In which we look at the various designs for SMP machines (cache architectures, interconnect topologies, atomic instructions, invalidation techniques) and consider how those designs affect our programming decisions. Some optimization possibilities are looked at.
  • Chapter 18, Examples—In which several complete programs are presented. The details and issues surrounding the way they use threads are discussed and references to other programs on the net are made.


Submit Errata

More Information

Unlimited one-month access with your purchase
Free Safari Membership