Pthreads Programming Contents
  Preface Contents
Organization
Example Programs
FTP
Typographical Conventions
Acknowledgments
Chapter 1 - - Why Threads
Overview
What Are Pthreads?
Potential Parallelism
Specifying Potential Parallelism in a Concurrent Programming Environment
UNIX Concurrent Programming: Multiple Processes
Pthreads Concurrent Programming: Multiple Threads
Parallel vs. Concurrent Programming
Synchronization
Sharing Process Resources
Communication
Scheduling
Who Am I? Who Are You?
Terminating Thread Execution
Exit Status and Return Values
Pthreads Library Calls and Errors
Why Use Threads Over Processes?
A Structured Programming Environment
Choosing Which Applications to Thread
Chapter 2 - - Designing Threaded Programs
Overview
Suitable Tasks for Threading
Models
Boss/Worker Model
Peer Model
Pipeline Model
Buffering Data Between Threads
Some Common Problems
Performance
Example: An ATM Server
The Serial ATM Server
The Multithreaded ATM Server
Example: A Matrix Multiplication Program
The Serial Matrix-Multiply Program
The Multithreaded Matrix-Multiply Program
Chapter 3 - - Synchronizing Pthreads
Overview
Selecting the Right Synchronization Tool
Mutex Variables
Using Mutexes
Error Detection and Return Values
Using pthread_mutex_trylock
When Other Tools Are Better
Some Shortcomings of Mutexes
Contention for a Mutex
Example: Using Mutexes in a Linked List
Complex Data Structures and Lock Granularity
Requirements and Goals for Synchronization
Access Patterns and Granularity
Locking Hierarchies
Sharing a Mutex Among Processes
Condition Variables
Using a Mutex with a Condition Variable
When Many Threads Are Waiting
Checking the Condition on Wake Up: Spurious Wake Ups
Condition Variable Attributes
Condition Variables and UNIX Signals
Condition Variables and Cancellation
Reader/Writer Locks
Synchronization in the ATM Server
Synchronizing Access to Account Data
Limiting the Number of Worker Threads
Synchronizing a Server Shutdown
Thread Pools
An ATM Server Example That Uses a Thread Pool
Chapter 4 - - Managing Pthreads
Overview
Setting Thread Attributes
Setting a Thread’s Stack Size
Setting a Thread’s Detached State
Setting Multiple Attributes
Destroying a Thread Attribute Object
The pthread_once Mechanism
Example: The ATM Server’s Communication Module
Keys: Using Thread-Specific Data
Initializing a Key: pthread_key_create
Associating Data with a Key
Retrieving Data from a Key
Destructors
Cancellation
The Complication with Cancellation
Cancelability Types and States
Cancellation Points: More on Deferred Cancellation
A Simple Cancellation Example
Cleanup Stacks
Cancellation in the ATM Server
Scheduling Pthreads
Scheduling Priority and Policy
Scheduling Scope and Allocation Domains
Runnable and Blocked Threads
Scheduling Priority
Scheduling Policy
Using Priorities and Policies
Setting Scheduling Policy and Priority
Inheritance
Scheduling in the ATM Server
Mutex Scheduling Attributes
Priority Ceiling
Priority Inheritance
The ATM Example and Priority Inversion
Chapter 5 - - Pthreads and UNIX
Overview
Threads and Signals
Traditional Signal Processing
Signal Processing in a Multithreaded World
Threads in Signal Handlers
A Simple Example
Some Signal Issues
Handling Signals in the ATM Example
Threadsafe Library Functions and System Calls
Threadsafe and Reentrant Functions
Example of Thread-Unsafe and Threadsafe Versions of the Same Function
Functions That Return Pointers to Static Data
Library Use of errno
The Pthreads Standard Specifies Which Functions Must Be Threadsafe
Using Thread-Unsafe Functions in a Multithreaded Program
Cancellation-Safe Library Functions and System Calls
Asynchronous Cancellation-Safe Functions
Cancellation Points in System and Library Calls
Thread-Blocking Library Functions and System Calls
Threads and Process Management
Calling fork from a Thread
Calling exec from a Thread
Process Exit and Threads
Multiprocessor Memory Synchronization
Chapter 6 - - Practical Considerations
Overview
Understanding Pthreads Implementation
Two Worlds
Two Kinds of Threads
Who’s Providing the Thread?
Debugging
Deadlock
Race Conditions
Event Ordering
Less Is Better
Trace Statements
Debugger Support for Threads
Example: Debugging the ATM Server
Performance
The Costs of Sharing Too Much—Locking
Thread Overhead
Synchronization Overhead
How Do Your Threads Spend Their Time?
Performance in the ATM Server Example
Conclusion
Appendix A - - Pthreads and DCE
The Structure of a DCE Server
What Does the DCE Programmer Have to Do?
Example: The ATM as a DCE Server
Appendix B - - Pthreads Draft 4 vs. the Final Standard
Detaching a Thread
Mutex Variables
Condition Variables
Thread Attributes
The pthread_once Function
Keys
Cancellation
Scheduling
Signals
Threadsafe System Interfaces
Error Reporting
System Interfaces and Cancellation-Safety
Process-Blocking Calls
Process Management
Appendix C - - Pthreads Quick Reference