Threading in C# Joseph Albahari

Threading in C# Joseph Albahari

Threading in C# Joseph Albahari Last updated 2011-4-27 Interested in a book on C# and .NET by the same author? See www.albahari.com/nutshell/ Table of Contents Part 1: Getting Started ..................................................................... 4 Introduction and Concepts .............................................................................. 4 Join and Sleep ....................................................................................... 6 How Threading Works ......................................................................... 7 Threads vs Processes ............................................................................ 7 Threading’s Uses and Misuses ............................................................. 8 Creating and Starting Threads ........................................................................ 8 Passing Data to a Thread ...................................................................... 9 Naming Threads ................................................................................. 11 Foreground and Background Threads ................................................ 11 Thread Priority.................................................................................... 12 Exception Handling ............................................................................ 12 Thread Pooling .............................................................................................. 14 Entering the Thread Pool via TPL ...................................................... 15 Entering the Thread Pool Without TPL .............................................. 16 Optimizing the Thread Pool ............................................................... 17 Part 2: Basic Synchronization ....................................................... 19 Synchronization Essentials ........................................................................... 19 Blocking ............................................................................................. 19 Blocking Versus Spinning .................................................................. 20 ThreadState ......................................................................................... 20 Locking ......................................................................................................... 21 Monitor.Enter and Monitor.Exit ......................................................... 22 Choosing the Synchronization Object ................................................ 23 When to Lock ..................................................................................... 23 Locking and Atomicity ....................................................................... 24 Nested Locking ................................................................................... 24 Deadlocks ........................................................................................... 25 Performance ........................................................................................ 26 Mutex .................................................................................................. 26 Semaphore .......................................................................................... 27 © 2006-2010 Joseph Albahari, O’Reilly Media, Inc. All rights reserved. www.albahari.com/threading/ 1 Thread Safety ................................................................................................ 28 Thread Safety and .NET Framework Types ....................................... 28 Thread Safety in Application Servers ................................................. 30 Rich Client Applications and Thread Affinity ................................... 30 Immutable Objects .............................................................................. 32 Signaling with Event Wait Handles .............................................................. 33 AutoResetEvent .................................................................................. 33 ManualResetEvent .............................................................................. 37 CountdownEvent ................................................................................ 38 Creating a Cross-Process EventWaitHandle ...................................... 39 Wait Handles and the Thread Pool ..................................................... 39 WaitAny, WaitAll, and SignalAndWait ............................................. 40 Synchronization Contexts ............................................................................. 41 Reentrancy .......................................................................................... 43 Part 3: Using Threads .................................................................... 44 The Event-Based Asynchronous Pattern ...................................................... 44 BackgroundWorker ....................................................................................... 45 Using BackgroundWorker .................................................................. 45 Subclassing BackgroundWorker ........................................................ 47 Interrupt and Abort ....................................................................................... 48 Interrupt .............................................................................................. 49 Abort ................................................................................................... 49 Safe Cancellation .......................................................................................... 50 Cancellation Tokens ........................................................................... 51 Lazy Initialization ......................................................................................... 52 Lazy<T> ............................................................................................. 53 LazyInitializer..................................................................................... 53 Thread-Local Storage ................................................................................... 54 [ThreadStatic] ..................................................................................... 54 ThreadLocal<T> ................................................................................. 55 GetData and SetData .......................................................................... 55 Timers ........................................................................................................... 56 Multithreaded Timers ......................................................................... 56 Single-Threaded Timers ..................................................................... 58 Part 4: Advanced Topics ............................................................... 59 Nonblocking Synchronization ...................................................................... 59 Memory Barriers and Volatility ......................................................... 59 Interlocked .......................................................................................... 63 Signaling with Wait and Pulse ...................................................................... 65 How to Use Wait and Pulse ................................................................ 65 Producer/Consumer Queue ................................................................. 67 Wait Timeouts .................................................................................... 70 Two-Way Signaling and Races .......................................................... 71 Simulating Wait Handles .................................................................... 72 Writing a CountdownEvent ................................................................ 74 Thread Rendezvous ............................................................................ 75 © 2006-2010 Joseph Albahari, O’Reilly Media, Inc. All rights reserved. www.albahari.com/threading/ 2 The Barrier Class .......................................................................................... 75 Reader/Writer Locks ..................................................................................... 77 Upgradeable Locks and Recursion ..................................................... 78 Suspend and Resume .................................................................................... 80 Aborting Threads .......................................................................................... 80 Complications with Thread.Abort ...................................................... 82 Ending Application Domains ............................................................. 83 Ending Processes ................................................................................ 84 Part 5: Parallel Programming ........................................................ 85 Parallel Programming ................................................................................... 85 Why PFX? .................................................................................................... 85 PFX Concepts ..................................................................................... 86 PFX Components................................................................................ 86 When to Use PFX ............................................................................... 87 PLINQ ........................................................................................................... 87 Parallel Execution Ballistics ..............................................................

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    125 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us