Programming with POSIX® Threads
Addison-Wesley Professional Computing Series
Brian W. Kernighan, Consulting Editor
Ken Arnold/John Peyton,
William R. Cheswick/Steven M. Bellovin,
Scott Meyers,
David R. Musser/Atul Saini,
J. Stephen Pendergrast Jr.,
David M. Piscitello/A. Lyman Chapin,
Curt Schimmel,
W. Richard Stevens,
An Imprint of Addison Wesley Longman, Inc.
Reading, Massachusetts • Harlow, England • Menlo Park, California Berkeley, California • Don Mills, Ontario • Sydney Bonn • Amsterdam • Tokyo • Mexico City
Trademark acknowledgments:
UNIX is a registered trademark in the United States and other countries, licensed exclusively through X/Open Company Ltd. Digital, DEC, Digital UNIX, DECthreads, VMS, and OpenVMS are trademarks of Digital Equipment Corporation. Solaris, SPARC, SunOS, and Sun are trademarks of Sun Microsystems Incorporated. SGI and IRIX are trademarks of Silicon Graphics, Incorporated. HP-UX is a trademark of Hewlett-Packard Company. ALX, lBM, and OS/2 are trademarks or registered trademarks of the IBM Corporation. X/Open is a trademark of X/Open Company Ltd. POSIX is a registered trademark of the Institute of Electrical and Electronics Engineers, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book and Addison-Wesley was aware of a trademark claim, the designations have been printed in initial caps or all caps.
The authors and publishers have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein.
The publisher offers discounts on this book when ordered in quantity for special sales.
For more information, please contact:
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,