C# Concurrency....1
brief contents....9
contents....11
preface....16
acknowledgments....18
about this book....20
Who should read this book....20
How this book is organized: A road map....20
About the code....21
liveBook discussion forum....22
about the author....23
about the cover illustration....24
Part 1....25
1 Asynchronous programming and multithreading ....27
1.1 What is multithreading?....28
1.2 Introducing multicore CPUs....30
1.3 Asynchronous programming....32
1.4 Using multithreading and asynchronous programming together....35
1.5 Software efficiency and cloud computing....35
2 The compiler rewrites your code....37
2.1 Lambda functions....38
2.2 Yield return....40
3 The async and await keywords....45
3.1 Asynchronous code complexity....46
3.2 Introducing Task and Task....47
3.2.1 Are we there yet?....49
3.2.2 Wake me up when we get there....50
3.2.3 The synchronous option....51
3.2.4 After the task has completed....52
3.3 How does async/await work?....53
3.4 async void methods....57
3.5 ValueTask and ValueTask....59
3.6 What about multithreading?....60
4 Multithreading basics....62
4.1 Different ways to run in another thread....63
4.1.1 Thread.Start....63
4.1.2 The thread pool....66
4.1.3 Task.Run....68
4.2 Accessing the same variables from multiple threads....70
4.2.1 No shared data....72
4.2.2 Immutable shared data....73
4.2.3 Locks and mutexes....73
4.2.4 Deadlocks....74
4.3 Special considerations for native UI apps....76
4.4 Waiting for another thread....76
4.5 Other synchronization methods....77
4.6 Thread settings....78
4.6.1 Thread background status....79
4.6.2 Language and locale....79
4.6.3 COM Apartment....79
4.6.4 Current user....79
4.6.5 Thread priority....80
5 async/await and multithreading....82
5.1 Asynchronous programming and multithreading....83
5.2 Where does code run after await?....86
5.3 Locks and async/await....88
5.4 UI threads ....91
6 When to use async/await....93
6.1 Asynchronous benefits on servers....94
6.2 Asynchronous benefits on native client applications....99
6.3 The downside of async/await....100
6.3.1 Asynchronous programming is contagious....101
6.3.2 Asynchronous programming has more edge cases....102
6.3.3 Multithreading has even more edge cases....103
6.3.4 async/await is expensive....103
6.4 When to use async await....103
7 Classic multithreading pitfalls and how to avoid them....105
7.1 Partial updates....106
7.2 Memory access reordering....109
7.3 Deadlocks....112
7.4 Race conditions....118
7.5 Synchronization....121
7.6 Starvation....123
Part 2....127
8 Processing a sequence of items in the background....129
8.1 Processing items in parallel....130
8.1.1 Processing items in parallel with the Thread class....131
8.1.2 Processing items in parallel with the thread pool....132
8.1.3 Asynchronously processing items in parallel....134
8.1.4 The Parallel class....136
8.2 Processing items sequentially in the background....139
8.2.1 Processing items sequentially in the background with the Thread class....139
8.2.2 The work queue pattern and BlockingCollection....141
8.2.3 Processing important items with persistent queues....143
9 Canceling background tasks....146
9.1 Introducing CancellationToken....146
9.2 Canceling using an exception....154
9.3 Getting a callback when the caller cancels our operation....154
9.4 Implementing timeouts....155
9.5 Combining cancelation methods....156
9.6 Special cancellation tokens....157
10 Await your own events....158
10.1 Introducing TaskCompletionSource....159
10.2 Choosing where continuations run....163
10.3 Example: Waiting for initialization....164
10.4 Example: Adapting old APIs....165
10.5 Old-style asynchronous operations (BeginXXX, EndXXX)....166
10.6 Example: Asynchronous data structures....167
11 Controlling on which thread your asynchronous code runs....171
11.1 await-threading behavior....172
11.1.1 await in UI threads....172
11.1.2 await in non-UI threads....174
11.2 Synchronization contexts....175
11.3 Breaking away—ConfigureAwait(false)....178
11.4 More ConfigureAwait options....185
11.5 Letting other code run: Task.Yield....186
11.6 Task schedulers....187
12 Exceptions and async/await....190
12.1 Exceptions and asynchronous code....191
12.2 await and AggregateException....194
12.3 The case of the lost exception....195
12.4 Exceptions and async void methods....195
13 Thread-safe collections....197
13.1 The problems with using regular collections ....198
13.2 The concurrent collections....202
13.2.1 ConcurrentDictionary....202
13.2.2 BlockingCollection....205
13.2.3 Async alternatives for BlockingCollection....208
13.2.4 ConcurrentQueue and ConcurrentStack....209
13.2.5 ConcurrentBag....210
13.2.6 When to use the concurrent collections....210
13.2.7 When not to use the concurrent collections ....210
13.3 The immutable collections....211
13.3.1 How immutable collections work....211
13.3.2 How to use the immutable collections ....217
13.3.3 ImmutableInterlocked....218
13.3.4 ImmutableDictionary ....219
13.3.5 ImmutableHashSet and ImmutableSortedSet....221
13.3.6 ImmutableList....221
13.3.7 ImmutableQueue and ImmutableStack....221
13.3.8 ImmutableArray....222
13.3.9 When to use the immutable collections....223
13.4 The frozen collections....223
13.4.1 When to use the frozen collections....225
14 Generating collections asynchronously/await foreach and IAsyncEnumerable....227
14.1 Iterating over an asynchronous collection....228
14.2 Generating an asynchronous collection....230
14.3 Canceling an asynchronous collection....233
14.4 Other options....235
14.5 IAsyncEnumerable and LINQ....236
14.6 Example: Iterating over asynchronously retrieved data....236
14.7 Example: BlockingCollection-like asynchronous queue....237
index....243
C# Concurrency teaches you how to write effective multithreaded and asynchronous software in C#. Practical techniques, real-world examples, and useful code samples cut through the confusion around async/await and help you write rapid, reliable, and bug-free code.
In C# Concurrency Nir Dobovizki, a seasoned C# veteran with over 30 years of high-performance programming experience, shares his deep knowledge and expert techniques. Say goodbye to frustrating pitfalls and impossible-to-find bugs that slow down your applications. Nir's careful approach will teach you how to navigate these challenges with ease, allowing you to achieve lightning-fast performance like never before!
Asynchronous and multithreaded programs can perform multiple tasks simultaneously without losing speed or reliability. But getting concurrency right can challenge even experienced developers. This practical book teaches you to deliver concurrent C# apps that are lighting fast and free of the deadlocks and other synchronization issues that undermine performance and take forever to find.
C# Concurrency equips programmers with a comprehensive understanding of multithreading and asynchronous programming, focusing on the practical use of the C# async-await feature to simplify asynchronous tasks. It teaches how to avoid common pitfalls, addresses classic multithreading issues like deadlocks and race conditions, and advanced topics such as controlling thread of execution and using thread-safe collections.
For experienced C# programmers. No knowledge of asynchronous programming required.