ADPLUS, 307–308 Aggregate Method, 260 ASP.NET MVC Index View, 180 in .NET 4.0 APM, 183 Asynccontroller, 181 EAP, 181 Indexasyn
Total Page:16
File Type:pdf, Size:1020Kb
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 thread 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 thread pool heuristics, 28 multiple threads, 3 NET 4.0 multiple treads, 3 thread pool heuristics, 30 thread scheduling, 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<T>, 105 Task Manager, 300 concurrentdictionary<K,V> Debugging multithreaded applications, 283 (see ConcurrentDictionary<K,V>) data corruption, 283 ConcurrentQueue<T> and deadlocks, 284 ConcurrentStack<T>, 104 interactive debugger, 285 CsvRepository class in production, 285 eager loading, 89 race condition, 283 finer-grain locking, 92 runaway thread, 284 lazy loading (see LazyLoadData method) Visual Studio, 284–285 Lazy<T> 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<T> class, 99 Directory Walker, 105 ConcurrentDictionary<K,V> Double check locking, 94 Add method, 101 AddOrGet method, 102 AddOrUpdate method, 103 E GetorAdd method with Lazy<T>, 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 Lock, 264 Foreground task, 150 323 ■ INDEX H fine-grained parallelism, 235 ForEach Loop, 247 HTTP pipeline, 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<T>, 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<T>, 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