CYAN
MAGENTA
YELLOW
BLACK
PANTONE 123 C
BOOKS FOR PROFESSIONALS BY PROFESSIONALS ®
Pro .NET 4 Parallel Programming in C#
Dear Reader,
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
• 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
THE APRESS ROADMAP
Companion eBook
See last page for details
on $10 eBook version
Introducing
.NET 4.0
Pro C# 2010
and the
.NET 4 Platform
Pro
.NET 4 Parallel
Programming in C#
Accelerated
C# 2010
Pro
LINQ in C# 2010
Pro
Dynamic .NET 4.0
Applications
www.apress.com
ISBN 978-1-4302-2967-4
5 59 9 9
Pro
Freeman
SOURCE CODE ONLINE
Companion
eBook
Available
.NET 4 Parallel Programming in C#
Adam Freeman, Author of
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:
THE EXPERT’S VOICE ® IN .NET
Pro
.NET 4 Parallel
Programming in C#
Discover how concurrent programming
can improve your code
Adam Freeman
Shelve in:
Programming Languages/C#
User level:
Intermediate–Advanced
9 781430 229674
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 , or
visit www.springeronline.com.
For information on translations, please e-mail , 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
■ INDEX
TryTake(out T, TimeSpan) method,
BlockingCollection class, 152
Wait(CancellationToken) method,
SemaphoreSlim class, 144
TryUpdate(TKey, TVal, TVal) method,
ConcurrentDictionary class, 95
WaitForAll( ) method, 30
WaitHandle class, 17, 26, 74, 76
WaitHandle property, 20, 26–27
■U
waiting for tasks, 25–34
unbounded collections, 149
one of many tasks, 34
unchecked cancellation, 162
several tasks, 33–34
unexpected mutability, 100
single task, 31–32
unhandled exceptions, 35, 122
using cancellation token wait handle,
26–27
UnobservedTaskException event,
TaskScheduler class, 157
UnobservedTaskExceptionEventArgs.Exce
ption property, 43
UnobservedTaskExceptionEventArgs.SetO
bserved( ) method, 43
upgradable read locks, 83–87
■V
value factory, TLS, 57
using classic sleep, 27–28
using spin waiting, 29–30
WaitingForActivation member, TaskStatus
enumeration, 43
WaitingForChildrenToComplete member,
TaskStatus enumeration, 43
WaitingToRun member, TaskStatus
enumeration, 43
Wait(int, CancellationToken) method
Value property, 55, 57
CountDownEvent class, 137
Visual Studio, performing parallel analysis
with, 256–260
ManualResetEvent class, 139
SemaphoreSlim class, 144
Wait(int) method
■W
CountDownEvent class, 137
Wait( ) method, 30–33, 36, 122, 129–130,
136–137, 139, 143–146, 159
ManualResetEvent class, 139
wait handles, 20, 60, 72–76
WaitAll( ) method, 33–34, 36–37, 40, 62, 74,
76, 104, 122, 266
WaitAny( ) method, 30, 34, 36, 74, 76, 164
WaitOne(int) method, AutoResetEvent
class, 141
Wait(CancellationToken) method,
CountDownEvent class, 137
WaitOne(TimeSpan) method,
AutoResetEvent class, 141
Wait(CancellationToken) method,
ManualResetEvent class, 139
310
SemaphoreSlim class, 144
WaitOne( ) method, 17, 20, 26–27, 72, 74,
76, 130, 141–142
■ INDEX
Wait(TimeSpan, CancellationToken)
method
WithCancellation( ) method, 222, 239
CountDownEvent class, 137
WithDegreeOfParallelism( ) method, 222,
236
ManualResetEvent class, 139
WithExecutionMode( ) method, 222, 235
SemaphoreSlim class, 144
WithMergeOptions( ) method, 222, 240
Wait(TimeSpan) method
WorkDuration property, 201
CountDownEvent class, 137
workers, 129
ManualResetEvent class, 139
WriteLine( ) method, 260, 290
SemaphoreSlim class, 144
311