Log In
Or create an account ->
Imperial Library
Home
About
News
Upload
Forum
Help
Login/SignUp
Index
Pthreads Programming
A Note Regarding Supplemental Files
Examples
Preface
Organization
Example Programs
FTP
Typographical Conventions
Acknowledgments
1. Why Threads?
What Are Pthreads?
Potential Parallelism
Specifying Potential Parallelism in a Concurrent Programming Environment
UNIX Concurrent Programming: Multiple Processes
Creating a new process: fork
Pthreads Concurrent Programming: Multiple Threads
Creating a new thread: pthread_create
Threads are peers
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
2. Designing Threaded Programs
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
Handling asynchronous events: blocking with select
Handling file I/O: blocking with read/write
The Multithreaded ATM Server
Model: boss/worker model
The boss thread
Dynamically detaching a thread
A worker thread
Synchronization: what’s needed
Future enhancements
Example: A Matrix Multiplication Program
The Serial Matrix-Multiply Program
The Multithreaded Matrix-Multiply Program
Passing data to a new thread
Synchronization in the matrix-multiply program
3. Synchronizing Pthreads
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
Initializing a thread pool
Checking for work
Adding work
Deleting a thread pool
Adapting the atm_server_init and main routines
4. Managing Pthreads
Setting Thread Attributes
Setting a Thread’s Stack Size
Specifying the Location of a Thread’s Stack
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
Using a statically initialized mutex
Using the pthread_once mechanism
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
The bullet_proof thread: no effect
The ask_for_it thread: deferred cancellation
The sitting_duck thread: asynchronous cancellation
Cleanup Stacks
Cancellation in the ATM Server
Aborting a deposit
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
5. Pthreads and UNIX
Threads and Signals
Traditional Signal Processing
Sending signals and waiting for signals
Using a signal mask to block signals
Signal Processing in a Multithreaded World
Synchronously generated signals
Asynchronously generated signals
Per-thread signal masks
Per-process signal actions
Putting it all together
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
Alternative interfaces for functions that return static data
Additional routines for performance considerations
File-locking functions for threads
Where are the threadsafe functions?
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
Fork-handling stacks
Calling exec from a Thread
Process Exit and Threads
Multiprocessor Memory Synchronization
6. Practical Considerations
Understanding Pthreads Implementation
Two Worlds
Two Kinds of Threads
Who’s Providing the Thread?
User-space Pthreads implementations
Kernel thread–based Pthreads implementations
Two-level scheduler Pthreads implementations: the best of both worlds
Debugging
Deadlock
Race Conditions
Event Ordering
Less Is Better
Trace Statements
Debugger Support for Threads
Example: Debugging the ATM Server
Debugging a deadlock caused by a missing unlock
Debugging a race condition caused by a missing lock
Performance
The Costs of Sharing Too Much—Locking
Thread Overhead
Thread context switches
Synchronization Overhead
How Do Your Threads Spend Their Time?
Performance in the ATM Server Example
Performance depends on input workload: increasing clients and contention
Performance depends on a good locking strategy
Performance depends on the type of work threads do
Key performance issues between using threads and using processes
Conclusion
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
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
C. Pthreads Quick Reference
D. About the Authors
Index
About the Authors
Colophon
Copyright
← Prev
Back
Next →
← Prev
Back
Next →