
Early Praise for Seven Concurrency Models in Seven Weeks For decades, professional programmers have dealt with concurrency and parallelism using threads and locks. But this model is one of many, as Seven Concurrency Models in Seven Weeks vividly demonstrates. If you want to get ahead in a world where main- stream languages are scrambling to support actors, CSP, data parallelism, functional programming, and Clojure’s unified succession model, read this book. ➤ Stuart Halloway Cofounder, Cognitect As our machines get more and more cores, understanding concurrency is more impor- tant than ever before. You’ll learn why functional programming matters for concurrency, how actors can be leveraged for writing distributed software, and how to explore par- allel processing with GPUs and Big Data. This book will expand your toolbox for writing software so you’re prepared for the years to come. ➤ José Valim Cofounder, Plataformatec An eye-opening survey of different concurrency/parallelism techniques, Seven Concur- rency Models strikes an excellent balance between providing explanations and encour- aging experimentation. ➤ Frederick Cheung CTO, Dressipi The world is changing, and every working programmer must learn to think about con- current programming. Now when they say, “How shall I do that?” I will have a book that I can suggest they read. I learned a lot and am happy to recommend it. ➤ Andrew Haley Java lead engineer, Red Hat As Amdahl’s law starts to eclipse Moore’s law, a transition from object-oriented pro- gramming to concurrency-oriented programming is taking place. As a result, the timing of this book could not be more appropriate. Paul does a fantastic job describing the most important concurrency models, giving you the necessary ammunition to decide which one of them best suits your needs. A must-read if you are developing software in the multicore era. ➤ Francesco Cesarini Founder and technical director, Erlang Solutions With this book, Paul has delivered an excellent introduction to the thorny topics of concurrency and parallelism, covering the different approaches in a clear and engaging way. ➤ Sean Ellis GPU architect, ARM A simple approach for a complex subject. I would love to have a university course about this with Seven Concurrency Models in Seven Weeks as a guide. ➤ Carlos Sessa Android developer, Groupon Paul Butcher takes an issue that strikes fear into many developers and gives a clear exposition of practical programming paradigms they can use to handle and exploit concurrency in the software they create. ➤ Páidí Creed Software engineer, SwiftKey Having worked with Paul on a number of occasions, I can recommend him as a genuine authority on programming-language design and structure. This book is a lucid exposi- tion of an often-misunderstood but vital topic in modern software engineering. ➤ Ben Medlock Cofounder and CTO, SwiftKey Seven Concurrency Models in Seven Weeks When Threads Unravel Paul Butcher The Pragmatic Bookshelf Dallas, Texas • Raleigh, North Carolina 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 The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf, PragProg and the linking g device are trade- marks of The Pragmatic Programmers, LLC. Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein. Our Pragmatic courses, workshops, and other products can help you and your team create better software and have more fun. For more information, as well as the latest Pragmatic titles, please visit us at http://pragprog.com. The team that produced this book includes: Bruce A. Tate (series editor) Jacquelyn Carter (editor) Potomac Indexing, LLC (indexer) Molly McBeath (copyeditor) David J Kelly (typesetter) Janet Furlow (producer) Ellie Callahan (support) For international rights, please contact [email protected]. Copyright © 2014 The Pragmatic Programmers, LLC. All rights reserved. No part of this 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. ISBN-13: 978-1-937785-65-9 Encoded using the finest acid-free high-entropy binary digits. Book version: P1.0—July 2014 Contents Foreword . vii Acknowledgments . ix Preface . xi 1. Introduction . 1 Concurrent or Parallel? 1 Parallel Architecture 3 Concurrency: Beyond Multiple Cores 4 The Seven Models 7 2. Threads and Locks . 9 The Simplest Thing That Could Possibly Work 9 Day 1: Mutual Exclusion and Memory Models 10 Day 2: Beyond Intrinsic Locks 21 Day 3: On the Shoulders of Giants 32 Wrap-Up 44 3. Functional Programming . 49 If It Hurts, Stop Doing It 49 Day 1: Programming Without Mutable State 50 Day 2: Functional Parallelism 61 Day 3: Functional Concurrency 71 Wrap-Up 82 4. The Clojure Way—Separating Identity from State . 85 The Best of Both Worlds 85 Day 1: Atoms and Persistent Data Structures 85 Day 2: Agents and Software Transactional Memory 97 Day 3: In Depth 106 Wrap-Up 113 Contents • vi 5. Actors . 115 More Object-Oriented than Objects 115 Day 1: Messages and Mailboxes 116 Day 2: Error Handling and Resilience 127 Day 3: Distribution 137 Wrap-Up 150 6. Communicating Sequential Processes . 153 Communication Is Everything 153 Day 1: Channels and Go Blocks 154 Day 2: Multiple Channels and IO 166 Day 3: Client-Side CSP 177 Wrap-Up 185 7. Data Parallelism . 189 The Supercomputer Hidden in Your Laptop 189 Day 1: GPGPU Programming 190 Day 2: Multiple Dimensions and Work-Groups 201 Day 3: OpenCL and OpenGL—Keeping It on the GPU 212 Wrap-Up 220 8. The Lambda Architecture . 223 Parallelism Enables Big Data 223 Day 1: MapReduce 224 Day 2: The Batch Layer 237 Day 3: The Speed Layer 249 Wrap-Up 261 9. Wrapping Up . 263 Where Are We Going? 263 Roads Not Taken 265 Over to You 267 Bibliography . 269 Index . 271 Foreword This book tells a story. That sentence may seem like a strange first thought for a book, but the idea is important to me. You see, we turn away dozens of proposals for Seven in Seven books from authors who think they can throw together seven disjointed essays and call it a book. That’s not what we’re about. The original Seven Languages in Seven Weeks: A Pragmatic Guide to Learning Programming Languages [Tat10] story was that object-oriented programming languages were good for their time, but as pressures built around software complexity and concurrency driven by multicore architectures, functional programming languages would begin to emerge and would shape the way we program. Paul Butcher was one of the most effective reviewers of that book. After a growing four-year relationship, I’ve come to understand why. Paul has been right on the front lines of bringing highly scalable concurrency to real business applications. In the Seven Languages book, he saw hints of some of the language-level answers to an increasingly important and compli- cated problem space. A couple of years later, Paul approached us to write a book of his own. He argued that languages play an important part of the overall story, but they just scratch the surface. He wanted to tell a much more complete story to our readers and map out in layman’s terms the most critical tools that modern applications use to solve big parallel problems in a scalable way. At first we were skeptical. These books are hard to write—they take much longer than most other books and have a high failure rate—and Paul chose a huge dragon to slay. As a team, we fought and worked, eventually coaxing a good story out of the original table of contents. As the pages came together, it became increasingly clear that Paul had not only the technical ability but also the passion to attack this topic. We have come to understand that this is a special book, one that arrives at the right time. As you dig in, you’ll see what I mean. report erratum • discuss Foreword • viii You’ll cringe with us as we show threading and locking, the most widely used concurrency solution today. You’ll see where that solution comes up short, and then you’ll go to work. Paul will walk you through vastly different approaches, from the Lambda Architecture used in some of busiest social platforms to the actor-based model that powers many of the world’s largest and most reliable telecoms. You will see the languages that the pros use, from Java to Clojure to the exciting, emerging Erlang-based Elixir language. Every step of the way, Paul will walk you through the complexities from an insider’s perspective. I am excited to present Seven Concurrency Models in Seven Weeks. I hope you will treasure it as much as I do. Bruce A. Tate CTO, icanmakeitbetter.com Series editor of the Seven in Seven books Austin, Texas report erratum • discuss Acknowledgments When I announced that I had signed the contract to write this book, a friend asked, “Has it been long enough that you’ve forgotten what writing the first one was like?” I guess I was naïve enough to imagine that writing a second book would be easier. Perhaps if I’d chosen an easier format than a Seven in Seven book, I would have been right. It certainly wouldn’t have been possible without the amazing support I’ve received from series editor Bruce Tate and development editor Jackie Carter.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages289 Page
-
File Size-