Freeman .NET 4 Parallel Programming in C# Companion eBook Available Pro this print for content only—size & color not accurate CYAN MAGENTA YELLOW BLACK PANTONE 123 C BOOKS FOR PROFESSIONALS BY PROFESSIONALS ® Adam Freeman, Author of Pro ASP.NET 4 in C# 2010 Pro LINQ: Language Integrated Query in C# 2010 Visual C# 2010 Recipes Programming .NET Security Microsoft .NET XML Web Services Step by Step C# for Java Developers Programming the Internet with Java Active Java Shelve in: Programming Languages/C# User level: Intermediate–Advanced THE APRESS ROADMAP Pro C# 2010 and the .NET 4 Platform Pro LINQ in C# 2010 Introducing .NET 4.0 Accelerated C# 2010 Pro .NET 4 Parallel Programming in C# Pro Dynamic .NET 4.0 Applications www.apress.com SOURCE CODE ONLINE Companion eBook See last page for details on $10 eBook version ISBN 978-1-4302-2967-4 9 781430 229674 5 59 9 9 Pro .NET 4 Parallel Programming in C# Dear Reader, Normal programs perform one task at a time. Parallel programs perform several tasks simultaneously, improving performance, scalability, and responsiveness. By writing parallel programs, your projects can take complete advantage of the latent power that multi-core and multi-processor computers have to offer. This book shows you how to get things done. I focus on the practice, rather than the theory and show you how the technology works using complete code examples to illustrate my points. Each chapter not only explains the principals of parallel programming but also contains a list of common pitfalls together with details of how to recognize them, and the steps you can take to prevent them happening to you. This book is an invaluable companion when tackling a wide range of parallel programming features and techniques including: • Using the .NET 4 Task Parallel Library (TPL) • Using synchronization to share data between tasks • Coordinating parallel execution • Using parallel loops • Using Parallel LINQ • Testing and debugging parallel programs • Implementing common parallel algorithms Each topic is explained with a complete fully working code example, so you can see, in one place, everything you need to do. Adam Freeman 7.5 x 9.25 spine = 0.75" 328 page count THE EXPERT’S VOICE ® IN .NET Pro .NET 4 Parallel Programming in C# Adam Freeman Discover how concurrent programming can improve your code Pro .NET 4 Parallel Programming in C# ■ ■ ■ Adam Freeman Pro .NET 4 Parallel Programming in C# Copyright © 2010 by Adam Freeman All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-4302-2967-4 ISBN-13 (electronic): 978-1-4302-2968-1 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. President and Publisher: Paul Manning Lead Editor: Ewan Buckingham Technical Reviewer: André van Meulebrouck Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Coordinating Editor: Anne Collett Copy Editor: Heather Lang Production Support: Patrick Cunningham Indexer: BIM Indexing & Proofreading Services Artist: April Milne Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders-ny@springer-sbm.com, or visit www.springeronline.com. For information on translations, please e-mail rights@apress.com, or visit www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/info/bulksales. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at www.apress.com. You will need to answer questions pertaining to this book in order to successfully download the code. For my wife Jacqui Griffyth and her chickens iv Contents at a Glance About the Author xiii About the Technical Reviewer xiv Acknowledgments xv ■Chapter 1: Introducing Parallel Programming 1 ■Chapter 2: Task Programming 7 ■Chapter 3: Sharing Data 49 ■Chapter 4: Coordinating Tasks 109 ■Chapter 5: Parallel Loops 173 ■Chapter 6: Parallel LINQ 219 ■Chapter 7: Testing and Debugging 251 ■Chapter 8: Common Parallel Algorithms 271 Index 295 v Contents About the Author xiii About the Technical Reviewer xiv Acknowledgments xv ■Chapter 1: Introducing Parallel Programming 1 Introducing .NET Parallel Programming 1 What’s in This Book (and What Is Not) 2 Understanding the Benefits (and Pitfalls) of Parallel Programming 3 Considering Overhead 3 Coordinating Data 3 Scaling Applications 3 Deciding When to Go Parallel 3 Deciding When to Stay Sequential 4 Getting Prepared for This Book 4 Understanding the Structure of This Book 4 Getting the Example Code 5 Summary 6 ■ CONTENTS vi ■Chapter 2: Task Programming 7 Hello Task 7 Creating and Starting Tasks 8 Creating Simple Tasks 9 Setting Task State 11 Getting a Result 13 Specifying Task Creation Options 15 Identifying Tasks 15 Cancelling Tasks 15 Monitoring Cancellation by Polling 17 Monitoring Cancellation with a Delegate 19 Monitoring Cancellation with a Wait Handle 20 Cancelling Several Tasks 22 Creating a Composite Cancellation Token 23 Determining If a Task Was Cancelled 24 Waiting for Time to Pass 25 Using a Cancellation Token Wait Handle 26 Using Classic Sleep 27 Using Spin Waiting 29 Waiting for Tasks 30 Waiting for a Single Task 31 Waiting for Several Tasks 33 Waiting for One of Many Tasks 34 Handling Exceptions in Tasks 35 Handling Basic Exceptions 36 Using an Iterative Handler 37 Reading the Task Properties 39 Using a Custom Escalation Policy 41 ■ CONTENTS vii Getting the Status of a Task 43 Executing Tasks Lazily 43 Understanding Common Problems and Their Causes 45 Task Dependency Deadlock 45 Local Variable Evaluation 46 Excessive Spinning 47 Summary 48 ■Chapter 3: Sharing Data 49 The Trouble with Data 50 Going to the Races 50 Creating Some Order 51 Executing Sequentially 52 Executing Immutably 52 Executing in Isolation 53 Synchronizing Execution 59 Defining Critical Regions 59 Defining Synchronization Primitives 59 Using Synchronization Wisely 60 Using Basic Synchronization Primitives 61 Locking and Monitoring 62 Using Interlocked Operations 67 Using Spin Locking 70 Using Wait Handles and the Mutex Class 72 Configuring Interprocess Synchronization 76 Using Declarative Synchronization 78 Using Reader-Writer Locks 79 ■ CONTENTS viii Working with Concurrent Collections 87 Using .NET 4 Concurrent Collection Classes 88 Using First-Generation Collections 97 Using Generic Collections 99 Common Problems and Their Causes 100 Unexpected Mutability 100 Multiple Locks 101 Lock Acquisition Order 103 Orphaned Locks 105 Summary 107 ■Chapter 4: Coordinating Tasks 109 Doing More with Tasks 110 Using Task Continuations 110 Creating Simple Continuations 111 Creating One-to-Many Continuations 113 Creating Selective Continuations 115 Creating Many-to-One and Any-To-One Continuations 117 Canceling Continuations 120 Waiting for Continuations 122 Handling Exceptions 122 Creating Child Tasks 126 Using Synchronization to Coordinate Tasks 129 Barrier 131 CountDownEvent 136 ManualResetEventSlim 139 AutoResetEvent 141 SemaphoreSlim 143 [...]... C#, the NET Framework, and Visual Studio 2010 that take a big step toward pairing a modern programming language with a modern approach to parallel programming Introducing NET Parallel Programming This book is about the parallel programming features of NET 4, specifically the Task Parallel Library (TPL), Parallel LINQ, and the legion of support classes that make writing parallel programs with C# simpler... parallel code and track down bugs Parallel programming adds some unique problems to debugging, but the new Visual Studio 2010 parallel debugger features go a long way to addressing them 4 CHAPTER 1 ■ INTRODUCING PARALLEL PROGRAMMING The final chapter, Chapter 8, contains some sample implementations of common parallel algorithms In many cases, especially when you are starting with parallel programming, ... Handling PLINQ Exceptions 238 Cancelling PLINQ Queries 239 Setting Merge Options 240 Using Custom Partitioning 242 Using Custom Aggregation 245 Generating Parallel Ranges 246 x ■ CONTENTS Common Problems and Their Causes 247 Forgetting the PLINQ Basics 247 Creating Race Conditions 248 Confusing... CHAPTER 1 ■ INTRODUCING PARALLEL PROGRAMMING Microsoft has responded to the need for a better way to write parallel programs with the enhancements to the NET framework I describe in this book .NET has had support for parallel programming since version 1.0, now referred to as classic threading, but it was hard to use and made you think too much about managing the parallel aspects of your program, which... Listing 2 -4 Creating Several Tasks Using Task State using System; using System.Threading.Tasks; namespace Listing_ 04 { class Listing_ 04 { static void Main(string[] args) { string[] messages = { "First task", "Second task", "Third task", "Fourth task" }; foreach (string msg in messages) { Task myTask = new Task(obj => printMessage((string)obj), msg); myTask.Start(); } // wait for input before exiting... Task in a single step as shown by Listing 2-6 Listing 2-6 Getting a Result with the Task Factory using System; using System.Threading.Tasks; namespace Listing_06 { class Listing_06 { static void Main(string[] args) { // create the task Task task1 = Task.Factory.StartNew(() => { int sum = 0; for (int i = 0; i < 100; i++) { sum += i; } return sum; }); 14 CHAPTER 2 ■ TASK PROGRAMMING // write... 256 Finding Parallel Bugs 260 Debugging Program State 261 Handling Exceptions 265 Detecting Deadlocks 267 Summary 269 ■Chapter 8: Common Parallel Algorithms 271 Sorting, Searching, and Caching 271 Using Parallel Quicksort 271 Traversing a Parallel Tree 2 74 Searching a Parallel... mainstream programmers and do much to drive up the utilization of all of those multicore machines out there 6 CHAPTER 2 ■■■ Task Programming Listing 2-1 Hello Task using System; using System.Threading.Tasks; namespace Listing_01 { class Listing_01 { static void Main(string[] args) { Task.Factory.StartNew(() => { Console.WriteLine("Hello World"); }); // wait for input before exiting Console.WriteLine("Main... Actions 175 Using Parallel Loops 176 Setting Parallel Loop Options 181 Breaking and Stopping Parallel Loops 183 Handling Parallel Loop Exceptions 187 Getting Loop Results 188 Canceling Parallel Loops 189 Using Thread Local Storage in Parallel Loops 190 Performing Parallel Loops with Dependencies... ■ TASK PROGRAMMING Task task2 = new Task(obj => { int sum = 0; int max = (int)obj; for (int i = 0; i < max; i++) { sum += i; } return sum; }, 100); // start the task task2.Start(); // write out the result Console.WriteLine("Result 2: {0}", task2.Result); // wait for input before exiting Console.WriteLine("Main method complete Press enter to finish."); Console.ReadLine(); } } } Reading the . Parallel Programming in C# Adam Freeman Discover how concurrent programming can improve your code Pro .NET 4 Parallel Programming in C# . pairing a modern programming language with a modern approach to parallel programming. Introducing .NET Parallel Programming This book is about the parallel