.Net Threading


Published on

An overview of Threading in the .Net framework, including classes that provide threading and classes which handles mutual exclusion of resources.

1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

.Net Threading

  1. 1. .Net Threading Erik Ralston BIS Birds of a Feather July 15th, 2010 1
  2. 2. How do I thread? 2
  3. 3. What is a thread? A single path of execution in a process A stream of instructions given a time-slice by the CPU Logically independent from state and resources 3
  4. 4. Death of Moore’s Law? “Number of transistors on a chip doubles every 2 years” Speed Memory Pixels 4
  5. 5. Rise of Amdahl “Speed increase from multi-tasking is not linear” 5
  6. 6. System.ComponentModel.BackgroundWorker Simple event-oriented threading Intended to run background task of a dialog Ideal for WinForms apps, but not available for WebForms 6
  7. 7. Invoke & BeginInvoke Synchronous and asynchronous calling of delegates Also, method for returning data to the UI thread Good for small, sporadically occurring tasks 7
  8. 8. System.Threading.ThreadPool Group of self-recycling threads that call queued delegates Intended for numerous short duration tasks 8
  9. 9. Timers System.Threading.Timer, System.Timers.Timer, System.Windows.Forms.Timer Many implementations of periodically calling an event What kind of thread is called varies Windows.Forms calls UI Thread (Not True Multithreading) Threading & Timers call worker threads MSDN Comparison of Timer Classes 9
  10. 10. System.Threading.Thread Dedicated instance of a thread Requires the most effort and management For long-running, intensive tasks with custom purpose 10
  11. 11. Thread Class Overview 11
  12. 12. How do I make them share? (Safely) 12
  13. 13. Race Conditions Race conditions happen when shared state depends on sequence of reads and writes (thread operations) Read and write are always separate actions Side-effect of time-slice and separating read & write Thread-Safe – Will not cause race conditions “Thread-Safe” is not the same as “Concurrent” Only using local variables/resource is naturally concurrent 13
  14. 14. Ownership & UI Threads Abort all attempts by other threads to use a resource If any other thread tries to update a WinForm, it will crash Utilize Invoke and BeginInvoke on the Controls objects 14
  15. 15. System.Threading.Interlocked Class with shared “atomic” functions Read and write happen together For simple operations like add, compare, increment, etc 15
  16. 16. SyncLock (Lock in C#) Creates a block that only one thread at a time can enter Code block is called “critical section” Other callers must wait in line Exclusive locking eliminates both the danger and the benefits of multi-threading for the code enclosed Plays into the “parallel portion” for Amdah’s law 16
  17. 17. System.Threading.Mutex Provides mutual exclusion on a single resource Child of WaitHandle class, a class for basic locking A “Global Mutex” can be used for inter- process communication 17
  18. 18. System.Threading.ReaderWriterLock Like a mutex, but provides “many reader, one writer” Ensure thread-safety while better preserving concurrency Similar to “S” versus “U” locking in SQL databases Read vs. write behavior must be controlled manually 18
  19. 19. System.Threading.Semaphore Provides for a pool of resources for many threads Uses functionality from the WaitHandle class 19
  20. 20. System.Threading.Monitor Forces threads to wait until another thread wakes them Must be used in conjunction with another lock Lack of automatic unlocking makes it dangerous Dependence on being in a critical section means concurrency is tough 20
  21. 21. Sharing Isn’t Easy 21
  22. 22. Stop Starvation! Starvation – When a thread is locked so long without a resource it has failed at its duty Deadlock - When two or more threads are preventing progress because they won’t (or can’t) share Timeouts and priority schemes help prevent Livelock – When two or more threads prevent progress for each other while haplessly trying to share 22
  23. 23. Priority Inversion Not all threads execute with the same priority Any thread may access mutually exclusive resources When a low threads locks on a resource, high threads may have to wait for the low thread to finish The scheduler and resource system do not communicate 23
  24. 24. Strategies for Preventing Starvation Use Timeouts – If a thread can’t get a resource for X milliseconds, let it go and try again later Fixed Order – If a thread needs multiple resources, then force them to acquire them in a specific order 24
  25. 25. Other Technologies Parallel Extensions in .Net 4.0 – Adds more language- oriented parallel constructs (parallel loops) and tasks OpenMP – Compiler extensions for C++ that offers language-oriented parallelism Thread Building Blocks – C++ Library with parallel loops and task pipeline without compiler extensions 25
  26. 26. Questions? 26
  27. 27. Thank You! 27