| help | account  


Programming with POSIX Threads
View Larger Image
David R. Butenhof
Addison-Wesley, Paperback, Published May 1997, 380 pages, ISBN 0201633922
List Price: $54.99
Our Price: $38.25
You Save: $16.74 (30% Off)


FREE Shipping on Orders over $40!*
Availability: Out-Of-Stock

Be the First to Write a Review and tell the world about this title!

People who purchase this book frequently purchase:

Books on similar topics, in best-seller order:Books from the same publisher, in best-seller order:

With this practical book, you will attain a solid understanding of threads and will discover how to put this powerful mode of programming to work in real-world applications.

The primary advantage of threaded programming is that it enables your applications to accomplish more than one task at the same time by using the number-crunching power of multiprocessor parallelism and by automatically exploiting I/O concurrency in your code, even on a single processor machine. The result: applications that are faster, more responsive to users, and often easier to maintain. Threaded programming is particularly well suited to network programming where it helps alleviate the bottleneck of slow network I/O.

This book offers an in-depth description of the IEEE operating system interface standard, POSIX® (Portable Operating System Interface) threads, commonly called Pthreads. Written for experienced C programmers, but assuming no previous knowledge of threads, the book explains basic concepts such as asynchronous programming, the lifecycle of a thread, and synchronization. You then move to more advanced topics such as attributes objects, thread-specific data, and realtime scheduling. An entire chapter is devoted to "real code," with a look at barriers, read/write locks, the work queue manager, and how to utilize existing libraries. In addition, the book tackles one of the thorniest problems faced by thread programmers-debugging-with valuable suggestions on how to avoid code errors and performance problems from the outset.

Numerous annotated examples are used to illustrate real-world concepts. A Pthreads mini-reference and a look at future standardization are also included.

David R. Butenhof, a recognized Pthreads authority, was deeply involved in the creation of the IEEE POSIX standard as well as the X/Open threading extensions, which were fast-tracked into X/Open XSH5 (UNIX98). An engineer at Digital Equipment Corporation, he was the lead architect and developer of Digital's own threading architecture and designed and implemented much of the Pthreads interfaces on Digital UNIX 4.0.

Table of Contents

List of Example Programs xii
Preface xv
Intended audience xvi
About the author xvi
Acknowledgments xvii
1 Introduction 1
1.1 The "bailing programmers" 3
1.2 Definitions and terminology 4
1.2.1 Asynchronous 4
1.2.2 Concurrency 4
1.2.3 Uniprocessor and multiprocessor 5
1.2.4 Parallelism 5
1.2.5 Thread safety and reentrancy 6
1.2.6 Concurrency control functions 7
1.3 Asynchronous programming is intuitive... 8
1.3.1 . . . because UNIX is asynchronous 9
1.3.2 . . . because the world is asynchronous 11
1.4 About the examples in this book 12
1.5 Asynchronous programming, by example 13
1.5.1 The baseline, synchronous version 14
1.5.2 A version using multiple processes 15
1.5.3 A version using multiple threads 17
1.5.4 Summary 19
1.6 Benefits of threading 20
1.6.1 Parallelism 20
1.6.2 Concurrency 22
1.6.3 Programming model 24
1.7 Costs of threading 25
1.7.1 Computing overhead 26
1.7.2 Programming discipline 26
1.7.3 Harder to debug 27
1.8 To thread or not to thread? 28
1.9 POSIX thread concepts 29
1.9.1 Architectural overview 30
1.9.2 Types and interfaces 30
1.9.3 Checking for errors 31
2 Threads 35
2.1 Creating and using threads 35
2.2 The life of a thread 39
2.2.1 Creation 40
2.2.2 Startup 41
2.2.3 Running and blocking 42
2.2.4 Termination 43
2.2.5 Recycling 44
3 Synchronization 45
3.1 Invariants, critical sections, and predicates 45
3.2 Mutexes 47
3.2.1 Creating and destroying a mutex 49
3.2.2 Locking and unlocking a mutex 52
3.2.2.1 Nonblocking mutex locks 58
3.2.3 Using mutexes for atomicity 61
3.2.4 Sizing a mutex to fit the job 62
3.2.5 Using more than one mutex 63
3.2.5.1 Lock hierarchy 63
3.2.5.2 Lock chaining 70
3.3 Condition variables 70
3.3.1 Creating and destroying a condition variable 74
3.3.2 Waiting on a condition variable 77
3.3.3 Waking condition variable waiters 81
3.3.4 One final alarm program 82
3.4 Memory visibility between threads 88
4 A few ways to use threads 97
4.1 Pipeline 98
4.2 Work Crew 106
4.3 Client/Server 120
5 Advanced threaded programming 131
5.1 One-time initialization 131
5.2 Attributes objects 134
5.2.1 Mutex attributes 135
5.2.2 Condition variable attributes 137
5.2.3 Thread attributes 138
5.3 Cancellation 142
5.3.1 Deferred cancelability 147
5.3.2 Asynchronous cancelability 150
5.3.3 Cleaning up 154
5.4 Thread-specific data 161
5.4.1 Creating thread-specific data 163
5.4.2 Using thread-specific data 166
5.4.3 Using destructor functions 167
5.5 Realtime scheduling 172
5.5.1 POSIX realtime options 173
5.5.2 Scheduling policies and priorities 174
5.5.3 Contention scope and allocation domain 181
5.5.4 Problems with realtime scheduling 183
5.5.5 Priority-aware mutexes 185
5.5.5.1 Priority ceiling mutexes 186
5.5.5.2 Priority inheritance mutexes 188
5.6 Threads and kernel entities 189
5.6.1 Many-to-one (user level) 190
5.6.2 One-to-one (kernel level) 191
5.6.3 Many-to-few (two level) 193
6 POSIX adjusts to threads 197
6.1 fork 197
6.1.1 Fork handlers 199
6.2 exec 204
6.3 Process exit 204
6.4 Stdio 204
6.4.1 flockfile and funlockfile 205
6.4.2 getchar_unlocked and putchar_unlocked 207
6.5 Thread-safe functions 209
6.5.1 User and terminal identification 210
6.5.2 Directory searching 212
6.5.3 String token 212
6.5.4 Time representation 212
6.5.5 Random number generation 213
6.5.6 Group and user database 213
6.6 Signals 214
6.6.1 Signal actions 215
6.6.2 Signal masks 216
6.6.3 pthread_kill 217
6.6.4 sigwait and sigwaitinfo 227
6.6.5 SIGEV_THREAD 230
6.6.6 Semaphores: synchronizing with a signal-catching function 234
7 "Real code" 241
7.1 Extended synchronization 241
7.1.1 Barriers 242
7.1.2 Read-write locks 253
7.2 Work queue manager 270
7.3 But what about existing libraries? 283
7.3.1 Modifying libraries to be thread-safe 284
7.3.2 Living with legacy libraries 285
8 Hints to avoid debugging 289
8.1 Avoiding incorrect code 290
8.1.1 Avoid relying on "thread inertia" 291
8.1.2 Never bet your mortgage on a thread race 293
8.1.3 Cooperate to avoid deadlocks 297
8.1.4 Beware of priority inversion 299
8.1.5 Never share condition variables between predicates 300
8.1.6 Sharing stacks and related memory corrupters 301
8.2 Avoiding performance problems 302
8.2.1 Beware of concurrent serialization 302
8.2.2 Use the right number of mutexes 303
8.2.2.1 Too many mutexes will not help 304
8.2.3 Never fight over cache lines 304
9 POSIX threads mini-reference 307
9.1 POSIX 1003.1c­1995 options 307
9.2 POSIX 1003.1c­1995 limits 308
9.3 POSIX 1003.1c­1995 interfaces 309
9.3.1 Error detection and reporting 310
9.3.2 Use of void* type 311
9.3.3 Threads 311
9.3.4 Mutexes 316
9.3.5 Condition variables 319
9.3.6 Cancellation. 323
9.3.7 Thread-specific data 325
9.3.8 Realtime scheduling 326
9.3.9 Fork handlers 336
9.3.10 Stdio 336
9.3.11 Thread-safe functions 338
9.3.12 Signals 342
9.3.13 Semaphores 345
10 Future standardization 347
10.1 X/Open XSH5 [UNIX98] 347
10.1.1 POSIX options for XSH5 348
10.1.2 Mutex type 349
10.1.3 Set concurrency level 351
10.1.4 Stack guard size 353
10.1.5 Parallel I/O 354
10.1.6 Cancellation points 355
10.2 POSIX 1003.1j 356
10.2.1 Barriers 358
10.2.2 Read-write locks 358
10.2.3 Spinlocks 359
10.2.4 Condition variable wait clock 359
10.2.5 Thread abort 361
10.3 POSIX 1003.14 361
Bibliography 363
Thread resources on the Internet 367
Index 369



Forgot your password?
FAQs
Shipping Options
Returns
Your Orders
Your Account