Cover....1
Title Page....2
Copyright and Credits....3
Dedication....4
About Packt....5
Contributors....6
Table of Contents....8
Preface....16
Section 1: Fundamentals of Threading, Multitasking, and Asynchrony....21
Chapter 1: Introduction to Parallel Programming....22
Technical requirements....23
Preparing for multi-core computing....23
Processes....23
Some more information about the OS....24
Multitasking....24
Hyper-threading....24
Flynn's taxonomy....26
Threads....27
Types of threads....27
Apartment state....27
Multithreading....30
Thread class....32
Advantages and disadvantages of threads....36
The ThreadPool class....36
Advantages, disadvantages, and when to avoid using ThreadPool....39
BackgroundWorker....40
Advantages and disadvantages of using BackgroundWorker....44
Multithreading versus multitasking....44
Scenarios where parallel programming can come in handy....45
Advantages and disadvantages of parallel programming ....45
Summary....46
Questions....47
Chapter 2: Task Parallelism....48
Technical requirements....49
Tasks....49
Creating and starting a task....50
The System.Threading.Tasks.Task class....50
Using lambda expressions syntax....51
Using the Action delegate....51
Using delegate....51
The System.Threading.Tasks.Task.Factory.StartNew method....52
Using lambda expressions syntax....52
Using the Action delegate....52
Using delegate....52
The System.Threading.Tasks.Task.Run method....53
Using lambda expressions syntax....53
Using the Action delegate....53
Using delegate....53
The System.Threading.Tasks.Task.Delay method....54
The System.Threading.Tasks.Task.Yield method....55
The System.Threading.Tasks.Task.FromResult method....58
The System.Threading.Tasks.Task.FromException and System.Threading.Tasks.Task.FromException methods....59
The System.Threading.Tasks.Task.FromCanceled and System.Threading.Tasks.Task.FromCanceled methods....59
Getting results from finished tasks....60
How to cancel tasks....61
Creating a token....62
Creating a task using tokens....62
Polling the status of the token via the IsCancellationRequested property ....62
Registering for a request cancellation using the Callback delegate....63
How to wait on running tasks....65
Task.Wait....66
Task.WaitAll....67
Task.WaitAny....67
Task.WhenAll....68
Task.WhenAny....68
Handling task exceptions....69
Handling exception from single tasks....69
Handling exceptions from multiple tasks....70
Handling task exceptions with a callback function....71
Converting APM patterns into tasks....72
Converting EAPs into tasks....74
More on tasks....76
Continuation tasks....76
Continuing tasks using the Task.ContinueWith method....76
Continuing tasks using Task.Factory.ContinueWhenAll and Task.Factory.ContinueWhenAll....78
Continuing tasks using Task.Factory.ContinueWhenAny and Task.Factory.ContinueWhenAny....78
Parent and child tasks....79
Creating a detached task....79
Creating an attached task....80
Work-stealing queues....81
Summary....84
Chapter 3: Implementing Data Parallelism....85
Technical requirements....85
Moving from sequential loops to parallel loops....86
Using the Parallel.Invoke method....87
Using the Parallel.For method....89
Using the Parallel.ForEach method....90
Understanding the degree of parallelism....91
Creating a custom partitioning strategy....93
Range partitioning....94
Chunk partitioning....94
Canceling loops....95
Using the Parallel.Break method....96
Using ParallelLoopState.Stop....98
Using CancellationToken to cancel loops....98
Understanding thread storage in parallel loops....100
Thread local variable....101
Partition local variable....102
Summary....103
Questions....104
Chapter 4: Using PLINQ....105
Technical requirements....105
LINQ providers in .NET....106
Writing PLINQ queries....107
Introducing the ParallelEnumerable class....107
Our first PLINQ query....108
Preserving order in PLINQ while doing parallel executions....109
Sequential execution using the AsUnOrdered() method....110
Merge options in PLINQ....111
Using the NotBuffered merge option....111
Using the AutoBuffered merge option....112
Using the FullyBuffered merge option....113
Throwing and handling exceptions with PLINQ....115
Combining parallel and sequential LINQ queries....118
Canceling PLINQ queries....119
Disadvantages of parallel programming with PLINQ....120
Understanding the factors that affect the performance of PLINQ (speedups)....121
Degree of parallelism....121
Merge option....121
Partitioning type....121
Deciding when to stay sequential with PLINQ....122
Order of operation....122
ForAll versus calling ToArray() or ToList()....123
Forcing parallelism....123
Generating sequences....123
Summary....124
Questions....124
Section 2: Data Structures that Support Parallelism in .NET Core....126
Chapter 5: Synchronization Primitives....127
Technical requirements....128
What are synchronization primitives?....128
Interlocked operations ....129
Memory barriers in .NET....130
What is reordering? ....131
Types of memory barriers....132
Avoiding code reordering using constructs....133
Introduction to locking primitives....134
How locking works....134
Thread state....135
Blocking versus spinning....136
Lock, mutex, and semaphore....137
Lock....137
Mutex....140
Semaphore....142
Local semaphore....143
Global semaphore....144
ReaderWriterLock....144
Introduction to signaling primitives....144
Thread.Join....145
EventWaitHandle....146
AutoResetEvent....146
ManualResetEvent....147
WaitHandles....150
Lightweight synchronization primitives....153
Slim locks....153
ReaderWriterLockSlim....154
SemaphoreSlim....156
ManualResetEventSlim....156
Barrier and countdown events....157
A case study using Barrier and CountDownEvent....157
SpinWait....160
SpinLock....161
Summary....162
Questions....163
Chapter 6: Using Concurrent Collections....164
Technical requirements....164
An introduction to concurrent collections....165
Introducing IProducerConsumerCollection....165
Using ConcurrentQueue....166
Using queues to solve a producer-consumer problem....167
Solving problems using concurrent queues....168
Performance consideration – Queue versus ConcurrentQueue....169
Using ConcurrentStack....170
Creating a concurrent stack....170
Using ConcurrentBag....171
Using BlockingCollection....173
Creating BlockingCollection....173
A multiple producer-consumer scenario....175
Using ConcurrentDictionary....176
Summary....178
Questions....179
Chapter 7: Improving Performance with Lazy Initialization....180
Technical requirements....180
Introducing lazy initialization concepts....181
Introducing System.Lazy....184
Construction logic encapsulated inside a constructor....184
Construction logic passed as a delegate to Lazy....186
Handling exceptions with the lazy initialization pattern....187
No exceptions occur during initialization....187
Random exception while initialization with exception caching....187
Not caching exceptions....190
Lazy initialization with thread-local storage....191
Reducing the overhead with lazy initializations....193
Summary....196
Questions....197
Section 3: Asynchronous Programming Using C#....198
Chapter 8: Introduction to Asynchronous Programming....199
Technical requirements....199
Types of program execution....200
Understanding synchronous program execution....200
Understanding asynchronous program execution....202
When to use asynchronous programming....203
Writing asynchronous code....203
Using the BeginInvoke method of the Delegate class....204
Using the Task class....205
Using the IAsyncResult interface....206
When not to use asynchronous programming....208
In a single database without connection pooling....208
When it is important that the code is easy to read and maintain....208
For simple and short-running operations....208
For applications with lots of shared resources....209
Problems you can solve using asynchronous code....209
Summary....210
Questions....211
Chapter 9: Async, Await, and Task-Based Asynchronous Programming Basics....212
Technical requirements....213
Introducing async and await....213
The return type of async methods....217
Async delegates and lambda expressions....218
Task-based asynchronous patterns....219
The compiler method, using the async keyword....219
Implementing the TAP manually....219
Exception handling with async code....220
A method that returns Task and throws an exception....220
An async method from outside a try-catch block without the await keyword....221
An async method from inside the try-catch block without the await keyword....223
Calling an async method with the await keyword from outside the try-catch block....225
Methods returning void....226
Async with PLINQ....227
Measuring the performance of async code....228
Guidelines for using async code....231
Avoid using async void....231
Async chain all the way....231
Using ConfigureAwait wherever possible....232
Summary....233
Questions....233
Section 4: Debugging, Diagnostics, and Unit Testing for Async Code....235
Chapter 10: Debugging Tasks Using Visual Studio....236
Technical requirements....237
Debugging with VS 2019 ....237
How to debug threads....237
Using Parallel Stacks windows....241
Debugging using Parallel Stacks windows....241
Threads view....242
Tasks view....243
Debugging using the Parallel Watch window....244
Using Concurrency Visualizer....246
Utilization view....247
Threads view....248
Cores view....249
Summary....249
Questions....250
Further reading ....250
Chapter 11: Writing Unit Test Cases for Parallel and Asynchronous Code....251
Technical requirements....252
Unit testing with .NET Core....252
Understanding the problems with writing unit test cases for async code....254
Writing unit test cases for parallel and async code....257
Checking for a successful result....257
Checking for an exception result when the divisor is 0....258
Mocking the setup for async code using Moq....258
Testing tools....261
Summary....262
Questions ....262
Further reading....263
Section 5: Parallel Programming Feature Additions to .NET Core....264
Chapter 12: IIS and Kestrel in ASP.NET Core....265
Technical requirements....265
IIS threading model and internals....266
Starvation Avoidance....267
Hill Climbing....267
Kestrel threading model and internals....268
ASP.NET Core 1.x....269
ASP.NET Core 2.x....270
Introducing the best practices of threading in microservices....271
Single thread-single process microservices....271
Single thread-multiple process microservices....272
Multiple threads-single process....272
Asynchronous services....272
Dedicated thread pools....272
Introducing async in ASP.NET MVC core....273
Async streams ....277
Summary....280
Questions....281
Chapter 13: Patterns in Parallel Programming....282
Technical requirements....282
The MapReduce pattern....283
Implementing MapReduce using LINQ....283
Aggregation....286
The fork/join pattern....288
The speculative processing pattern....288
The lazy pattern....290
Shared state pattern....293
Summary....293
Questions....294
Chapter 14: Distributed Memory Management....295
Technical requirements....296
Introduction to distributed systems....296
Shared versus distributed memory model....297
Shared memory model....298
Distributed memory model....300
Types of communication network....301
Static communication networks....301
Dynamic communication networks....302
Properties of communication networks....302
Topology....302
Routing algorithms....303
Switching strategy....304
Flow control....304
Exploring topologies....305
Linear and ring topologies....305
Linear arrays....305
Ring or torus....306
Meshes and tori....306
2D mesh....307
2D torus....308
Programming distributed memory machines using message passing....308
Why MPI?....309
Installing MPI on Windows....309
Sample program using MPI....309
Basic send/receive use....310
Collectives....312
Summary....312
Questions....313
Assessments....314
Other Books You May Enjoy....318
Index....321
This book is for C# programmers who want to learn multithreading and parallel programming concepts and want to use them in enterprise applications built using .NET Core. It is also designed for students and professionals who simply want to learn about how parallel programming works with modern-day hardware.It is assumed that you already have some familiarity with the C# programming language and some basic knowledge of how OSes work.