.Net Threading

eralston 748 views 27 slides Jul 15, 2010
Slide 1
Slide 1 of 27
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27

About This Presentation

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


Slide Content

.Net Threading
Erik Ralston
BIS Birds of a Feather
July 15
th
, 2010
1

How do I thread?
2

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

Death of Moore’s Law?
“Number of transistors on a chip doubles every 2 years”
Speed
Memory
Pixels
4

Rise of Amdahl
“Speed increase from multi-tasking is not linear”
5

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

Invoke & BeginInvoke
Synchronous and asynchronous calling of delegates
Also, method for returning data to the UI thread
Good for small, sporadically occurring tasks
7

System.Threading.ThreadPool
Group of self-recycling threads that call queued delegates
Intended for numerous short duration tasks
8

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

System.Threading.Thread
Dedicated instance of a thread
Requires the most effort and management
For long-running, intensive tasks with custom purpose
10

Thread Class Overview
11

How do I make them
share?
(Safely)
12

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

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

System.Threading.Interlocked
Class with shared “atomic” functions
Read and write happen together
For simple operations like add, compare, increment, etc
15

SyncLock
(Lockin 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

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

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

System.Threading.Semaphore
Provides for a pool of resources for many threads
Uses functionality from the WaitHandle class
19

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

Sharing Isn’t Easy
21

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

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

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

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

Questions?
26

Thank You!
27