Pro .NET 4 Parallel Programming in C# (eBook)
XV, 328 Seiten
Apress (Verlag)
978-1-4302-2968-1 (ISBN)
Parallel programming has been revolutionised in .NET 4, providing, for the first time, a standardised and simplified method for creating robust, scalable and reliable multi-threaded applications. The Parallel programming features of .NET 4 allow the programmer to create applications that harness the power of multi-core and multi-processor machines. Simpler to use and more powerful than 'classic' .NET threads, parallel programming allows the developer to remain focused on the work an application needs to perform.
In Pro .NET 4 Parallel Programming in C#, Adam Freeman presents expert advice that guides you through the process of creating concurrent C# applications from the ground up. You'll be introduced to .NET's parallel programming features, both old and new, discover the key functionality that has been introduced in .NET 4, and learn how you can take advantage of the power of multi-core and multi-processor machines with ease.
Pro .NET 4 Parallel Programming in C# is a reliable companion that will remain with you as you explore the parallel programming universe, elegantly and comprehensively explaining all aspects of parallel programming, guiding you around potential pitfalls and providing clear-cut solutions to the common problems that you will encounter.
Adam Freeman is an experienced IT professional who has held senior positions in a range of companies, most recently serving as chief technology officer and chief operating officer of a global bank. Now retired, he spends his time writing and long-distance running.
Parallel programming has been revolutionised in .NET 4, providing, for the first time, a standardised and simplified method for creating robust, scalable and reliable multi-threaded applications. The Parallel programming features of .NET 4 allow the programmer to create applications that harness the power of multi-core and multi-processor machines. Simpler to use and more powerful than "e;classic"e; .NET threads, parallel programming allows the developer to remain focused on the work an application needs to perform. In Pro .NET 4 Parallel Programming in C#, Adam Freeman presents expert advice that guides you through the process of creating concurrent C# applications from the ground up. You ll be introduced to .NET s parallel programming features, both old and new, discover the key functionality that has been introduced in .NET 4, and learn how you can take advantage of the power of multi-core and multi-processor machines with ease. Pro .NET4 Parallel Programming in C# is a reliable companion that will remain with you as you explore the parallel programming universe, elegantly and comprehensively explaining all aspects of parallel programming, guiding you around potential pitfalls and providing clear-cut solutions to the common problems that you will encounter.
Adam Freeman is an experienced IT professional who has held senior positions in a range of companies, most recently serving as chief technology officer and chief operating officer of a global bank. Now retired, he spends his time writing and long-distance running.
Title Page 1
Copyright Page 2
Contents at a Glance 4
Table of Contents 5
About the Author 13
About the Technical Reviewer 14
Acknowledgments 15
CHAPTER 1 Introducing Parallel Programming 16
Introducing .NET Parallel Programming 16
What’s in This Book (and What Is Not) 17
Understanding the Benefits (and Pitfalls) of Parallel Programming 18
Considering Overhead 18
Coordinating Data 18
Scaling Applications 18
Deciding When to Go Parallel 18
Deciding When to Stay Sequential 19
Getting Prepared for This Book 19
Understanding the Structure of This Book 19
Getting the Example Code 20
Summary 21
CHAPTER 2 Task Programming 22
Hello Task 22
Creating and Starting Tasks 23
Creating Simple Tasks 24
Setting Task State 26
Getting a Result 28
Specifying Task Creation Options 30
Identifying Tasks 30
Cancelling Tasks 30
Monitoring Cancellation by Polling 32
Monitoring Cancellation with a Delegate 34
Monitoring Cancellation with a Wait Handle 35
Cancelling Several Tasks 37
Creating a Composite Cancellation Token 38
Determining If a Task Was Cancelled 39
Waiting for Time to Pass 40
Using a Cancellation Token Wait Handle 41
Using Classic Sleep 42
Using Spin Waiting 44
Waiting for Tasks 45
Waiting for a Single Task 46
Waiting for Several Tasks 48
Waiting for One of Many Tasks 49
Handling Exceptions in Tasks 50
Handling Basic Exceptions 51
Using an Iterative Handler 52
Reading the Task Properties 54
Using a Custom Escalation Policy 56
Getting the Status of a Task 58
Executing Tasks Lazily 58
Understanding Common Problems and Their Causes 60
Task Dependency Deadlock 60
Solution 60
Example 60
Local Variable Evaluation 61
Solution 61
Example 61
Excessive Spinning 62
Solution 62
Example 62
Summary 63
CHAPTER 3 Sharing Data 64
The Trouble with Data 65
Going to the Races 65
Creating Some Order 66
Executing Sequentially 67
Executing Immutably 67
Executing in Isolation 68
Synchronizing Execution 74
Defining Critical Regions 74
Defining Synchronization Primitives 74
Using Synchronization Wisely 75
Don’t Synchronize Too Much 76
Don’t Synchronize Too Little 76
Pick the Lightest Tool 76
Don’t Write Your Own Synchronization Primitives 76
Using Basic Synchronization Primitives 76
Locking and Monitoring 77
Using Interlocked Operations 82
Using Spin Locking 85
Using Wait Handles and the Mutex Class 87
Acquiring Multiple Locks 89
Configuring Interprocess Synchronization 91
Using Declarative Synchronization 93
Using Reader-Writer Locks 94
Using the ReaderWriterLockSlim Class 94
Using Recursion and Upgradable Read Locks 98
Working with Concurrent Collections 102
Using .NET 4 Concurrent Collection Classes 103
ConcurrentQueue 104
ConcurrentStack 106
ConcurrentBag 108
ConcurrentDictionary 109
Using First-Generation Collections 112
Using Generic Collections 114
Common Problems and Their Causes 115
Unexpected Mutability 115
Solution 115
Example 115
Multiple Locks 116
Solution 117
Example 117
Lock Acquisition Order 118
Solution 119
Example 119
Orphaned Locks 120
Solution 120
Example 120
Summary 122
CHAPTER 4 Coordinating Tasks 123
Doing More with Tasks 124
Using Task Continuations 124
Creating Simple Continuations 125
Creating One-to-Many Continuations 127
Creating Selective Continuations 129
Creating Many-to-One and Any-To-One Continuations 131
Canceling Continuations 134
Waiting for Continuations 136
Handling Exceptions 136
Creating Child Tasks 140
Using Synchronization to Coordinate Tasks 143
Barrier 145
CountDownEvent 150
ManualResetEventSlim 153
AutoResetEvent 155
SemaphoreSlim 157
Using the Parallel Producer/Consumer Pattern 160
Creating the Pattern 161
Creating a BlockingCollection instance 162
Selecting the Collection Type 163
Creating the Producers 163
Creating the Consumer 164
Combining Multiple Collections 166
Using a Custom Task Scheduler 170
Creating a Custom Scheduler 170
Using a Custom Scheduler 174
Common Problems and Their Causes 176
Inconsistent/Unchecked Cancellation 176
Solution 176
Example 176
Assuming Status on Any-To-One Continuations 178
Solution 178
Example 178
Trying to Take Concurrently 179
Solution 179
Example 179
Reusing Objects in Producers 180
Solution 180
Example 180
Using BlockingCollection as IEnumerable 182
Solution 182
Example 182
Deadlocked Task Scheduler 183
Solution 183
Example 183
Summary 186
CHAPTER 5 Parallel Loops 187
Parallel vs. Sequential Loops 187
The Parallel Class 189
Invoking Actions 189
Using Parallel Loops 190
Creating a Basic Parallel For Loop 191
Creating a Basic Parallel ForEach Loop 193
Setting Parallel Loop Options 195
Breaking and Stopping Parallel Loops 197
Handling Parallel Loop Exceptions 201
Getting Loop Results 202
Canceling Parallel Loops 203
Using Thread Local Storage in Parallel Loops 204
Performing Parallel Loops with Dependencies 207
Selecting a Partitioning Strategy 209
Using the Chunking Partitioning Strategy 210
Using the Ordered Default Partitioning Strategy 213
Creating a Custom Partitioning Strategy 214
Writing a Contextual Partitioner 215
Writing an Orderable Contextual Partitioner 223
Common Problems and Their Causes 228
Synchronization in Loop Bodies 228
Solution 228
Example 229
Loop Body Data Races 229
Solution 229
Example 229
Using Standard Collections 230
Solution 230
Example 230
Using Changing Data 231
Solution 231
Example 231
Summary 232
CHAPTER 6 Parallel LINQ 233
LINQ, But Parallel 233
Using PLINQ Queries 236
Using PLINQ Query Features 239
Ordering Query Results 240
Using Ordered Subqueries 244
Performing a No-Result Query 245
Managing Deferred Query Execution 246
Controlling Concurrency 248
Forcing Parallelism 249
Limiting Parallelism 250
Forcing Sequential Execution 251
Handling PLINQ Exceptions 252
Cancelling PLINQ Queries 253
Setting Merge Options 254
Using Custom Partitioning 256
Using Custom Aggregation 259
Generating Parallel Ranges 260
Common Problems and Their Causes 261
Forgetting the PLINQ Basics 261
Solution 261
Creating Race Conditions 262
Solution 262
Example 262
Confusing Ordering 262
Solution 263
Example 263
Sequential Filtering 263
Solution 264
Example 264
Summary 264
CHAPTER 7 Testing and Debugging 265
Making Things Better When Everything Goes Wrong 265
Measuring Parallel Performance 266
Using Good Coding Strategies 266
Using Synchronization Sparingly 266
Using Synchronization Readily 266
Partitioning Work Evenly 266
Avoiding Parallelizing Small Work Loads 266
Measure Different Degrees of Concurrency 267
Making Simple Performance Comparisons 267
Performing Parallel Analysis with Visual Studio 270
Finding Parallel Bugs 274
Debugging Program State 275
Handling Exceptions 279
Detecting Deadlocks 281
Summary 283
CHAPTER 8 Common Parallel Algorithms 284
Sorting, Searching, and Caching 284
Using Parallel Quicksort 284
The Code 285
Using the Code 286
Traversing a Parallel Tree 287
The Code 287
Using the Code 288
Searching a Parallel Tree 289
The Code 289
Using the Code 290
Using a Parallel Cache 291
The Code 292
Using the Code 292
Using Parallel Map and Reductions 293
Using a Parallel Map 293
The Code 293
Using the Code 294
Using a Parallel Reduction 295
The Code 295
Using the Code 295
Using Parallel MapReduce 296
The Code 296
Using the Code 297
Speculative Processing 298
Selection 298
The Code 298
Using the Code 300
Speculative Caching 301
The Code 301
Using the Code 302
Using Producers and Consumers 303
Decoupling the Console Class 303
The Code 303
Using the Code 304
Creating a Pipeline 305
The Code 305
Using the Code 306
Index 308
Erscheint lt. Verlag | 11.1.2011 |
---|---|
Zusatzinfo | XV, 328 p. |
Verlagsort | Berkeley |
Sprache | englisch |
Themenwelt | Informatik ► Programmiersprachen / -werkzeuge ► NET Programmierung |
Informatik ► Software Entwicklung ► Objektorientierung | |
Schlagworte | algorithms • C# • Class • Debugging • LINQ • Parallel Programming • Scala |
ISBN-10 | 1-4302-2968-3 / 1430229683 |
ISBN-13 | 978-1-4302-2968-1 / 9781430229681 |
Haben Sie eine Frage zum Produkt? |
Größe: 2,9 MB
DRM: Digitales Wasserzeichen
Dieses eBook enthält ein digitales Wasserzeichen und ist damit für Sie personalisiert. Bei einer missbräuchlichen Weitergabe des eBooks an Dritte ist eine Rückverfolgung an die Quelle möglich.
Dateiformat: PDF (Portable Document Format)
Mit einem festen Seitenlayout eignet sich die PDF besonders für Fachbücher mit Spalten, Tabellen und Abbildungen. Eine PDF kann auf fast allen Geräten angezeigt werden, ist aber für kleine Displays (Smartphone, eReader) nur eingeschränkt geeignet.
Systemvoraussetzungen:
PC/Mac: Mit einem PC oder Mac können Sie dieses eBook lesen. Sie benötigen dafür einen PDF-Viewer - z.B. den Adobe Reader oder Adobe Digital Editions.
eReader: Dieses eBook kann mit (fast) allen eBook-Readern gelesen werden. Mit dem amazon-Kindle ist es aber nicht kompatibel.
Smartphone/Tablet: Egal ob Apple oder Android, dieses eBook können Sie lesen. Sie benötigen dafür einen PDF-Viewer - z.B. die kostenlose Adobe Digital Editions-App.
Zusätzliches Feature: Online Lesen
Dieses eBook können Sie zusätzlich zum Download auch online im Webbrowser lesen.
Buying eBooks from abroad
For tax law reasons we can sell eBooks just within Germany and Switzerland. Regrettably we cannot fulfill eBook-orders from other countries.
aus dem Bereich