Corporate & Professional Publishing Group Addison-Wesley Publishing Company One Jacob Way
Reading, Massachusetts 01867
Library of Congress Cataloging-in-Publication Data
Butenhof, David R., 1956-
Programming with POSIX threads / David R. Butenhof.
p. cm. — (Addison-Wesley professional computing series) Includes bibliographical references and index. ISBN 0-201-63392-2 (pbk.)
l.Threads(Computerprograms) 2. POSIX(Computersoftware standard) 3. Electronic digital computers—Programming. I. Title. II. Series.
QA76.76.T55B88 1997
005.4'32-dc21 97-6635
CIP
Copyright © 1997 by Addison Wesley Longman, Inc.
All rights reserved. No part ofthis publication may be reproduced, stored in a retrieval system, or transmitted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior consent of the publisher. Printed in the United States of America. Published simultaneously in Canada.
Text printed on recycled and acid-free paper.
2 3 4 5 6 7 8 9 MA 00 99 98 97
2nd Printing October, 1997
To Anne, Amy, and Alyssa.
Quote acknowledgments:
Lewis Carroll,
Lewis Carroll,
Lewis Carroll,
Preface
The White Rabbit put on his spectacles, "Where shall I begin, please your Majesty?" he asked. "Begin at the beginning," the King said, very gravely, "and go on till you come to the end: then stop."
This book is about "threads" and how to use them. "Thread" is just a name for a basic software "thing" that can do work on a computer. A thread is smaller, faster, and more maneuverable than a traditional process. In fact, once threads have been added to an operating system, a "process" becomes just data—address space, files, and so forth—plus one or more threads that do something with all that data.
With threads, you can build applications that utilize system resources more efficiently, that are more friendly to users, that run blazingly fast on multiprocessors, and that may even be easier to maintain. To accomplish all this, you need only add some relatively simple function calls to your code, adjust to a new way of thinking about programming, and leap over a few yawning chasms. Reading this book carefully will, I hope, help you to accomplish all that without losing your sense of humor.
The threads model used in this book is commonly called "Pthreads," or "POSIX threads." Or, more formally (since you haven't yet been properly introduced), the POSIX 1003.lc-1995 standard. I'll give you a few other names later— but for now, "Pthreads" is all you need to worry about.
As I write this, Sun's Solaris, Digital's Digital UNIX, and SGI's IRIX already support Pthreads. The other major commercial UNIX operating systems will soon have Pthreads as well, maybe even by the time you read this, including IBM's AIX and Hewlett-Packard's HP-UX. Pthreads implementations are also available for Linux and other UNIX operating systems.
In the personal computer market, Microsoft's Win32 API (the primary programming interface to both Windows NT and Windows 95) supports threaded programming, as does IBM's OS/2. These threaded programming models are quite different from Pthreads, but the important first step toward using them productively is understanding concurrency, synchronization, and scheduling. The rest is (more or less) a matter of syntax and style, and an experienced thread programmer can adapt to any of these models.
The threaded model can be (and has been) applied with great success to a wide range of programming problems. Here are just a few:
• Large scale, computationally intensive programs
• High-performance application programs and library code that can take advantage of multiprocessor systems
• Library code that can be used by threaded application programs
• Realtime application programs and library code
• Application programs and library code that perform I/O to slow external devices (such as networks and human beings).
Intended audience