Documentation Home
> Multithreaded Programming Guide
Multithreaded Programming Guide
Book Information
Index
Numbers and Symbols
A
B
C
D
E
F
G
I
J
K
L
M
N
P
R
S
T
U
V
W
X
Preface
Chapter 1 Covering Multithreading Basics
Multithreading Terms
Solaris Multithreading Libraries and Standards
Benefiting From Multithreading
Improving Application Responsiveness
Using Multiprocessors Efficiently
Improving Program Structure
Using Fewer System Resources
Combining Threads and RPC
Multithreading Concepts
Concurrency and Parallelism
Multithreading Structure
User-Level Threads
User-Level Threads State
Thread Scheduling
Thread Cancellation
Thread Synchronization
Using the 64-bit Architecture
Chapter 2 Basic Threads Programming
Lifecycle of a Thread
The Pthreads Library
Creating a Default Thread
pthread_create Syntax
pthread_create Return Values
Waiting for Thread Termination
pthread_join Syntax
pthread_join Return Values
Simple Threads Example
Detaching a Thread
pthread_detach Syntax
pthread_detach Return Values
Creating a Key for Thread-Specific Data
pthread_key_create Syntax
pthread_key_create Return Values
Deleting the Thread-Specific Data Key
pthread_key_delete Syntax
pthread_key_delete Return Values
Setting Thread-Specific Data
pthread_setspecific Syntax
pthread_setspecific Return Values
Getting Thread-Specific Data
pthread_getspecific Syntax
pthread_getspecific Return Values
Global and Private Thread-Specific Data Example
Getting the Thread Identifier
pthread_self Syntax
pthread_self Return Values
Comparing Thread IDs
pthread_equal Syntax
pthread_equal Return Values
Calling an Initialization Routine for a Thread
pthread_once Syntax
pthread_once Return Values
Yielding Thread Execution
sched_yield Syntax
sched_yield Return Values
Setting the Thread Policy and Scheduling Parameters
pthread_setschedparam Syntax
pthread_setschedparam Return Values
Getting the Thread Policy and Scheduling Parameters
pthread_getschedparam Syntax
pthread_getschedparam Return Values
Setting the Thread Priority
pthread_setschedprio Syntax
pthread_setschedprio Return Values
Sending a Signal to a Thread
pthread_kill Syntax
pthread_kill Return Values
Accessing the Signal Mask of the Calling Thread
pthread_sigmask Syntax
pthread_sigmask Return Values
Forking Safely
pthread_atfork Syntax
pthread_atfork Return Values
Terminating a Thread
pthread_exit Syntax
pthread_exit Return Values
Finishing Up
Cancel a Thread
Cancellation Points
Placing Cancellation Points
Cancelling a Thread
pthread_cancel Syntax
pthread_cancel Return Values
Enabling or Disabling Cancellation
pthread_setcancelstate Syntax
pthread_setcancelstate Return Values
Setting Cancellation Type
pthread_setcanceltype Syntax
pthread_setcanceltype Return Values
Creating a Cancellation Point
pthread_testcancel Syntax
pthread_testcancel Return Values
Pushing a Handler Onto the Stack
pthread_cleanup_push Syntax
pthread_cleanup_push Return Values
Pulling a Handler Off the Stack
pthread_cleanup_pop Syntax
pthread_cleanup_pop Return Values
Chapter 3 Thread Attributes
Attribute Object
Initializing Attributes
pthread_attr_init Syntax
pthread_attr_init Return Values
Destroying Attributes
pthread_attr_destroy Syntax
pthread_attr_destroy Return Values
Setting Detach State
pthread_attr_setdetachstate(3C) Syntax
pthread_attr_setdetachstate Return Values
Getting the Detach State
pthread_attr_getdetachstate Syntax
pthread_attr_getdetachstate Return Values
Setting the Stack Guard Size
pthread_attr_setguardsize(3C) Syntax
pthread_attr_setguardsize Return Values
Getting the Stack Guard Size
pthread_attr_getguardsize Syntax
pthread_attr_getguardsize Return Values
Setting the Scope
pthread_attr_setscope Syntax
pthread_attr_setscope Return Values
Getting the Scope
pthread_attr_getscope Syntax
pthread_attr_getscope Return Values
Setting the Thread Concurrency Level
pthread_setconcurrency Syntax
pthread_setconcurrency Return Values
Getting the Thread Concurrency Level
pthread_getconcurrency Syntax
pthread_getconcurrency Return Values
Setting the Scheduling Policy
pthread_attr_setschedpolicy(3C) Syntax
pthread_attr_setschedpolicy Return Values
Getting the Scheduling Policy
pthread_attr_getschedpolicy Syntax
pthread_attr_getschedpolicy Return Values
Setting the Inherited Scheduling Policy
pthread_attr_setinheritsched Syntax
pthread_attr_setinheritsched Return Values
Getting the Inherited Scheduling Policy
pthread_attr_getinheritsched Syntax
pthread_attr_getinheritsched Return Values
Setting the Scheduling Parameters
pthread_attr_setschedparam Syntax
pthread_attr_setschedparam Return Values
Getting the Scheduling Parameters
pthread_attr_getschedparam Syntax
Creating a Thread With a Specified Priority
Example of Creating a Prioritized Thread
pthread_attr_getschedparam Return Values
About Stacks
Allocating Stack Space for Threads
Building Your Own Stack
Setting the Stack Size
pthread_attr_setstacksize Syntax
pthread_attr_setstacksize Return Values
Getting the Stack Size
pthread_attr_getstacksize Syntax
pthread_attr_getstacksize Return Values
Setting the Stack Address and Size
pthread_attr_setstack(3C) Syntax
pthread_attr_setstack(3C) Return Values
Getting the Stack Address and Size
pthread_attr_getstack Syntax
pthread_attr_getstack Return Values
Chapter 4 Programming with Synchronization Objects
Mutual Exclusion Lock Attributes
Initializing a Mutex Attribute Object
pthread_mutexattr_init Syntax
pthread_mutexattr_init Return Values
Destroying a Mutex Attribute Object
pthread_mutexattr_destroy Syntax
pthread_mutexattr_destroy Return Values
Setting the Scope of a Mutex
pthread_mutexattr_setpshared Syntax
pthread_mutexattr_setpshared Return Values
Getting the Scope of a Mutex
pthread_mutexattr_getpshared Syntax
pthread_mutexattr_getpshared Return Values
Setting the Mutex Type Attribute
pthread_mutexattr_settype Syntax
pthread_mutexattr_settype Return Values
Getting the Mutex Type Attribute
pthread_mutexattr_gettype Syntax
pthread_mutexattr_gettype Return Values
Setting the Mutex Attribute's Protocol
pthread_mutexattr_setprotocol Syntax
pthread_mutexattr_setprotocol Return Values
Getting the Mutex Attribute's Protocol
pthread_mutexattr_getprotocol Syntax
pthread_mutexattr_getprotocol Return Values
Setting the Mutex Attribute's Priority Ceiling
pthread_mutexattr_setprioceiling Syntax
pthread_mutexattr_setprioceiling Return Values
Getting the Mutex Attribute's Priority Ceiling
pthread_mutexattr_getprioceiling Syntax
pthread_mutexattr_getprioceiling Return Values
Setting the Mutex's Priority Ceiling
pthread_mutex_setprioceiling Syntax
pthread_mutex_setprioceiling Return Values
Getting the Mutex's Priority Ceiling
pthread_mutex_getprioceiling Syntax
pthread_mutex_getprioceiling Return Values
Setting the Mutex's Robust Attribute
pthread_mutexattr_setrobust_np Syntax
pthread_mutexattr_setrobust_np Return Values
Getting the Mutex's Robust Attribute
pthread_mutexattr_getrobust_np Syntax
pthread_mutexattr_getrobust_np Return Values
Using Mutual Exclusion Locks
Initializing a Mutex
pthread_mutex_init Syntax
pthread_mutex_init Return Values
Making a Mutex Consistent
pthread_mutex_consistent_np Syntax
pthread_mutex_consistent_np Return Values
Locking a Mutex
pthread_mutex_lock Syntax
pthread_mutex_lock Return Values
Unlocking a Mutex
pthread_mutex_unlock Syntax
pthread_mutex_unlock Return Values
Locking a Mutex Without Blocking
pthread_mutex_trylock Syntax
pthread_mutex_trylock Return Values
Locking a Mutex Before a Specified Absolute Time
pthread_mutex_timedlock() Syntax
pthread_mutex_timedlock() Return Values
Locking a Mutex Within a Specified Time Interval
pthread_mutex_reltimedlock_np() Syntax
pthread_mutex_reltimedlock_np() Return Values
Destroying a Mutex
pthread_mutex_destroy Syntax
pthread_mutex_destroy Return Values
Code Examples of Mutex Locking
Examples of Using Lock Hierarchies
Examples of Using Nested Locking With a Singly-Linked List
Example of Nested Locking With a Circularly-Linked List
Using Spin Locks
Initializing a Spin Lock
pthread_spin_init() Syntax
pthread_spin_init() Return Values
Acquiring a Spin Lock
pthread_spin_lock() Syntax
pthread_spin_lock() Return Values
Acquiring a Non-Blocking Spin Lock
pthread_spin_trylock() Syntax
pthread_spin_trylock() Return Values
Unlocking a Spin Lock
pthread_spin_unlock() Syntax
pthread_spin_unlock() Return Values
Destroying a Spin Lock
pthread_spin_destroy() Syntax
pthread_spin_destroy() Return Values
Condition Variable Attributes
Initializing a Condition Variable Attribute
pthread_condattr_init Syntax
pthread_condattr_init Return Values
Removing a Condition Variable Attribute
pthread_condattr_destroy Syntax
pthread_condattr_destroy Return Values
Setting the Scope of a Condition Variable
pthread_condattr_setpshared Syntax
pthread_condattr_setpshared Return Values
Getting the Scope of a Condition Variable
pthread_condattr_getpshared Syntax
pthread_condattr_getpshared Return Values
Setting the Clock Selection Condition Variable
pthread_condattr_setclock Syntax
pthread_condattr_setclock Returns
Getting the Clock Selection Condition Variable
pthread_condattr_getclock Syntax
pthread_condattr_getclock Returns
Using Condition Variables
Initializing a Condition Variable
pthread_cond_init Syntax
pthread_cond_init Return Values
Blocking on a Condition Variable
pthread_cond_wait Syntax
pthread_cond_wait Return Values
Unblocking One Thread
pthread_cond_signal Syntax
pthread_cond_signal Return Values
Blocking Until a Specified Time
pthread_cond_timedwait Syntax
pthread_cond_timedwait Return Values
Blocking For a Specified Interval
pthread_cond_reltimedwait_np Syntax
pthread_cond_reltimedwait_np Return Values
Unblocking All Threads
pthread_cond_broadcast Syntax
pthread_cond_broadcast Return Values
Destroying the Condition Variable State
pthread_cond_destroy Syntax
pthread_cond_destroy Return Values
Lost Wake-Up Problem
Producer and Consumer Problem
Synchronization With Semaphores
Named and Unnamed Semaphores
Counting Semaphores Overview
Initializing a Semaphore
sem_init Syntax
Initializing Semaphores With Intraprocess Scope
Initializing Semaphores With Interprocess Scope
sem_init Return Values
Incrementing a Semaphore
sem_post Syntax
sem_post Return Values
Blocking on a Semaphore Count
sem_wait Syntax
sem_wait Return Values
Decrementing a Semaphore Count
sem_trywait Syntax
sem_trywait Return Values
Destroying the Semaphore State
sem_destroy Syntax
sem_destroy Return Values
Producer and Consumer Problem Using Semaphores
Read-Write Lock Attributes
Initializing a Read-Write Lock Attribute
pthread_rwlockattr_init Syntax
pthread_rwlockattr_init Return Values
Destroying a Read-Write Lock Attribute
pthread_rwlockattr_destroy Syntax
pthread_rwlockattr_destroy Return Values
Setting a Read-Write Lock Attribute
pthread_rwlockattr_setpshared Syntax
pthread_rwlockattr_setpshared Return Values
Getting a Read-Write Lock Attribute
pthread_rwlockattr_getpshared Syntax
pthread_rwlockattr_getpshared Return Values
Using Read-Write Locks
Initializing a Read-Write Lock
pthread_rwlock_init Syntax
pthread_rwlock_init Return Values
Acquiring the Read Lock on Read-Write Lock
pthread_rwlock_rdlock Syntax
pthread_rwlock_rdlock Return Values
Acquiring a Read Lock on a Read-Write Lock Before a Specified Absolute Time
pthread_rwlock_timedrdlock Syntax
pthread_rwlock_timedrdlock Return Values
Acquiring a Non-Blocking Read Lock on a Read-Write Lock
pthread_rwlock_tryrdlock Syntax
pthread_rwlock_tryrdlock Return Values
Acquiring the Write Lock on a Read-Write Lock
pthread_rwlock_wrlock Syntax
pthread_rwlock_wrlock Return Values
Acquiring a Non-blocking Write Lock on a Read-Write Lock
pthread_rwlock_trywrlock Syntax
pthread_rwlock_trywrlock Return Values
Acquiring a Write Lock on a Read-Write Lock Before a Specified Absolute Time
pthread_rwlock_timedwrlock Syntax
pthread_rwlock_timedwrlock Returns
Unlocking a Read-Write Lock
pthread_rwlock_unlock Syntax
pthread_rwlock_unlock Return Values
Destroying a Read-Write Lock
pthread_rwlock_destroy Syntax
pthread_rwlock_destroy Return Values
Using Barrier Synchronization
Initializing a Synchronization Barrier
pthread_barrier_init() Syntax
pthread_barrier_init() Return Values
Waiting for Threads to Synchronize at a Barrier
pthread_barrier_wait() Syntax
pthread_barrier_wait() Return Values
Destroying a Synchronization Barrier
pthread_barrier_destroy Syntax
pthread_barrier_destroy Return Values
Initializing a Barrier Attributes Object
pthread_barrierattr_init() Syntax
pthread_barrierattr_init() Return Values
Setting a Barrier Process-Shared Attribute
pthread_barrierattr_setpshared() Syntax
pthread_barrierattr_setpshared() Return Values
Getting a Barrier Process-Shared Attribute
pthread_barrierattr_getpshared() Syntax
pthread_barrierattr_getpshared() Return Values
Destroying a Barrier Attributes Object
pthread_barrierattr_destroy() Syntax
pthread_barrierattr_destroy() Return Values
Synchronization Across Process Boundaries
Producer and Consumer Problem Example
Comparing Primitives
Chapter 5 Programming With the Solaris Software
Forking Issues in Process Creation
Fork-One Model
Fork-One Safety Problem and Solution
Virtual Forks–vfork
Solution: pthread_atfork
Fork-All Model
Choosing the Right Fork
Process Creation: exec and exit Issues
Timers, Alarms, and Profiling
Timers
Alarms
Profiling a Multithreaded Program
Nonlocal Goto: setjmp and longjmp
Resource Limits
LWPs and Scheduling Classes
Timeshare Scheduling
Realtime Scheduling
Fair Share Scheduling
Fixed Priority Scheduling
Extending Traditional Signals
Synchronous Signals
Asynchronous Signals
Continuation Semantics
Operations on Signals
Setting the Thread's Signal Mask
Sending a Signal to a Specific Thread
Waiting for a Specified Signal
Waiting for Specified Signal Within a Given Time
Thread-Directed Signals
Completion Semantics
Signal Handlers and Async-Signal Safety
Interrupted Waits on Condition Variables
I/O Issues
I/O as a Remote Procedure Call
Tamed Asynchrony
Asynchronous I/O
Asynchronous I/O Operations
Waiting for I/O Operation to Complete
Shared I/O and New I/O System Calls
Alternatives to getc and putc
Chapter 6 Programming With Solaris Threads
Comparing APIs for Solaris Threads and POSIX Threads
Major API Differences
Function Comparison Table
Unique Solaris Threads Functions
Suspending Thread Execution
thr_suspend Syntax
thr_suspend Return Values
Continuing a Suspended Thread
thr_continue Syntax
thr_continue Return Values
Similar Synchronization Functions—Read-Write Locks
Initialize a Read-Write Lock
rwlock_init Syntax
Initializing Read-Write Locks With Intraprocess Scope
Initializing Read-Write Locks With Interprocess Scope
rwlock_init Return Values
Acquiring a Read Lock
rw_rdlock Syntax
rw_rdlock Return Values
Trying to Acquire a Read Lock
rw_tryrdlock Syntax
rw_tryrdlock Return Values
Acquiring a Write Lock
rw_wrlock Syntax
rw_wrlock Return Values
Trying to Acquire a Write Lock
rw_trywrlock Syntax
rw_trywrlock Return Values
Unlock a Read-Write Lock
rw_unlock Syntax
rw_unlock Return Values
Destroying the Read-Write Lock State
rwlock_destroy Syntax
rwlock_destroy Return Values
Similar Solaris Threads Functions
Creating a Thread
thr_create Syntax
thr_create Return Values
Getting the Minimal Stack Size
thr_min_stack Syntax
thr_min_stack Return Values
Acquiring the Thread Identifier
thr_self Syntax
thr_self Return Values
Yield Thread Execution
thr_yield Syntax
thr_yield Return Values
Send a Signal to a Thread
thr_kill Syntax
thr_kill Return Values
Access the Signal Mask of the Calling Thread
thr_sigsetmask Syntax
thr_sigsetmask Return Values
Terminate a Thread
thr_exit Syntax
thr_exit Return Values
Wait for Thread Termination
thr_join Syntax
thr_join, Join Specific
thr_join, Join Any
thr_join Return Values
Creating a Thread-Specific Data Key
thr_keycreate Syntax
thr_keycreate Return Values
Setting the Thread-Specific Data Value
thr_setspecific Syntax
thr_setspecific Return Values
Getting the Thread-Specific Data Value
thr_getspecific Syntax
thr_getspecific Return Values
Set the Thread Priority
thr_setprio Syntax
thr_setprio Return Values
Get the Thread Priority
thr_getprio Syntax
thr_getprio Return Values
Similar Synchronization Functions—Mutual Exclusion Locks
Initialize a Mutex
mutex_init(3C) Syntax
Mutexes With Intraprocess Scope
Mutexes With Interprocess Scope
Mutexes With Interprocess Scope-Robust
mutex_init Return Values
Destroy a Mutex
mutex_destroy Syntax
mutex_destroy Return Values
Acquiring a Mutex
mutex_lock Syntax
mutex_lock Return Values
Releasing a Mutex
mutex_unlock Syntax
mutex_unlock Return Values
Trying to Acquire a Mutex
mutex_trylock Syntax
mutex_trylock Return Values
Similar Synchronization Functions: Condition Variables
Initialize a Condition Variable
cond_init Syntax
Condition Variables With Intraprocess Scope
Condition Variables With Interprocess Scope
cond_init Return Values
Destroying a Condition Variable
cond_destroy Syntax
cond_destroy Return Values
Waiting for a Condition
cond_wait Syntax
cond_wait Return Values
Wait for an Absolute Time
cond_timedwait Syntax
cond_timedwait Return Values
Waiting for a Time Interval
cond_reltimedwait Syntax
cond_reltimedwait Return Values
Unblock One Thread
cond_signal Syntax
cond_signal Return Values
Unblock All Threads
cond_broadcast Syntax
cond_broadcast Return Values
Similar Synchronization Functions: Semaphores
Initialize a Semaphore
sema_init Syntax
Semaphores With Intraprocess Scope
Semaphores With Interprocess Scope
sema_init Return Values
Increment a Semaphore
sema_post Syntax
sema_post Return Values
Block on a Semaphore Count
sema_wait Syntax
sema_wait Return Values
Decrement a Semaphore Count
sema_trywait Syntax
sema_trywait Return Values
Destroy the Semaphore State
sema_destroy(3C) Syntax
sema_destroy(3C) Return Values
Synchronizing Across Process Boundaries
Example of Producer and Consumer Problem
Special Issues for fork() and Solaris Threads
Chapter 7 Safe and Unsafe Interfaces
Thread Safety
MT Interface Safety Levels
Reentrant Functions for Unsafe Interfaces
Async-Signal-Safe Functions in Solaris Threads
MT Safety Levels for Libraries
Unsafe Libraries
Chapter 8 Compiling and Debugging
Setting Up the Solaris Environment for Developing Multithreaded Applications
Compiling a Multithreaded Application
Preparing for Compilation
Choosing Solaris or POSIX Threads
Including <thread.h> or <pthread.h>
Compiling and Linking a Multithreaded Program
Compiling and Linking in the POSIX Threads Environment
Compiling and Linking in the Solaris Threads Environment
Compiling and Linking in a Mixed Threads Environment
Linking With -lrt for POSIX Semaphores
Alternate Threads Library
Debugging a Multithreaded Program
Common Oversights in Multithreaded Programs
Tracing and Debugging with DTrace
Profiling with Performance Analyzer
Detecting Data Races and Deadlocks Using Thread Analyzer
Using dbx
Tracing and Debugging With the TNF Utilities
Using truss
Using mdb
Chapter 9 Programming Guidelines
Rethinking Global Variables
Providing for Static Local Variables
Synchronizing Threads
Single-Threaded Strategy
Reentrant Function
Code Locking
Data Locking
Invariants and Locks
Avoiding Deadlock
Deadlocks Related to Scheduling
Locking Guidelines
Finding Deadlocks
Some Basic Guidelines for Threaded Code
Creating and Using Threads
Working With Multiprocessors
Underlying Architecture
Shared-Memory Multiprocessors
Peterson's Algorithm
Parallelizing a Loop on a Shared-Memory Parallel Computer
Examples of Threads Programs
Further Reading
Appendix A Extended Example: A Thread Pool Implementation
What is a Thread Pool?
About the Thread Pool Example
Thread Pool Functions
thr_pool_create()
thr_pool_queue()
thr_pool_wait()
thr_pool_destroy()
Thread Pool Code Examples
thr_pool.h File
thr_pool.c File
What the Thread Pool Example Shows
© 2010, Oracle Corporation and/or its affiliates