Đây là quyển sách tiếng anh về lĩnh vực công nghệ thông tin cho sinh viên và những ai có đam mê. Quyển sách này trình về lý thuyết ,phương pháp lập trình cho ngôn ngữ C và C++.
MANNING Anthony Williams Practical Multithreading IN ACTION C++ Concurrency in Action C++ Concurrency in Action P RACTICAL M ULTITHREADING ANTHONY WILLIAMS MANNING S HELTER I SLAND For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 261 Shelter Island, NY 11964 Email: orders@manning.com ©2012 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine. Manning Publications Co. Development editor: Cynthia Kane 20 Baldwin Road Technical proofreader: Jonathan Wakely PO Box 261 Copyeditor: Linda Recktenwald Shelter Island, NY 11964 Proofreader: Katie Tennant Typesetter: Dennis Dalinnik Cover designer: Marija Tudor ISBN: 9781933988771 Printed in the United States of America 1 2 3 4 5 6 7 8 9 10 – MAL – 18 17 16 15 14 13 12 To Kim, Hugh, and Erin vii brief contents 1 ■ Hello, world of concurrency in C++! 1 2 ■ Managing threads 15 3 ■ Sharing data between threads 33 4 ■ Synchronizing concurrent operations 67 5 ■ The C++ memory model and operations on atomic types 103 6 ■ Designing lock-based concurrent data structures 148 7 ■ Designing lock-free concurrent data structures 180 8 ■ Designing concurrent code 224 9 ■ Advanced thread management 273 10 ■ Testing and debugging multithreaded applications 300 ix contents preface xv acknowledgments xvii about this book xix about the cover illustration xxii 1 Hello, world of concurrency in C++! 1 1.1 What is concurrency? 2 Concurrency in computer systems 2 Approaches to concurrency 4 1.2 Why use concurrency? 6 Using concurrency for separation of concerns 6 Using concurrency for performance 7 ■ When not to use concurrency 8 1.3 Concurrency and multithreading in C++ 9 History of multithreading in C++ 10 ■ Concurrency support in the new standard 10 ■ Efficiency in the C++ Thread Library 11 ■ Platform-specific facilities 12 1.4 Getting started 13 Hello, Concurrent World 13 1.5 Summary 14 [...]... Waiting for tasks submitted to a thread pool 276 Tasks that wait for other tasks 280 Avoiding contention on the work queue 283 Work stealing 284 ■ ■ ■ 9.2 Interrupting threads 289 Launching and interrupting another thread 289 Detecting that a thread has been interrupted 291 Interrupting a condition variable wait 291 Interrupting a wait on std::condition_variable_any 294 Interrupting other blocking... world of concurrency in C++! multithreaded C++ programs without relying on platform-specific extensions and thus allow writing portable multithreaded code with guaranteed behavior It also comes at a time when programmers are increasingly looking to concurrency in general, and multithreaded programming in particular, to improve application performance This book is about writing programs in C++ using multiple... in a single process tion between threads The low overhead associated with launching and communicating between multiple threads within a process compared to launching and communicating between multiple single-threaded processes means that this is the favored approach to concurrency in mainstream languages including C++, despite the potential problems arising from the shared memory In addition, the C++. .. the support for concurrency in C++ ■ What a simple multithreaded C++ program looks like These are exciting times for C++ users Thirteen years after the original C++ Standard was published in 1998, the C++ Standards Committee is giving the language and its supporting library a major overhaul The new C++ Standard (referred to as C++1 1 or C++0 x) was published in 2011 and brings with it a whole swathe... with sharing data between threads 34 Race conditions 3.2 35 ■ Avoiding problematic race conditions Protecting shared data with mutexes 36 37 Using mutexes in C++ 38 Structuring code for protecting shared data 39 Spotting race conditions inherent in interfaces 40 Deadlock: the problem and a solution 47 Further guidelines for avoiding deadlock 49 Flexible locking with std::unique_lock 54 Transferring mutex... processing its own set of incoming records We wrote the code in C++, using POSIX threads, and made a fair number of mistakes—multithreading was new to all of us—but we got there in the end It was also while working on this project that I first became aware of the C++ Standards Committee and the freshly published C++ Standard I have had a keen interest in multithreading and concurrency ever since Where... original developer, I jumped at the chance to get involved I have been the primary developer and maintainer of the Boost Thread Library ever since xv xvi PREFACE As the work of the C++ Standards Committee shifted from fixing defects in the existing standard to writing proposals for the next standard (named C++0 x in the hope that it would be finished by 2009, and now officially C++1 1, because it was finally... Handling interruptions 297 Interrupting background tasks on application exit 298 ■ ■ ■ ■ ■ 9.3 10 Summary 299 Testing and debugging multithreaded applications 300 10.1 Types of concurrency- related bugs Unwanted blocking 301 10.2 ■ 301 Race conditions 302 Techniques for locating concurrency- related bugs Reviewing code to locate potential bugs 303 Locating concurrency- related bugs by testing 305 Designing... manufacturers have increasingly been favoring multicore designs with 2, 4, 16, or more processors on a single chip over better performance with a single core Consequently, multicore desktop computers, and even multicore embedded devices, are now increasingly prevalent The increased computing power of these machines comes not from running a single task faster but from running multiple tasks in parallel In the past,... use concurrency in your application, whether for performance, separation of concerns, or because it’s “multithreading Monday,” what does that mean for C++ programmers? 1.3 Concurrency and multithreading in C++ Standardized support for concurrency through multithreading is a new thing for C++ It’s only with the upcoming C++1 1 Standard that you’ll be able to write multithreaded code without resorting . MANNING Anthony Williams Practical Multithreading IN ACTION C++ Concurrency in Action C++ Concurrency in Action P RACTICAL M ULTITHREADING ANTHONY WILLIAMS MANNING S HELTER . 6 Using concurrency for performance 7 ■ When not to use concurrency 8 1.3 Concurrency and multithreading in C++ 9 History of multithreading in C++ 10 ■ Concurrency