net threading

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

Upload: erik-ralston

Post on 30-Nov-2014

1.199 views

Category:

Documents


3 download

DESCRIPTION

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

TRANSCRIPT

Page 1: Net Threading

.Net ThreadingErik Ralston

BIS Birds of a Feather

July 15th, 2010

1

Page 2: Net Threading

How do I thread?

2

Page 3: Net Threading

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

Page 4: Net Threading

Death of Moore’s Law?

“Number of transistors on a chip doubles every 2 years”

Speed

Memory

Pixels

4

Page 5: Net Threading

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

5

Page 6: Net Threading

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

Page 7: Net Threading

Invoke & BeginInvoke

Synchronous and asynchronous calling of delegates

Also, method for returning data to the UI thread

Good for small, sporadically occurring tasks

7

Page 8: Net Threading

System.Threading.ThreadPool

Group of self-recycling threads that call queued delegates

Intended for numerous short duration tasks

8

Page 9: Net Threading

TimersSystem.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

Page 10: Net Threading

System.Threading.Thread

Dedicated instance of a thread

Requires the most effort and management

For long-running, intensive tasks with custom purpose

10

Page 11: Net Threading

Thread Class Overview

11

Page 12: Net Threading

How do I make them share?

(Safely)

12

Page 13: Net Threading

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

Page 14: Net Threading

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

Page 15: Net Threading

System.Threading.Interlocked

Class with shared “atomic” functions

Read and write happen together

For simple operations like add, compare, increment, etc

15

Page 16: Net Threading

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

Page 17: Net Threading

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

Page 18: Net Threading

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

Page 19: Net Threading

System.Threading.Semaphore

Provides for a pool of resources for many threads

Uses functionality from the WaitHandle class

19

Page 20: Net Threading

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

Page 21: Net Threading

Sharing Isn’t Easy

21

Page 22: Net Threading

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

Page 23: Net Threading

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

Page 24: Net Threading

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

Page 25: Net Threading

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

Page 26: Net Threading

Questions?

26

Page 27: Net Threading

Thank You!

27