Tru64 UNIX Guide to the POSIX Threads Library
Total Page:16
File Type:pdf, Size:1020Kb
Tru64 UNIX Guide to the POSIX Threads Library Order Number: AA–RH9RB–TE April 2000 This guide reviews the principles of multithreaded programming, as reflected in the IEEE® POSIX® 1003.1-1996 standard, and provides implementation guidelines and reference information for the Compaq® Multithreading Run-Time Library. Revision/Update Information: This guide supersedes the Guide to DECthreads, printed July 1999. Product Version: Tru64 UNIX Version 5.0A or higher Compaq Computer Corporation Houston, Texas © 2000 Compaq Computer Corporation. All rights reserved. COMPAQ and the Compaq logo are registered in the U.S. Patent and Trademark Office. Tru64 and OpenVMS are trademarks of Compaq Information Technologies Group, L.P. Microsoft is a registered trademark of Microsoft Corporation. UNIX is a registered trademark and The Open Group is a trademark of The Open Group in the U.S. and other countries. Other product names mentioned herein may be trademarks or registered trademarks of their respective companies. Confidential computer software. Valid license from Compaq required for possession, use, or copying. Consistent with FAR 12.211 and 12.212, Commercial Computer Software, Computer Software Documentation, and Technical Data for Commercial Items are licensed to the U.S. Government under vendor’s standard commercial license. Compaq shall not be liable for technical or editorial errors or omissions contained herein. The information in this publication is subject to change without notice and is provided "as is" without warranty of any kind. The entire risk arising out of the use of this information remains with recipient. In no event shall Compaq be liable for any direct, consequential, incidental, special, punitive, or other damages whatsoever (including without limitation, damages for loss of business profits, business interruption or loss of business information), even if Compaq has been advised of the possibility of such damages. The foregoing shall apply regardless of the negligence or other fault of either party and regardless of whether such liability sounds in contract, negligence, tort, or any other theory of legal liability, and notwithstanding any failure of essential purpose of any limited remedy. The limited warranties for Compaq products are exclusively set forth in the documentation accompanying such products. Nothing herein should be construed as constituting a further or additional warranty. This document is available on CD–ROM. This document was prepared using VAX DOCUMENT Version 2.1. Contents Preface ..................................................... xvii Part I POSIX Threads Library Overview and Programming Guidelines 1 Introducing Multithreaded Programming 1.1 Advantages of Using Threads ........................... 1–1 1.2 Overview of Threads .................................. 1–2 1.3 Thread Execution . ................................... 1–4 1.4 Functional Models for Multithreaded Programming .......... 1–5 1.4.1 Boss/Worker Model ................................. 1–5 1.4.2 Work Crew Model ................................. 1–6 1.4.3 Pipelining Model .................................. 1–6 1.4.4 Combination of Functional Models . ................... 1–7 1.5 Programming Issues for Multithreaded Programs . .......... 1–7 1.6 POSIX Threads Libraries and Interfaces ................... 1–8 1.6.1 The pthread Multithreading Interface .................. 1–10 1.6.1.1 Optionally Implemented POSIX.1 Routines . .......... 1–22 1.6.2 Thread-Independent Services Interface ................. 1–23 1.6.3 Undocumented and Obsolete Interfaces ................. 1–26 1.6.3.1 The cma Interface . ........................... 1–26 1.6.3.2 The d4 (DCEthread) Interfaces . ................... 1–27 2 Objects and Operations 2.1 Threads and Synchronization Objects . ................... 2–1 2.2 Attributes Objects . ................................... 2–2 2.3 Thread Operations . ................................... 2–2 2.3.1 Creating a Thread ................................. 2–3 iii 2.3.2 Setting the Attributes of a New Thread . ................ 2–3 2.3.2.1 Setting the Inherit Scheduling Attribute ............. 2–4 2.3.2.2 Setting the Scheduling Policy Attribute .............. 2–4 2.3.2.2.1 Techniques for Setting the Scheduling Policy Attribute . ................................ 2–5 2.3.2.2.2 Comparing Throughput and Real-Time Policies ..... 2–6 2.3.2.2.3 Portability of Scheduling Policy Settings . ....... 2–6 2.3.2.3 Setting the Scheduling Parameters Attribute . ....... 2–7 2.3.2.4 Setting the Stacksize Attribute .................... 2–8 2.3.2.5 Setting the Stack Address Attribute ................ 2–8 2.3.2.6 Setting the Guardsize Attribute .................... 2–9 2.3.2.7 Setting the Contention Scope Attribute .............. 2–9 2.3.3 Terminating a Thread .............................. 2–11 2.3.3.1 Cleanup Handlers .............................. 2–14 2.3.4 Detaching and Destroying a Thread .................... 2–14 2.3.5 Joining With a Thread .............................. 2–15 2.3.6 Scheduling a Thread ............................... 2–16 2.3.6.1 Calculating the Scheduling Priority . ................ 2–16 2.3.6.2 Effects of Scheduling Policy ....................... 2–17 2.3.7 Canceling a Thread ................................ 2–19 2.3.7.1 Thread Cancelation Implemented Using Exceptions .... 2–19 2.3.7.2 Thread Return Value After Cancelation .............. 2–19 2.3.7.3 Controlling Thread Cancelation .................... 2–20 2.3.7.4 Deferred Cancelation Points ...................... 2–21 2.3.7.5 Cleanup from Deferred Cancelation . ................ 2–21 2.3.7.6 Cleanup from Asynchronous Cancelation ............. 2–22 2.3.7.7 Example of Thread Cancelation Code ............... 2–23 2.4 Synchronization Objects ................................ 2–26 2.4.1 Mutexes . ........................................ 2–26 2.4.1.1 Normal Mutex . ................................ 2–27 2.4.1.2 Default Mutex . ................................ 2–27 2.4.1.3 Recursive Mutex ............................... 2–27 2.4.1.4 Errorcheck Mutex .............................. 2–28 2.4.1.5 Mutex Operations .............................. 2–28 2.4.1.6 Mutex Attributes ............................... 2–29 2.4.2 Condition Variables ................................ 2–30 2.4.3 Condition Variable Attributes ........................ 2–35 2.4.4 Read-Write Locks . ................................ 2–36 2.4.4.1 Thread Priority and Writer Precedence for Read-Write Locks ........................................ 2–36 2.4.4.2 Initializing and Destroying a Read-Write Lock . ....... 2–37 2.4.4.3 Read-Write Lock Attributes ....................... 2–37 2.5 Process-Shared Synchronization Objects . ................ 2–37 iv 2.5.1 Programming Considerations ......................... 2–38 2.5.2 Process-Shared Mutexes . ........................... 2–38 2.5.3 Process-Shared Condition Variables . ................... 2–38 2.5.4 Process-Shared Read-Write Locks . ................... 2–39 2.6 Thread-Specific Data .................................. 2–39 3 Programming with Threads 3.1 Designing Code for Asynchronous Execution ................ 3–1 3.1.1 Avoid Passing Stack Local Data ....................... 3–2 3.1.2 Initialize Objects Before Thread Creation ............... 3–2 3.1.3 Do Not Use Scheduling As Synchronization .............. 3–3 3.2 Memory Synchronization Between Threads ................. 3–4 3.3 Sharing Memory Between Threads ....................... 3–4 3.3.1 Using Static Memory ............................... 3–5 3.3.2 Using Stack Memory ............................... 3–5 3.3.3 Using Dynamic Memory . ........................... 3–6 3.4 Managing a Thread’s Stack . ........................... 3–6 3.4.1 Sizing the Stack ................................... 3–7 3.4.2 Using Stack Overflow Warning and Stack Guard Areas . 3–7 3.4.3 Diagnosing Stack Overflow Errors . ................... 3–8 3.5 Scheduling Issues . ................................... 3–8 3.5.1 Real-Time Scheduling . ........................... 3–8 3.5.2 Priority Inversion .................................. 3–9 3.5.3 Dependencies Among Scheduling Attributes and Contention Scope ........................................... 3–9 3.6 Using Synchronization Objects ........................... 3–10 3.6.1 Distinguishing Proper Usage of Mutexes and Condition Variables ........................................ 3–10 3.6.2 Avoiding Race Conditions . ........................... 3–10 3.6.3 Avoiding Deadlocks ................................ 3–11 3.6.4 Signaling a Condition Variable ........................ 3–11 3.6.5 Static Initialization Inappropriate for Stack-Based Synchronization Objects . ........................... 3–13 3.7 Granularity Considerations . ........................... 3–13 3.7.1 Determinants of a Program’s Granularity ............... 3–14 3.7.1.1 Alpha Processor Granularity . ................... 3–15 3.7.1.2 VAX Processor Granularity ....................... 3–15 3.7.2 Compiler Support for Determining the Program’s Actual Granularity . ................................... 3–16 3.7.3 Word Tearing . ................................... 3–16 3.7.4 Alignments of Members of Composite Data Objects ........ 3–17 v 3.7.5 Avoiding Granularity-Related Errors . ................ 3–17 3.7.5.1 Changing the Composite Data Object’s Layout . ....... 3–18 3.7.5.2 Maintaining the Composite Data Object’s Layout ...... 3–18 3.7.5.3 Using One Mutex Per Composite Data Object . ....... 3–19 3.7.6 Identifying Possible Word-Tearing Situations Using Visual Threads . .......................................