Nytro Posted September 2, 2011 Report Share Posted September 2, 2011 Threading in C#Joseph AlbahariLast updated 2011-4-27Table of ContentsPart 1: Getting Started ..................................................................... 4Introduction and Concepts .............................................................................. 4Join and Sleep ....................................................................................... 6How Threading Works ......................................................................... 7Threads vs Processes ............................................................................ 7Threading’s Uses and Misuses ............................................................. 8Creating and Starting Threads ........................................................................ 8Passing Data to a Thread ...................................................................... 9Naming Threads ................................................................................. 11Foreground and Background Threads ................................................ 11Thread Priority.................................................................................... 12Exception Handling ............................................................................ 12Thread Pooling .............................................................................................. 14Entering the Thread Pool via TPL ...................................................... 15Entering the Thread Pool Without TPL .............................................. 16Optimizing the Thread Pool ............................................................... 17Part 2: Basic Synchronization ....................................................... 19Synchronization Essentials ........................................................................... 19Blocking ............................................................................................. 19Blocking Versus Spinning .................................................................. 20ThreadState ......................................................................................... 20Locking ......................................................................................................... 21Monitor.Enter and Monitor.Exit ......................................................... 22Choosing the Synchronization Object ................................................ 23When to Lock ..................................................................................... 23Locking and Atomicity ....................................................................... 24Nested Locking ................................................................................... 24Deadlocks ........................................................................................... 25Performance ........................................................................................ 26Mutex .................................................................................................. 26Semaphore .......................................................................................... 27© 2006-2010 Joseph Albahari, O’Reilly Media, Inc. All rights reserved. www.albahari.com/threading/ 2Thread Safety ................................................................................................ 28Thread Safety and .NET Framework Types ....................................... 28Thread Safety in Application Servers ................................................. 30Rich Client Applications and Thread Affinity ................................... 30Immutable Objects .............................................................................. 32Signaling with Event Wait Handles .............................................................. 33AutoResetEvent .................................................................................. 33ManualResetEvent .............................................................................. 37CountdownEvent ................................................................................ 38Creating a Cross-Process EventWaitHandle ...................................... 39Wait Handles and the Thread Pool ..................................................... 39WaitAny, WaitAll, and SignalAndWait ............................................. 40Synchronization Contexts ............................................................................. 41Reentrancy .......................................................................................... 43Part 3: Using Threads .................................................................... 44The Event-Based Asynchronous Pattern ...................................................... 44BackgroundWorker ....................................................................................... 45Using BackgroundWorker .................................................................. 45Subclassing BackgroundWorker ........................................................ 47Interrupt and Abort ....................................................................................... 48Interrupt .............................................................................................. 49Abort ................................................................................................... 49Safe Cancellation .......................................................................................... 50Cancellation Tokens ........................................................................... 51Lazy Initialization ......................................................................................... 52Lazy<T> ............................................................................................. 53LazyInitializer..................................................................................... 53Thread-Local Storage ................................................................................... 54[ThreadStatic] ..................................................................................... 54ThreadLocal<T> ................................................................................. 55GetData and SetData .......................................................................... 55Timers ........................................................................................................... 56Multithreaded Timers ......................................................................... 56Single-Threaded Timers ..................................................................... 58Part 4: Advanced Topics ............................................................... 59Nonblocking Synchronization ...................................................................... 59Memory Barriers and Volatility ......................................................... 59Interlocked .......................................................................................... 63Signaling with Wait and Pulse ...................................................................... 65How to Use Wait and Pulse ................................................................ 65Producer/Consumer Queue ................................................................. 67Wait Timeouts .................................................................................... 70Two-Way Signaling and Races .......................................................... 71Simulating Wait Handles .................................................................... 72Writing a CountdownEvent ................................................................ 74Thread Rendezvous ............................................................................ 75© 2006-2010 Joseph Albahari, O’Reilly Media, Inc. All rights reserved. www.albahari.com/threading/ 3The Barrier Class .......................................................................................... 75Reader/Writer Locks ..................................................................................... 77Upgradeable Locks and Recursion ..................................................... 78Suspend and Resume .................................................................................... 80Aborting Threads .......................................................................................... 80Complications with Thread.Abort ...................................................... 82Ending Application Domains ............................................................. 83Ending Processes ................................................................................ 84Part 5: Parallel Programming ........................................................ 85Parallel Programming ................................................................................... 85Why PFX? .................................................................................................... 85PFX Concepts ..................................................................................... 86PFX Components................................................................................ 86When to Use PFX ............................................................................... 87PLINQ ........................................................................................................... 87Parallel Execution Ballistics ............................................................... 89PLINQ and Ordering .......................................................................... 89PLINQ Limitations ............................................................................. 90Example: Parallel Spellchecker .......................................................... 90Functional Purity ................................................................................ 92Calling Blocking or I/O-Intensive Functions ..................................... 92Cancellation ........................................................................................ 94Optimizing PLINQ ............................................................................. 94Parallelizing Custom Aggregations .................................................... 96The Parallel Class ....................................................................................... 100Parallel.Invoke .................................................................................. 100Parallel.For and Parallel.ForEach ..................................................... 100Task Parallelism .......................................................................................... 105Creating and Starting Tasks .............................................................. 105Waiting on Tasks .............................................................................. 107Exception-Handling Tasks ............................................................... 108Canceling Tasks ................................................................................ 109Continuations .................................................................................... 110Task Schedulers and UIs .................................................................. 113TaskFactory ...................................................................................... 114TaskCompletionSource .................................................................... 114Working with AggregateException ............................................................ 115Flatten and Handle ............................................................................ 116Concurrent Collections ............................................................................... 117IProducerConsumerCollection<T> .................................................. 117ConcurrentBag<T> ........................................................................... 118BlockingCollection<T> .................................................................... 119SpinLock and SpinWait .............................................................................. 121SpinLock........................................................................................... 121SpinWait ........................................................................................... 122[B][/B]Download:http://www.albahari.com/threading/threading.pdf Quote Link to comment Share on other sites More sharing options...
skull Posted September 2, 2011 Report Share Posted September 2, 2011 Super tare. Merci! Quote Link to comment Share on other sites More sharing options...
kozy Posted September 2, 2011 Report Share Posted September 2, 2011 Multam fain ! Quote Link to comment Share on other sites More sharing options...