Documentation Home
> Multithreaded Programming Guide
Multithreaded Programming Guide
Book Information
Preface
Chapter 1 Covering Multithreading Basics
Defining Multithreading Terms
Meeting Multithreading Standards
Benefiting From Multithreading
Improving Application Responsiveness
Using Multiprocessors Efficiently
Improving Program Structure
Using Fewer System Resources
Combining Threads and RPC
Understanding Basic Multithreading Concepts
Concurrency and Parallelism
Looking at Multithreading Structure
User-Level Threads
Lightweight Processes
Scheduling
Process Scope (Unbound Threads)
System Scope (Bound Threads)
Cancellation
Synchronization
Using the 64-bit Architecture
Chapter 2 Basic Threads Programming
The Threads Library
Create a Default Thread
pthread_create(3THR)
Return Values
Wait for Thread Termination
pthread_join(3THR)
Return Values
A Simple Threads Example
Detaching a Thread
pthread_detach(3THR)
Return Values
Create a Key for Thread-Specific Data
pthread_key_create(3THR)
Return Values
Delete the Thread-Specific Data Key
pthread_key_delete(3THR)
Return Values
Set the Thread-Specific Data Key
pthread_setspecific(3THR)
Return Values
Get the Thread-Specific Data Key
pthread_getspecific(3THR)
Return Values
Global and Private Thread-Specific Data Example
Get the Thread Identifier
pthread_self(3THR)
Return Values
Compare Thread IDs
pthread_equal(3THR)
Return Values
Initializing Threads
pthread_once(3THR)
Return Values
Yield Thread Execution
sched_yield(3RT)
Return Values
Set the Thread Priority
pthread_setschedparam(3THR)
Return Values
Get the Thread Priority
pthread_getschedparam(3THR)
Return Values
Send a Signal to a Thread
pthread_kill(3THR)
Return Values
Access the Signal Mask of the Calling Thread
pthread_sigmask(3THR)
Return Values
Forking Safely
pthread_atfork(3THR)
Terminate a Thread
pthread_exit(3THR)
Return Values
Finishing Up
Cancellation
Cancellation Points
Cancel a Thread
pthread_cancel(3THR)
Return Values
Enable or Disable Cancellation
pthread_setcancelstate(3THR)
Return Values
Set Cancellation Type
pthread_setcanceltype(3THR)
Return Values
Create a Cancellation Point
pthread_testcancel(3THR)
Push a Handler Onto the Stack
pthread_cleanup_push(3THR)
Pull a Handler Off the Stack
pthread_cleanup_pop(3THR)
Chapter 3 Thread Create Attributes
Attributes
Initialize Attributes
pthread_attr_init(3THR)
Return Values
Destroy Attributes
pthread_attr_destroy(3THR)
Return Values
Set Detach State
pthread_attr_setdetachstate(3THR)
Return Values
Get Detach State
pthread_attr_getdetachstate(3THR)
Return Values
Set Stack Guard Size
pthread_attr_setguardsize(3THR)
Return Value
Get Stack Guard Size
pthread_attr_getguardsize(3THR)
Return Value
Set Scope
pthread_attr_setscope(3THR)
Return Values
Get Scope
pthread_attr_getscope(3THR)
Return Values
Set Thread Concurrency Level
pthread_setconcurrency(3THR)
Return Value
Get Thread Concurrency Level
pthread_getconcurrency(3THR)
Return Value
Set Scheduling Policy
pthread_attr_setschedpolicy(3THR)
Return Values
Get Scheduling Policy
pthread_attr_getschedpolicy(3THR)
Return Values
Set Inherited Scheduling Policy
pthread_attr_setinheritsched(3THR)
Return Values
Get Inherited Scheduling Policy
pthread_attr_getinheritsched(3THR)
Return Values
Set Scheduling Parameters
pthread_attr_setschedparam(3THR)
Return Values
Get Scheduling Parameters
pthread_attr_getschedparam(3THR)
Return Values
Creating a Thread With a Specified Priority
Set Stack Size
pthread_attr_setstacksize(3THR)
Return Values
Get Stack Size
pthread_attr_getstacksize(3THR)
Return Values
About Stacks
Building Your Own Stack
Set Stack Address
pthread_attr_setstackaddr(3THR)
Return Values
Get Stack Address
pthread_attr_getstackaddr(3THR)
Return Values
Chapter 4 Programming with Synchronization Objects
Mutual Exclusion Lock Attributes
Initialize a Mutex Attribute Object
pthread_mutexattr_init(3THR)
Return Values
Destroy a Mutex Attribute Object
pthread_mutexattr_destroy(3THR)
Return Values
Set the Scope of a Mutex
pthread_mutexattr_setpshared(3THR)
Return Values
Get the Scope of a Mutex
pthread_mutexattr_getpshared(3THR)
Return Values
Set the Mutex Type Attribute
pthread_mutexattr_settype(3THR)
Return Values
Get the Mutex Type Attribute
pthread_mutexattr_gettype(3THR)
Set Mutex Attribute's Protocol
pthread_mutexattr_setprotocol(3T)
Return Values
Get Mutex Attribute's Protocol
pthread_mutexattr_getprotocol(3T)
Return Values
Set Mutex Attribute's Priority Ceiling
pthread_mutexattr_setprioceiling(3T)
Return Values
Get Mutex Attribute's Priority Ceiling
pthread_mutexattr_getprioceiling(3T)
Return Values
Set Mutex's Priority Ceiling
pthread_mutex_setprioceiling(3T)
Return Values
Get Mutex's Priority Ceiling
pthread_mutex_getprioceiling(3T)
Return Values
Set Mutex's Robust Attribute
pthread_mutexattr_setrobust_np(3T)
Return Values
Get Mutex's Robust Attribute
pthread_mutexattr_getrobust_np(3T)
Return Values
Using Mutual Exclusion Locks
Initialize a Mutex
pthread_mutex_init(3THR)
Return Values
Make Mutex Consistent
pthread_mutex_consistent_np(3T)
Return Values
Lock a Mutex
pthread_mutex_lock(3THR)
Return Values
Unlock a Mutex
pthread_mutex_unlock(3THR)
Return Values
Lock With a Nonblocking Mutex
pthread_mutex_trylock(3THR)
Return Values
Destroy a Mutex
pthread_mutex_destroy(3THR)
Return Values
Mutex Lock Code Examples
Using Locking Hierarchies
Nested Locking With a Singly Linked List
Nested Locking With a Circular Linked List
Condition Variable Attributes
Initialize a Condition Variable Attribute
pthread_condattr_init(3THR)
Return Values
Remove a Condition Variable Attribute
pthread_condattr_destroy(3THR)
Return Values
Set the Scope of a Condition Variable
pthread_condattr_setpshared(3THR)
Return Values
Get the Scope of a Condition Variable
pthread_condattr_getpshared(3THR)
Return Values
Using Condition Variables
Initialize a Condition Variable
pthread_cond_init(3THR)
Return Values
Block on a Condition Variable
pthread_cond_wait(3THR)
Return Values
Unblock a Specific Thread
pthread_cond_signal(3THR)
Return Values
Block Until a Specified Event
pthread_cond_timedwait(3THR)
Return Values
Unblock All Threads
pthread_cond_broadcast(3THR)
Return Values
Condition Variable Broadcast Example
Destroy Condition Variable State
pthread_cond_destroy(3THR)
Return Values
The Lost Wake-Up Problem
The Producer/Consumer Problem
Semaphores
Counting Semaphores
Initialize a Semaphore
sema_init(3THR)
Return Values
Initializing Semaphores With Intraprocess Scope
Initializing Semaphores With Interprocess Scope
Named Semaphores
Increment a Semaphore
sem_post(3THR)
Return Values
Block on a Semaphore Count
sema_wait(3THR)
Return Values
Decrement a Semaphore Count
sema_trywait(3THR)
Return Values
Destroy the Semaphore State
sema_destroy(3THR)
Return Values
The Producer/Consumer Problem, Using Semaphores
Read-Write Lock Attributes
Initialize a Read-Write Lock Attribute
pthread_rwlockattr_init(3THR)
Return Values
Destroy a Read-Write Lock Attribute
pthread_rwlockattr_destroy(3THR)
Return Values
Set a Read-Write Lock Attribute
pthread_rwlockattr_setpshared(3THR)
Return Value
Get a Read-Write Lock Attribute
pthread_rwlockattr_getpshared(3THR)
Return Value
Using Read-Write Locks
Initialize a Read-Write Lock
pthread_rwlock_init(3THR)
Return Value
Read Lock on Read-Write Lock
pthread_rwlock_rdlock(3THR)
Return Value
Read Lock With a Nonblocking Read-Write Lock
pthread_rwlock_tryrdlock(3THR)
Return Value
Write Lock on Read-Write Lock
pthread_rwlock_wrlock(3THR)
Return Value
Write Lock With a Nonblocking Read-Write Lock
pthread_rwlock_trywrlock(3THR)
Return Value
Unlock a Read-Write Lock
pthread_rwlock_unlock(3THR)
Return Value
Destroy a Read-Write Lock
pthread_rwlock_destroy(3THR)
Return Value
Synchronization Across Process Boundaries
Producer/Consumer Problem Example
Interprocess Locking Without the Threads Library
Comparing Primitives
Chapter 5 Programming With the Operating Environment
Process Creation--Forking Issues
The Fork-One Model
The Fork-One Safety Problem and Solution
Virtual Forks-vfork(2)
The Solution--pthread_atfork(3T)
Return Values
The Fork-All Model
Choosing the Right Fork
Cautions for Any Fork
Process Creation--exec(2)and exit(2) Issues
Timers, Alarms, and Profiling
Per-LWP POSIX Timers
Per-Thread Alarms
Profiling
Nonlocal Goto--setjmp(3C) and longjmp(3C)
Resource Limits
LWPs and Scheduling Classes
Timeshare Scheduling
Realtime Scheduling
LWP Scheduling and Thread Binding
SIGWAITING--Creating LWPs for Waiting Threads
Aging LWPs
Extending Traditional Signals
Synchronous Signals
Asynchronous Signals
Continuation Semantics
Operations on Signals
pthread_sigmask(3THR)
pthread_kill(3THR)
sigwait(2)
New sigwait() Implementations
sigtimedwait(2)
Thread-Directed Signals
Completion Semantics
Signal Handlers and Async-Signal Safety
Interrupted Waits on Condition Variables (Solaris Threads Only)
I/O Issues
I/O as a Remote Procedure Call
Tamed Asynchrony
Asynchronous I/O
Asynchronous I/O Operations
Shared I/O and New I/O System Calls
Alternatives to getc(3S) and putc(3S)
Chapter 6 Safe and Unsafe Interfaces
Thread Safety
MT Interface Safety Levels
Reentrant Functions for Unsafe Interfaces
Async-Signal-Safe Functions
MT Safety Levels for Libraries
Unsafe Libraries
Chapter 7 Compiling and Debugging
Compiling a Multithreaded Application
Preparing for Compilation
Choosing Solaris or POSIX Semantics
Including <thread.h> or <pthread.h>
Defining _REENTRANT or _POSIX_C_SOURCE
Linking With libthread or libpthread
Linking With -lposix4 for POSIX Semaphores
Link Old With New
Linking With the Alternate One-level libthread Library
Debugging a Multithreaded Program
Common Oversights
Tracing and Debugging With the TNF Utilities
Using truss(1)
Using adb(1)
Using dbx
Chapter 8 Programming With Solaris Threads
Comparing APIs for Solaris Threads and POSIX Threads
Major API Differences
Function Comparison Table
Unique Solaris Threads Functions
Suspend Thread Execution
thr_suspend(3THR)
Return Values
Continue a Suspended Thread
thr_continue(3THR)
Return Values
Set Thread Concurrency Level
thr_setconcurrency(3THR)
Return Values
Get Thread Concurrency
thr_getconcurrency(3THR)
Return Value
Similar Synchronization Functions--Read-Write Locks
Initialize a Read-Write Lock
rwlock_init(3THR)
Initializing Read-Write Locks With Intraprocess Scope
Initializing Read-Write Locks With Interprocess Scope
Return Values
Acquire a Read Lock
rw_rdlock(3THR)
Return Values
Try to Acquire a Read Lock
rw_tryrdlock(3THR)
Return Values
Acquire a Write Lock
rw_wrlock(3THR)
Return Values
Try to Acquire a Write Lock
rw_trywrlock(3THR)
Return Values
Unlock a Read-Write Lock
rw_unlock(3THR)
Return Values
Destroy Read-Write Lock State
rwlock_destroy(3THR)
Return Values
Read-Write Lock Example
Similar Solaris Threads Functions
Create a Thread
thr_create(3THR)
Return Values
Stack Behavior
Get the Minimal Stack Size
thr_min_stack(3THR)
Get the Thread Identifier
thr_self(3THR)
Yield Thread Execution
thr_yield(3THR)
Send a Signal to a Thread
thr_kill(3THR)
Access the Signal Mask of the Calling Thread
thr_sigsetmask(3THR)
Terminate a Thread
thr_exit(3THR)
Wait for Thread Termination
thr_join(3THR)
Join specific
Join any
Create a Thread-Specific Data Key
thr_keycreate(3THR)
Set the Thread-Specific Data Key
thr_setspecific(3THR)
Get the Thread-Specific Data Key
thr_getspecific(3THR)
Set the Thread Priority
thr_setprio(3THR)
Get the Thread Priority
thr_getprio(3THR)
Similar Synchronization Functions--Mutual Exclusion Locks
Initialize a Mutex
mutex_init(3THR)
Mutexes With Intraprocess Scope
Mutexes With Interprocess Scope
Mutexes With Interprocess Scope-Robust
Destroy a Mutex
mutex_destroy(3THR)
Acquire a Mutex
mutex_lock(3THR)
Release a Mutex
mutex_unlock(3THR)
Try to Acquire a Mutex
mutex_trylock(3THR)
Similar Synchronization Functions--Condition Variables
Initialize a Condition Variable
cond_init(3THR)
Condition Variables With Intraprocess Scope
Condition Variables With Interprocess Scope
Destroy a Condition Variable
cond_destroy(3THR)
Wait for a Condition
cond_wait(3THR)
Wait for an Absolute Time
cond_timedwait(3THR)
Signal One Condition Variable
cond_signal(3THR)
Signal All Condition Variables
cond_broadcast(3THR)
Similar Synchronization Functions--Semaphores
Initialize a Semaphore
sema_init(3THR)
Semaphores With Intraprocess Scope
Semaphores With Interprocess Scope
Increment a Semaphore
sema_post(3THR)
Block on a Semaphore Count
sema_wait(3THR)
Decrement a Semaphore Count
sema_trywait(3THR)
Destroy the Semaphore State
sem_destroy(3RT)
Synchronization Across Process Boundaries
Using LWPs Between Processes
Producer/Consumer Problem Example
Special Issues for fork() and Solaris Threads
Chapter 9 Programming Guidelines
Rethinking Global Variables
Providing for Static Local Variables
Synchronizing Threads
Single-Threaded Strategy
Reentrance
Code Locking
Data Locking
Invariants
Avoiding Deadlock
Deadlocks Related to Scheduling
Locking Guidelines
Following Some Basic Guidelines
Creating and Using Threads
Lightweight Processes
Unbound Threads
Bound Threads
Thread Concurrency (Solaris Threads Only)
Efficiency
Thread Creation Guidelines
Working With Multiprocessors
The Underlying Architecture
"Shared-Memory" Multiprocessors
Peterson's Algorithm
Parallelizing a Loop on a Shared-Memory Parallel Computer
Summary
Further Reading
Appendix A Sample Application--Multithreaded grep
Description of tgrep
Getting Online Source Code
Appendix B Solaris Threads Example: barrier.c
Appendix C MT Safety Levels: Library Interfaces
Index
Numbers and Symbols
A
B
C
D
E
F
G
H
I
J
K
L
M
N
P
R
S
T
U
V
W
X
© 2010, Oracle Corporation and/or its affiliates