Index

„ „ A page lifecycle, 166 synchronous WebForms implementation, 167 ADPLUS, 307–308 Async and await keywords Aggregate method, 260 asynchronous web page word count, 137 ASP.NET MVC CalculateMeaningOfLifeAsync method, 135 Index View, 180 catch block, 136 in .NET 4.0 coercing result, 135 APM, 183 keyword performance, 136 AsyncController, 181 mechanics EAP, 181 awaiter implementation, 146 IndexAsync implementation, 182 GetAwaiter method, 146 IndexCompleted method INotifyCompletion interface, 146 implementation, 182 TickTockAsync method, 145, 147 Multiple Async I/O Operations, 183 reserved word language, 135 in .NET 4.5, 184 return type method, 135 Synchronous Controller, 179 SynchronizationContext, 137 ASP.NET Web API synchronous structure, 134 in .NET 4.0, 186 synchronous web page word count, 136 in .NET 4.5, 187 task delay Synchronous, 185 synchronous back off and retry, 140 ASP.NET WebForms efficient back off and retry, 140 AuthorRepository implementation, 167 task effects, 137 GetAuthors method, 167 Task.WhenAll in .NET 4.0 downloading documents one by one, 141 Begin method implementation, 171 download many documents Complex Async Page, 174 asynchronous, 141 End method implementation, 172 error handling, 142 error handling, 175 Task.WhenAny IAsyncResult, 172 task code, 143 lifecycle, 170 UI updation, 143 marking, 170 winning task, 143 registering, 171 UI event handler, 133 in .NET 4.5 UI responsive goal, 134 async and await, 178 UI thread RegisterAsyncTask method, 177 API ways, 139 Task.WhenAll, 178 asynchronously loading web page TPL-Friendly Version, 177 and removing adverts, 138

321 ■ index

Async and await keywords (cont.) Asynchronous Programming Model (APM), 14 ConfigureAwait method, 139 Asynchronous UI LoadContent method, 139 BackgroundWorker, 118 RemoveAdverts method, 139 data binding, 120 synchronously loading web page EAP, 117 and removing adverts, 138 mechanics, 113 task continuation Send and Post, 116 method, 138 SynchronizationContext, 115 Asynchronous API task continuations, 116 accessing results, 15 threading model, 114 APM timers delegates, 20 Dispatcher Time, 128 NET Framework, 18 System.Windows, 128 Begin method, 14 UI thread, 129 Completion Notification, 18 Windows Forms, 121 Errors, 15 Windows Presentation Foundation Housekeeping, 17 data-binding layer, 123 IAsyncResult, 14 Dispatcher Time, 128 NET 1.0 Freezable Components, 129 abort method, 8 marshaling, 122 background threads, 10 observable collection, 122 Boolean flag, 9 ReaderWriterLockSlim, 124 Coordinating Threads (Join), 10 synchronization context, 122 interrupt method, 9 user-defined context, 124 JIT compiler, 9 WinRT, 125 memory model, 10 WinRT dispatcher Start Method, 8 CoreWindow.GetForCurrentThread() system.threading.thread, 7 .Dispatcher, 127 Thread class, 11 Priorities, 127 thread’s interaction, COM, 11 RunAsync method, 127 NET 1.1, 21 UI Thread, 127 NET 2.0 WPF Dispatcher closures, 23 APM pattern (BeginInvoke), 125 EAP pattern, 25 Application.Current.Dispatcher, 125 logical and physical Dispatcher Priorities, 126 separation, 22 UI thread, 126 ParameterizedThreadStart, 22 Asynchronous programming SynchronizationContext, 23 mechanisms NET 3.5 multiple machines, 2 lambda expressions, 27 multiple processes, 3 heuristics, 28 multiple threads, 3 NET 4.0 multiple treads, 3 thread pool heuristics, 30 thread , 3 thread pool queue, 28 thread-specific resources work-stealing queues, 30 register values, 4 Polling for Completion, 15 stack, 4 progress, 49 thread local storage (TLS), 4 system thread pool atomic state transition, 58 heuristics, 13 use of, 12 worker and I/O threads, 13 „ „ B thread pool Base class library (BCL), 165 APM, 14 Blocking collection ThreadPool.QueueUserWorkItem, 13 Add method, 108 timers, 13 bounded collection, 109 Waiting for Completion, 16 consuming enumerable, 111 322 ■ Index

feature, 112 Custom scheduler graceful shutdown, 109 unit testing producer/consumer, 106, 108 adding members, 279 Take method, 108 advantages, 277 TPL, 107 GetScheduledTasks, 280 Building task-based combinator issues, 276 OrderByCompletion, 157 synchronization primitives, 277 SetException, 158 WhenAllOrFail task, 159 WhenAny „ „ D Out of the Box task, 156 DebugDiag WhenNext style method, 157 adding rules, 301 crash option, 302 native memory and handle leak, 307 „ „ C performance, 304 CalculateMeaningOfLifeAsync method, 134–135 executing rules, 307 Child task, 53 Debugging Async, 299 Community Technology Preview (CTP), 38 memory dump Component Object Model (COM), 11 bitness, 299 Concurrent data structures DebugDiag (see DebugDiag) API, 98 full dumps, 299 blocking collection (see Blocking collection) mini dumps, 299 ConcurrentBag, 105 Task Manager, 300 concurrentdictionary Debugging multithreaded applications, 283 (see ConcurrentDictionary) data corruption, 283 ConcurrentQueue and deadlocks, 284 ConcurrentStack, 104 interactive debugger, 285 CsvRepository class in production, 285 eager loading, 89 race condition, 283 finer-grain locking, 92 runaway thread, 284 (see LazyLoadData method) Visual Studio, 284–285 Lazy breakpoint and threads, 285 CsvRepository class, 98 Call Stack window, 286 multiple creation, 96 Concurrency Visualizer, 297 non-thread-safe creation, 97 Locals, Autos, and Watch no thread safety, 96 Windows, 286 object creation function, 97 Parallel Stacks Window, 293 use of, 95 Parallel Tasks/Tasks Window, 290 NET collection, 98 Threads window, 289 nonblocking, 107 Dequeue method, 104 Queue class, 99 Directory Walker, 105 ConcurrentDictionary Double check locking, 94 Add method, 101 AddOrGet method, 102 AddOrUpdate method, 103 „ „ E GetorAdd method with Lazy, 102 Enqueue method, 105 initial refactor, 101 Event-based asynchronous pattern (EAP) locking mechanics, 103 cancellation, 26 Map method, 99 error handling, 25 non-thread-safe CsvRepository, 100 multiple async requests, 26 ConcurrentExclusiveSchedulerPair, 263 WebClient, 25 concurrent scheduler, 264 exclusive scheduler, 264 nonthread-Safe, 263 „ „ F, G Reader/Writer , 264 Foreground task, 150

323 ■ index

„ „ H fine-grained parallelism, 235 ForEach Loop, 247 HTTP , 166 For Loop associated tasks, 241 „ „ I, J, K cache memory, 244 calculate pi, 243 Interlocked functionality, 61 C# Loop, 240 Invoke method, 237–239 CPU utilization, 244 I/O Completion Ports (IOCP), 163 implementation, 240 Per-Task Local State, 245 „ „ L Per-Task Value, 245 synchronous version, 247 Large Object Heap (LOH), 76 thread safety, 246 LazyLoadData method Work per Iteration, 242 contention possibility, 91 goals, 234 double check locking, 94 high-level abstraction, 237 goal of, 90 imagine scene, 236 less synchronization, 93 implementation, 236 thread-safe lazy loading, 91 Invoke method, 237–239 LoadContent method, 139 nested loops CalculatePi, 253 „ „ M default partitioner, 254–255 Map method, 89, 99 delegate invocation, 253–254 Memory dumps implementations, 255 bitness, 299 single and equivalent loop, 252 DebugDiag (see DebugDiag) single-threaded version, 252 full dumps, 299 ParallelLoopState mini dumps, 299 AggregateException, 251 SOS, 310 loop termination, 248 commands, 317 ParallelLoopState.Break(), 248 deadlocks, 312 ParallelLoopState.Stop(), 249–250 examining threads, 311 properties, 250 loading extensions, 310 stop request, 250–251 PSSCOR, 319 PLINQ runaway thread, 315 Aggregate method, 260 SOSEX, 318 AsUnOrdered() method, 258 Task Manager, 300 configuration, 259 ADPLUS. ADPLUS extension methods, 256 WinDbg, 309 ForAll method, 259 Model, View, Controller (MVC), 180 IEnumerable, 256 Model-View-View Model (MVVM), 120 input index, 258–259 MoveNext method, 148 LINQ query, 255–256 Multithreaded Apartment (MTA), 11, 268 partitioning, 257 Pre-TPL asynchronous, 233 System.Threading.Tasks, 237 „ „ N, O task and data-based parallelism, 235 Nested task, 53 task scheduler, 237 PSSCOR, 319 „ „ P, Q Parallel Framework Extensions (Pfx), 234 „ „ R Parallel programming Random error notification mechanism, 44 algorithms, 235–236 Razor, 180 CancellationToken, 237 RemoveAdverts method, 139 coarse-grained parallelism, 234 REpresentational State Transfer (REST), 185

324 ■ Index

„ „ S asynchronous Hello World, 31 DataImporter class, 34 Server-side asynchronous factory-style approach, 32 ASP.NET MVC (see ASP.NET MVC) Import method, 33 ASP.NET Web API long-running task, 33 in .NET 4.0, 186 task body, Parameter, 34 in .NET 4.5, 187 thread pool thread, 32 Synchronous, 185 CTP, 38 ASP.NET WebForms (see ASP.NET WebForms) definition, 31 I/O designing task-based APIs, 45 IOCP, 164 error handling load test output, 164 AggregateException, 42 Overlapped, 163 exception handler, role of, 43 synchronous I/O, 162 Handle method, 43 natural parallelism, 161 ignoring errors, 43 WCF (see Windows Communication inner exceptions, 42 Foundation (WCF)) NET 1.1, 41 Single Threaded Apartments (STAs), 11, 268 parent/child relationship, 42 SlowConsume method, 199 try/catch, 41 Son of Strike, 310 underlying exceptions, 42 SOSEX, 318 unhandled exception, 41 SpinLock, 62 XML-based errors, 43 I/O-based tasks APM idiom, 40 „ „ T asynchronous operation, 40 Task CPU, 38 background threads, 150 DownloadPageAsync building task-based combinator (see Building method, 39 task-based combinator DownloadWebPage, 39 foreground task, 150 NET 4.5, 41 TaskCompletionSource, 149 thread pool, 40 thread types, 150 NET 4.0, 43 TPL, 150 NET 4.5, 44 unit test method (see Unit test method) progress, 49 Task delay relationships synchronous back off and retry, 140 chaining tasks (continuations), 51 thread efficient back off and retry, 140 nested and child tasks, 53 Task Parallel Library (TPL) tasks return results, 37 cancellation Task Scheduling API, 48 ConcurrentExclusiveSchedulerPair, 263, 265 asynchronous operation, 46 custom scheduler, 267 cancellation token, 46 basic implementation, 268 cancellation token source, 46 CheckForIdleThread, 276 OperationCancelledException, 47 CheckIdle event, 273 request, 47 enqueued task, 268 wait handle, 48 idle detection abstraction, 272 closures remove idle threads, 272 dangers of, 36 StartNewPoolThread, 275 instance fields, 35 STA threads, 269 lambda, 35 ThreadControl class, 274 passing data, 35 threadsInUse, 271 stack-based reference, 35 threads on demand, 270 compute-based task timer-based idle trigger, 273 asynchronous functionality, 34 TryExecuteTaskInline, 270 asynchronous guaranteed “Hello World”, 32 unit testing, 276

325 ■ index

Task Scheduling (cont.) Large Object Heap buffer pool, 75 GetScheduledTasks, 266 SemaphoreSlim’s Wait method, 78 QueueTask, 266 wrap, LOH Buffer, 76 SynchronizationContextTaskScheduler, 265 signaling ThreadPoolTaskScheduler, 265 API, 70 TryExecuteTask, 267 building block, 72 TryExecuteTaskInline, 267 Consume method, 71 Task.WhenAll implementation, 70 downloading documents one by one, 141 Monitor class, 70 downloading many documents produce method, 71 asynchronously, 141 standard primitives and error handling, 142 kernel objects, 87 Thread Local Storage (TLS), 162 WaitHandle class Thread safety SignalAndWait method, 86 acquisition WaitAll method, 86 Monitor.TryEnter, 69 WaitAny method, 86 timeouts, 69 TickTockAsync method, 145, 147 TryEnter, 68 TPL Dataflow using block, 70 asynchronous blocks, 229 AppDomain boundary, WaitHandle glue block events, 83 batch block, 222 mutex, 83 broadcast blocks, 223 semaphore, 83 buffer block, 220 signaling mechanism, 83 greedy and non-greedy Joining, 227 background printing, 57 WCF-based grid scheduler, 226 cached values, 60 graceful termination CountdownEvent, 80 automatic completion, 215 global synchronization construct, 61 block cancellation, 218 immutable state, 57 block’s Complete method, 213 Interlocked.CompareExchange, 63 error handling, 216 Interlocked.Exchange, 62 premature completion, 214 Interlocked static class linking blocks basic operations, 61 degree of parallelism, 206 richer functions, 62 greedy and non-greedy Blocks, 207 kernel synchronization abstraction, 83 If/else and switch/case, 209 lock-free implementation, 87 ImageProcessor class, 205 ManualResetEventSlim, 78 Load-balance databases, 207 Monitor multiple files process, 203 CLR, 64 recursion, 211 finally block, 66 scalable approach, 203 lock keyword, 67 topology, 203 multistage transition, 64 transform block, 201 NetWorth property, 64 TransformManyBlock, 204 SmallBusiness class, 65 types, 200 Sync Block, 64 multiple threads, 196 try block, 66 pipeline process, 196 multithreaded processing, 60 producer and consumer optimal approach block configuration, 200 ReaderWriterLock, 73 BlockingCollection, 197 ReaderWriterLockSlim, 74 ExecutionDataflowBlockOptions, 199 processing cores, 60 greedy block, 199 rendezvous-based synchronization, 81 output, 198 semaphore Post method, 197 abstractions, buffer pool, 76 SlowConsume method, 199 BufferPool Implementation, 77 two-thread action block, 200

326 ■ Index

„ „ U „ „ V UI thread Visual Studio debugging asynchronously loading web page and breakpoint and threads, 285 removing adverts, 138 Call Stack window ConfigureAwait method, 139 async and await keywords, 286 LoadContent method, 139 Just My Code, 287 RemoveAdverts method, 139 Threads window, 289 synchronously loading web page and flagging and filtering, 289 removing adverts, 138 freezing and thawing, 289 task continuation method, 138 Uniform Resource Identifier (URI), 185 Unit test method „ „ W, X, Y, Z asynchronous WebPage, 153 WinDbg, 309–310 AsyncStubs class, 155 Windows Communication Foundation (WCF) DocumentLoadingTask, 154 in .NET 4.0 DocumentLoadingTask.Wait(), 154 Async Contract, 189 ExecuteSynchronous, 156 error handling, 191 Run-Based Test, 153 TaskCompletionSource, 190 TaskCompletionSource-based stubs, 155 in .NET 4.5, 193 Url method, 152 service functionality, 188 WebPage class, 151 synchronous service, 188

327