Home > Store

Thread Time: The MultiThreaded Programming Guide

Register your product to gain access to bonus material or receive a coupon.

Thread Time: The MultiThreaded Programming Guide

Book

  • Sorry, this book is no longer in print.
Not for Sale

Description

  • Copyright 1997
  • Dimensions: 7" x 9-1/4"
  • Pages: 560
  • Edition: 1st
  • Book
  • ISBN-10: 0-13-190067-6
  • ISBN-13: 978-0-13-190067-7

The easiest way for programmers to learn important new multi-threading techniques that are increasingly important in Windows NT/95, UNIX, POSIX, and other application development.KEY TOPICS:Each concept in the book is illustrated with a picture, making this an exceptionally easy-to-understand guide. The book introduces the process model, the thread model, and basic thread management functions. Learn how to synchronize and schedule threads. In a Programming Guidelines chapter, learn the basic do's and don'ts of multithreaded programming. The book includes extensive examples, exercises and references, including manual pages, debugging advice, and a CD-ROM loaded with practical information.MARKET:This book is an effective introduction to multithreading for both professional programmers and students.

Sample Content

Table of Contents



Preface.


1. The Process Model.

The [Single-Threaded] Process. The Context of a Process. The Process Model Operating System. Process Scheduling. Process Context Switches. Process Life Cycle, Modes, States, and Transitions. An Illustration of a Process. The Limitations of Single-Threaded Programs. Summary. Exercises.



2. The Thread Model.

Analogy of Single-Threaded Execution. Analogy of Multithreaded Execution. The Benefits of Threads. Thread Context, Modes, States, and Transitions. User Threads and Kernel Threads. Bound and Unbound Threads. The Thread Model Operating System. Many-to-One (Mx1). One-to-One (1x1). Many-to-Many (MxN). An Illustration of a Multithreaded Program. Summary. Exercises.



3. Introduction to POSIX.

The POSIX Standard. POSIX Components. POSIX Options. Compile-Time Checking. Run-Time Checking. Namespace Reservation. Summary. Exercises.



4. Basic Thread Management.

Relating Process and Thread Model Functions. Creating a Thread. Waiting for a Thread. Detaching a Thread. Terminating a Thread. Finding and Comparing Thread Ids. Performing One-Time Initialization. Establishing Fork Handlers. Creating Specialized Thread Attributes. Handling Thread Management Errors. Summary. Exercises.



5. Thread Synchronization.

Thread Synchronization. Mutexes. Mutex Attributes. Initializing and Destroying Mutexes. Locking and Unlocking Mutexes. Example: Mutexes and Spinlocks. Condition Variables. Condition Variable Attributes. Initializing and Destroying Condition Variables. Waiting on a Condition. Signaling a Condition. Example: Condition Variables and Barriers. Semaphores. Initializing and Destroying Unnamed Semaphores. Locking and Unlocking Semaphores. Handling Synchronization Errors. Memory Models and Synchronization. Summary. Exercises.



6. Thread Scheduling.

Thread Scheduling Contention Scopes. Thread Scheduling Policies. SCHED_FIFO. SCHED_RR. SCHED_OTHER. A Thread's Scheduling Policy and Priority. Thread Scheduling Priority Values. Thread Scheduling Allocation Domains. Thread Creation Scheduling Attributes. Dynamic Thread Scheduling. Yielding the Processor. Priority Inversion and Mutexes. Summary. Exercises.



7. Threads and Signals.

Signals in the Thread Model. Synchronous and Asynchronous Signals. Examining/Changing a Thread's Signal Mask. Sending Signals to Threads. Waiting for a Signal. Async-Signal Safe Thread Functions. A New Signal Delivery Method: SIGEV_THREAD. Summary. Exercises.



8. Thread Cancellation.

Thread Cancellation. Thread Cancelability States. Cancellation Points. Setting Thread Cancelability States. Creating a Cancellation Point. Installing Cancellation Cleanup Handlers. Canceling a Thread. Async-Cancel Safety. Summary. Exercises.



9. Thread-Specific Data.

Thread-Specific Data. Creating Thread-Specific Data Keys. Deleting Thread-Specific Data Keys. Accessing Thread-Specific Data Elements. The Special Case, errno. Example: Per-Thread Signal Handlers. Summary. Exercises.



10. Thread Extensions.

HP and X/Open Thread Extensions. Thread Stack Attributes. Suspending and Resuming Threads. Mutex Extensions. Condition Variable Expiration Time. Read-Write Locks. Read-Write Lock Attributes. Initializing and Destroying Read-Write Locks. Locking and Unlocking Read-Write Locks. The Timeshare Scheduling Policy. Thread to Processor Binding. Thread Concurrency and MxN Threads. Scheduling Unbound Kernel Entities. Summary. Exercises.



11. Thread F/X.

fork(), exec(), exit(), and wait(). File and Message-Passing Operations. Signals. Locale State. Timers and Sleepers. NonLocal Jumps. Scheduling. Other Functions. Summary. Exercises.



12. Writing Thread-Safe Code.

Reentrant, Thread-Safe, and Unsafe Functions. How to Manage Shared Objects. How to Avoid Deadlock. How to Find Thread-Safe Library Interfaces. How to Write Cancel-Safe Functions. How to Write Fork-Safe Functions. How to Write a Thread-Safe Library. Summary. Exercises.



13. Programming Guidelines.

Basic Thread Management. Thread Synchronization. Thread Scheduling. Threads and Signals. Thread Cancellation. Thread-Specific Data. Thread Extensions. Thread F/X. Summary. Exercises.



14. Debugging Threaded Applications.

Deadlock. Inconsistent Behavior Across Platforms. Out of Resources. Poor Performance. Unexpected Behavior, Value, or Output. Summary. Exercises.



15. Parallel Programming Models and Issues.

Parallel Programming. The Work-Crew Model. The Boss-Worker Model. The Pipeline Model. The Master-Slave Model. Thread Creation Issues. Thread Synchronization Issues. Multiprocessor Cache Thrashing Issues. Summary. Exercises.



APPENDIX A. Pthread Manual Pages.


APPENDIX B. Glossary.


APPENDIX C. Bibliography.


Index.

Preface

PREFACE

Although threads have been around for decades, the use of threads on UNIXâ systems only started to become popular in the early 1990s. During this time, multiprocessor systems, client/server applications, and graphical user interfaces (GUIs) were making their way into the UNIX mainstream. Into this new era for UNIX, threads brought many benefits, but the most important was (and still is) performance. A multithreaded program can achieve significant performance gains through concurrent and/or parallel thread execution.
Concurrent thread execution means that two or more threads are in progress at the same time. If one thread blocks for some reason, another thread from the same program can execute in its place. This feature is especially relevant for cli- ent/server, GUI-based, and general I/O-bound applications. Parallelism occurs when two or more threads execute simultaneously across multiple processors. Parallelism exploits the processing power of multiprocessor systems and is espe- cially useful for compute-bound applications. Yet, threads are not a panacea. For example, thread management carries with it a certain amount of overhead. Thus, in order for a program to achieve a net performance gain, the concurrency or parallelism benefit must outweigh the thread management overhead liability. Fortunately, many applications can use threads to achieve significant perfor- mance gains.
This book teaches the application of multithreaded programming with the instruction, examples, and reference material needed to exploit this important software technology. Thread Time is primarily designed to be a practical guide for programming with POSIX.1c threads (also known as Pthreads). The Pthreads interfaces are presented and explained in detail. Code examples are provided, both in this book and on CD-ROM, that show how the interfaces are used. Guidelines teach you when to use threads, how to use them, how to avoid problems, and how to solve problems when they arise.
Thread Time may be suitable as a textbook in an advanced undergraduate or first-year graduate computer science curriculum. Clear objectives are provided at the beginning of each chapter, and exercises that help reinforce your knowl- edge of the technology are provided at the end of each chapter. The code exam- ples in this book are written in the C programming language.

Chapter Organization and Descriptions.
Although this has not been done explicitly, this book can be divided into five parts: Foundational Information, Pthreads Interfaces and Their Use, Program- ming with Threads, Advanced Topics, and Reference Material. Depending on your needs, you may wish to skip to a particular part or chapter.

1. Foundational Information.
Chapter 1: The Process Model chapter provides a foundation for understand- ing the benefits of threads. Herein, we present an overview of two areas: (1) the single-threaded process and (2) the process management portion of the UNIX operating system. At the end of this chapter, we draw a few conclusions about the limitations of single-threaded programming. Definitions for the Process Model, a process, and a thread are found in this chapter.
Chapter 2: The Thread Model chapter builds a foundation for programming with threads. Presented are several attributes of programs suitable for threads, several benefits of threads, and a high-level overview of three Thread Model operating systems. A number of essential definitions and concepts are provided in this chapter.
Chapter 3: The Introduction to POSIX chapter provides a brief introduction to the structure of POSIX and teaches you how to verify which POSIX thread functionality is supported on your system.
2. Pthreads Interfaces and Their Use.
Chapter 4: This chapter presents the Basic Thread Management interfaces and explains how they are used. These interfaces allow you to create, terminate, and synchronize threads. Specialized attributes are also available for creating threads with unique characteristics.
Chapter 5: The programmer assumes more responsibility when programming with threads. Thread operations must occur in the correct order, access to shared objects must be coordinated, and all threads must work together to achieve the desired result. Several mechanisms are available to coordinate, or synchronize, threads within a program. The Thread Synchronization chapter explains how to use these mechanisms.
Chapter 6: To accommodate varying needs, the scheduling policy and priority of POSIX.1c threads can be programmatically controlled. This chapter presents the Thread Scheduling interfaces, explains how they are used, and describes how threads compete for processor time.
Chapter 7: When a single-threaded process receives a signal, the default action is to terminate the process unless a signal handler has been installed. If a signal handler is invoked, program execution is interrupted. In a multithreaded pro- gram, signal handling is more flexible. A signal can be handled by a single thread within the program, allowing other threads to continue execution. The Threads and Signals chapter presents the Pthreads signal interfaces and explains how they are used.
Chapter 8: When programming with threads, it is often necessary to terminate a thread within the application. Signals appear to provide the most logical means to accomplish thread termination. However, signals have two major draw- backs: (1) if a signal causes a thread to terminate, the entire process terminates and (2) if POSIX allowed a signal to cause thread termination (and not process termination), mutexes and other resources acquired by terminated threads would never be released. The latter case could lead to deadlock. This chapter pre- sents the Thread Cancellation interfaces, explains the concept of thread can- cellation, and explains how resources are released when a thread is canceled.
Chapter 9: When a multithreaded program is written, a determination must be made as to how global data will be shared among the threads in the process. There are two choices: (1) a global data variable can be process shared and pro- tected with synchronization primitives; or (2) a global data variable can be spe- cific to each thread. In the second case, Thread-Specific Data is employed and synchronization is not required. This chapter presents the thread-specific data interfaces and explains how they are used.
Chapter 10: The HP-UX system includes several Thread Extensions, such as read-write locks, enhanced MxN Model scheduling control, processor affinity, and additional mutex types. Many of these features are thread extensions pro- vided by the X/Open Portability Guide. Some of these features are provided only by Hewlett-Packard and may not be portable. Nevertheless, all of these are pow- erful features that can enhance multithreaded programs.
Chapter 11: The Thread F/X (Effects) chapter discusses the behavior of com- mon library functions when they are used in multithreaded programs. For exam- ple, what happens when a thread calls fork? If a thread locks a file, will other threads in the same program still have access? Are there performance or porta- bility considerations when common library functions are used in multithreaded programs? Questions such as these are answered in this chapter.
3. Programming with Threads.
Chapter 12: The Writing Thread-Safe Code chapter defines thread-safing terminology and then describes several important facets of writing thread-safe code. At the end of this chapter, we provide a preflight checklist for designing thread-safe libraries.
Chapter 13: The Programming Guidelines chapter provides specific guide- lines for programming with threads based upon the information presented in the previous chapters. These guidelines include best practices, performance and portability tips, and constructs to avoid.
Chapter 14: You have written your multithreaded application. It compiles. However, when running the application, you notice some unexpected behavior. Perhaps a value from a calculation is incorrect, I/O is interleaved, or perfor- mance is poor. Alternatively, you may notice that certain tasks are incomplete or maybe the entire program hangs. The Debugging Threaded Applications chapter presents symptoms, probable causes, and possible solutions for you to consider during your systematic debugging process.
4. Advanced Topics.
Chapter 15: The Parallel Programming Models and Issues chapter dis- cusses several software models suitable for multithreaded programs. Some mod- els are suitable for I/O-bound applications; others are suitable for compute- bound applications. The former exploit the concurrency benefit whereas the lat- ter uses parallelism to exploit the power of multiprocessor systems. A discussion of parallel programming issues follows. Several important issues relating to thread creation, thread synchronization, and multiprocessor cache thrashing are identified. Specific techniques, guidelines, and solutions are also presented.
5. Reference Material.
Appendix A: The Pthread Manual Pages in this appendix come directly from Hewlett-Packard's HP-UX Release 10.30. They appear in this book exactly as they appear on-line. Many thanks to Hewlett-Packard for allowing us to publish these manual pages.
Appendix B: Glossary.
Appendix C: Bibliography.

Conventions Used in this Book.
1. Chapters.
To assist in rapid learning and to provide ease of use, information in this book is presented in modular form. Chapters are divided into sections; each section cov- ers a particular topic. A section is composed of a title, descriptive picture, synoptic caption, full text explanation, and guidelines or code examples as appropriate. Here is a simplified example of a section:
Protecting Shared Objects.
Fig. 3-7 Use a mutex to serialize access to a shared object. If a piece of shared data will never be modified, a mutex is not needed.
A mutex is a mutual exclusion object that is used to serialize access to shared objects. Once a thread locks a mutex, other threads wishing to gain access to the shared object block until the mutex is unlocked.
Not all shared objects require protection. For example, if a piece of shared data will never be modified, there is no need to protect that data with a mutex.
Guidelines.
* decide which shared objects require serial access
* use one mutex for each shared object requiring
serial access
Example

pthread_mutex_t m;

pthread_mutex_lock(&m);

pthread_mutex_unlock(&m);


The title of each section is given in the “Topics Covered” portion of each chap- ter introduction. Sections are presented in the same order listed. The last section in each chapter is followed by a chapter summary and exercises.
2. Use of Fonts.
Bold is used to indicate great importance.
Italics is used for definition of terms, emphasis, function arguments, or ref- erences to chapters (e.g., see Chapter 5, Thread Synchronization).
Courier is used for function names e.g., pthread_create() and for code examples.
Bold courier is used for macros and definitions referenced in the text.
CD-ROM and Code Examples
This book contains numerous code examples. To provide focus, many examples omit included files, declarations, and error checking. The CD-ROM packaged with Thread Time contains full, working versions of all code examples presented herein. In addition, templates are provided for several parallel programming models.
The CD-ROM has been formatted in the ISO 9660 and Rock Ridge file system formats. These formats are suitable for use on most operating system platforms. To access the code examples, mount the CD-ROM as a file system. The examples should then be copied to your local file system so that they may be compiled and/ or manipulated.

Acknowledgments.
We're indebted to the following individuals who performed a full review of the technical material contained herein: Greg Astfalk, Bruce Blinn, Wayne A. Booth, Tom Doeppner, Edith Epstein, Duncan Missimer, Ann Schneider, Richard Mar- lon Stein, Tom Watson, and Joel Williamson. Their valuable feedback has gone a long way to improve the quality of this book. Special thanks to Ann Schneider for reviewing and editing the manuscript many times and answering countless edit- ing questions at all hours of the night. We would also like to thank Tony Coon, Marti Jones, Doug McKenzie, and Anil Rao for their constructive technical and editorial comments.
We would especially like to thank our families, Ann, Amy, and Ann, for their sup- port, patience, and encouragement throughout the writing of this book.

Updates

Submit Errata

More Information

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020