Table of Contents....5
About the Author....11
About the Technical Reviewer....12
Acknowledgments....14
Foreword....15
Introduction....17
Chapter 1: Understanding Tasks....26
Stepping into Parallel Development....26
Introduction to the Task Parallel Library....28
How Does the TPL Help?....29
The Concept of Tasks....29
Creating and Executing a Task....30
The Alternative Approaches....32
Demonstration 1....33
Output....36
Q&A Session....37
Passing and Returning Values....39
Passing Values into Tasks....39
Returning Values from Tasks....42
Demonstration 2....43
Output....44
Q&A Session....45
Continuation Tasks....45
Simple Continuation....46
Demonstration 3....47
Output....47
Specialized Continuation....48
Case Study 1....48
Demonstration 4....49
Output....49
Analysis....50
Q&A Session....50
Case Study 2....52
Demonstration 5....53
Output....54
Nested Tasks....54
Using TaskCreationOptions....56
Demonstration 6....56
Output....57
Q&A Session....57
Using TaskContinuationOptions....58
Demonstration 7....60
Output....61
Analysis....61
Q&A Session....63
Demonstration 8....65
Output....67
Discussion of Waiting....67
Why Do We Wait?....68
Demonstration 9....68
Output....69
Analysis....69
How Do We Wait?....69
Using Sleep....70
Using Delay....71
Using ReadKey()....73
Using Wait....73
Using WaitAll....74
Using WaitAny....74
Using WhenAny....76
Waiting for Cancellation....77
Q&A Session....78
Exercises....82
Summary....86
Solutions to Exercises....86
E1.1....86
E1.2....87
E1.3....88
E1.4....88
E1.5....88
E1.6....89
Additional Note....92
E1.7....93
E1.8....95
Chapter 2: Handling Special Scenarios....96
Introduction to Exceptions....96
Understanding the Challenge....96
Demonstration 1....97
Output....98
Demonstration 2....100
Output....100
Retrieving the Error Details....101
Demonstration 3....102
Output....102
Q&A Session....103
Consider Using AggregateException....104
Exception Management....105
Handling Exceptions in a Single Location....106
Demonstration 4....106
Output....108
Alternative Approaches....108
Alternative Approach 1....108
Alternative Approach 2....109
Q&A Session....110
Demonstration 5....111
Output....112
Handling Exceptions in Multiple Locations....112
Demonstration 6....113
Output....116
Final Suggestion....116
Understanding Cancellations....116
Different Ways of Cancellation....117
Initial Approach....119
Demonstration 7....119
Output....121
Q&A Session....122
Alternative Approach....123
Demonstration 8....123
Output....124
Shortening the Code....125
Q&A Session....126
One Final Note....126
Q&A Session....128
Monitoring Task Cancellation....128
Using Register....128
Using WaitHandle.WaitOne....129
Demonstration 9....130
Output....131
Cancelling Child Tasks....132
Q&A Session....132
Managing Multiple Cancellation Tokens....132
Demonstration 10....133
Output....135
Microsoft’s Note for Visual Studio Users....136
Organizing Exceptions and Cancellations....137
Case Study 1: Using Wait()....137
Demonstration 11....137
Output....139
Case Study 2: Using Wait(token)....140
Q&A Session....141
Handling I/O-Bound Tasks....142
Using TaskCompletionSource....143
Demonstration 12....146
Output....148
Q&A Session....148
Exercises....150
Summary....161
Solutions to Exercises....161
E2.1....161
E2.2....162
E2.3....162
E2.4....162
E2.5....162
E2.6....163
E2.7....163
E2.8....163
E2.9....165
E2.10....165
E2.11....165
E2.12....167
E2.13....167
Chapter 3: Exploring Synchronization and Concurrent Collections....168
Synchronization....168
Understanding Why We Need Synchronization....170
An Erroneous Program....170
Demonstration 1....170
Output....171
Analysis....172
Q&A Session....172
Using Lock Statements....173
Demonstration 2....174
Output....175
Using Interlocked Classes....175
Demonstration 3....177
Output....178
Q&A Session....178
Signaling Using AutoResetEvent....179
Demonstration 4....182
Output....183
Analysis....184
Q&A Session....185
Concurrent Collections....187
System.Collections.Concurrent Namespace....187
IProducerConsumerCollection Interface....188
Notable Characteristics....193
Q&A Session....196
ConcurrentStack....197
Demonstration 5....204
Output....207
Analysis....207
ConcurrentQueue....207
Demonstration 6....209
Output....209
Analysis....210
ConcurrentBag....210
Demonstration 7....215
Output....217
Analysis....218
Q&A Session....218
BlockingCollection....219
Demonstration 8....223
Output....225
Analysis....226
Note from Microsoft....226
ConcurrentDictionary....226
Are Atomicity and Thread-Safety Guaranteed?....231
Demonstration 9....231
Output....234
Analysis....234
Q&A Session....235
Exercises....235
Summary....238
Solutions to Exercises....239
E3.1....239
E3.2....239
E3.3....239
E3.4....240
E3.5....240
E3.6....241
E3.7....243
Chapter 4: Working on Parallel Loops....244
Revisiting Sequential Loops....244
Experimenting with Parallel Loops....245
Introducing the Parallel Class....246
Parallel.ForEach....247
Parallel.For....248
Demonstration 1....249
Output....250
Analysis....251
Parallel.Invoke....251
Demonstration 2....252
Output....252
Q&A Session....253
Scenarios for Parallel Execution....255
Case Study 1....256
Demonstration 3....256
Output....257
Case Study 2....257
Analysis....259
Useful Parameters....259
Using ParallelLoopState....260
Using Stop....260
Demonstration 4....261
Output....261
Using Break....262
Q&A Session....262
Retrieving More Information....263
Using ParallelOptions....266
Demonstration 5....268
Output....269
Analysis....269
Managing Cancellations....270
Following the Previous Approach....270
Recommended Approach....271
Demonstration 6....271
Output....273
Q&A Session....274
Handling Exceptions....275
Demonstration 7....276
Output....278
Fine-Tuning....279
Using Thread-Local Variables....281
Demonstration 8....283
Output....284
Using Partition-Local Variables....285
Additional Note....285
Q&A Session....287
Reviewing Different Coding Styles....290
Introducing the ParallelEnumerable Class....291
Exercises....292
Summary....296
Solutions to Exercises....297
E4.1....297
E4.2....297
E4.3....298
E4.4....299
E4.5....300
E4.6....301
Chapter 5: Parallel LINQ....303
Prerequisite Knowledge....303
Imperative vs. Declarative Programming....303
PLINQ Supports Declarative Programming....304
Understanding LINQ Is Beneficial....305
The Path Toward Parallelism....305
Converting LINQ into PLINQ Using AsParallel....306
Using the ParallelEnumerable Class....307
Introducing ForAll....308
Q&A Session....310
Getting Familiar with PLINQ....310
The First Program....310
Demonstration 1....311
Output....312
Analysis....312
Q&A Session....312
Do Parallel Queries Run Faster Than Sequential Queries?....315
Demonstration 2....315
Output....316
Analysis....317
Q&A Session....318
Demonstration 3....318
Output....319
Enabling Parallelization....319
Output....319
Analysis....320
Q&A Session....320
Forcing Parallelism and Controlling the Degree....322
Demonstration 4....322
Output....323
Merging Data....324
Merge Options....324
Demonstration 5....325
Output....327
Suggestions....328
Q&A Session....328
Managing Special Scenarios....330
Handling Exceptions....330
Demonstration 6....331
Output....333
Handling Cancellations....334
Demonstration 7....335
Output....338
Q&A Session....339
Exercising Aggregation....339
Sequential Custom Aggregation....340
Parallel Custom Aggregation....342
Demonstration 8....343
Output....344
Analysis....345
Q&A Session....345
Exercises....346
Summary....349
Solutions to Exercises....350
E5.1....350
E5.2....351
E5.3....352
E5.4....353
E5.5....353
E5.6....355
Chapter 6: Simplifying Asynchronous Programming....357
Introduction to async and await....358
Understanding async....359
Naming Convention....360
Return Types and Parameters....360
Async Restrictions....362
Q&A Session....362
Understanding await....363
Demonstration 1....364
Output....365
Analysis....365
Q&A Session....367
Await Restrictions....370
Q&A Session....372
State Machines Are Behind the Scenes....373
Q&A Session....381
Task.Run vs. Task.Factory.StartNew....383
Task.Factory.StartNew Treats Ordinary Lambdas and Asynchronous Lambdas Differently....384
Task.Run Treats Ordinary Lambdas and Asynchronous Lambdas Uniformly....385
Unwrapping Using the Unwrap Method....386
Unwrapping Using await....386
Asynchronous Construction....387
Approach 1....390
Demonstration 2....390
Output....392
Analysis....392
Approach 2....392
Demonstration 3....392
Output....393
Analysis....394
Approach 3 (Using a Static Method aka Factory)....396
Demonstration 4....396
Output....397
Analysis....398
Q&A Session....398
Approach 4 (Lazy Initialization)....399
Understanding Lazy....399
Demonstration 5....400
Analysis....401
Using Lazy> for Async Construction....403
Demonstration 6....404
Output....405
Using Nito.AsyncEx Library....405
Introducing ValueTasks....407
Consuming ValueTasks....408
Q&A Session....409
Reviewing Exceptions....411
Exercises....414
Summary....417
Solutions to Exercises....418
E6.1....418
E6.2....418
E6.3....419
E6.4....419
E6.5....420
E6.6....420
E6.7....421
E6.8....421
E6.9....421
E6.10....423
E6.11....425
E6.12....425
If you need to understand concurrency, this book is your guide to the fundamentals behind the advanced software you seek to implement to achieve highly responsive and scalable code. Support for parallel computation is an essential part of concurrency. Concurrency is an advanced concept and solutions are not straightforward. Many developers have been burned by it and are still being burned by it. This book aims to simplify the concept for C# developers. It tries to simplify the concept using the Task Parallel Library (TPL), Concurrent Collections, Parallel LINQ (PLINQ), Asynchronous Programming Patterns, and related topics.
The book starts with an overview of TPL and discusses Tasks. Understanding these areas is necessary to learn the concepts that follow in the book. You will go through special scenarios, such as handling exceptions and cancellations, followed by demonstrations of synchronization techniques and concurrent collections. You will see demonstrations of parallel loops to speed up the computations. And you'll understand PLINQ in detail. Finally, you'll learn how to simplify asynchronous programming with async and await keywords is discussed.
The book contains “Q&A sessions”, review questions, and exercises (in .NET 8 and C#12). After reading the book, you will be able to understand advanced concepts in parallel programming and implement them in your code.
Developers familiar with C# but are absolute beginners for parallel programming.