ebook img

Pro .NET 4 Parallel Programming in C# (Expert's Voice in .NET) PDF

329 Pages·2010·6.29 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Pro .NET 4 Parallel Programming in C# (Expert's Voice in .NET)

CYAN  YELLOW  MAGENTA  BLACK  PANTONE 123 C BOOKS FOR PROFESSIONALS BY PROFESSIONALS® THE EXPERT’S VOICE® IN .NET Companion eBook Available Pro .NET 4 Parallel Programming in C# Pro Dear Reader, . Normal programs perform one task at a time. Parallel programs perform several N tasks simultaneously, improving performance, scalability, and responsiveness. E By writing parallel programs, your projects can take complete advantage of the latent power that multi-core and multi-processor computers have to offer. T Pro This book shows you how to get things done. I focus on the practice, rather 4 Adam Freeman, Author of 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 Pro ASP.NET 4 in C# 2010 P of parallel programming but also contains a list of common pitfalls together Pro LINQ: Language with details of how to recognize them, and the steps you can take to prevent a Integrated Query in C# them happening to you. This book is an invaluable companion when tackling a r .NET 4 Parallel 2010 a wide range of parallel programming features and techniques including: Visual C# 2010 Recipes l l Programming .NET Security • Using the .NET 4 Task Parallel Library (TPL) e Microsoft .NET XML Web • Using synchronization to share data between tasks l Services Step by Step • Coordinating parallel execution P C# for Java Developers • Using parallel loops Programming in C# r Programming the Internet • Using Parallel LINQ o with Java • Testing and debugging parallel programs g • Implementing common parallel algorithms Active Java r a Each topic is explained with a complete fully working code example, so you can see, in one place, everything you need to do. m m Adam Freeman i n THE APRESS ROADMAP g Discover how concurrent programming Companion eBook Introducing Proa nCd# t2h0e1 0 .NET 4P rPoa rallel i can improve your code .NET 4.0 .NET 4 Platform Programming in C# n C Pro Accelerated Pro Dynamic .NET 4.0 # C# 2010 LINQ in C# 2010 See last page for details Applications on $10 eBook version F SOURCE CODE ONLINE re Adam Freeman www.apress.com ISBN 978-1-4302-2967-4 e m 55999 a n Shelve in: Programming Languages/C# User level: 9 781430 229674 Intermediate–Advanced this print for content only—size & color not accurate 7.5 x 9.25 spine = 0.75" 328 page count 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 [email protected], or visit www.springeronline.com. For information on translations, please e-mail [email protected], 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 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 iv 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 v ■ CONTENTS ■ 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 vi ■ CONTENTS 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 vii ■ CONTENTS 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 viii ■ CONTENTS Using the Parallel Producer/Consumer Pattern ............................................................. 146 Creating the Pattern ............................................................................................................................. 147 Combining Multiple Collections ............................................................................................................ 152 Using a Custom Task Scheduler .................................................................................... 156 Creating a Custom Scheduler ............................................................................................................... 156 Using a Custom Scheduler ................................................................................................................... 160 Common Problems and Their Causes ............................................................................ 162 Inconsistent/Unchecked Cancellation .................................................................................................. 162 Assuming Status on Any-To-One Continuations ................................................................................... 164 Trying to Take Concurrently ................................................................................................................. 165 Reusing Objects in Producers ............................................................................................................... 166 Using BlockingCollection as IEnumerable ............................................................................................ 168 Deadlocked Task Scheduler ................................................................................................................. 169 Summary ....................................................................................................................... 172 ■ Chapter 5: Parallel Loops .................................................................................... 173 Parallel vs. Sequential Loops ......................................................................................... 173 The Parallel Class .......................................................................................................... 175 Invoking 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 ..................................................................................... 193 Selecting a Partitioning Strategy .......................................................................................................... 195 Creating a Custom Partitioning Strategy .............................................................................................. 200 ix

Description:
Great book, reommended even if you are a newbie on parallel programming. The examples are quite illustrative, close to real world applications. The only reason for rating it as 4 stars it's because the print/paper quality of Apress books it's not as good as it was a few years ago.
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.