30
C# Threading 1 C# Threading CNS 3260 C# .NET Software Development

C# Threading1 CNS 3260 C#.NET Software Development

Embed Size (px)

Citation preview

Page 1: C# Threading1 CNS 3260 C#.NET Software Development

C# Threading 1

C# Threading

CNS 3260

C# .NET Software Development

Page 2: C# Threading1 CNS 3260 C#.NET Software Development

C# Threading 2

Timers

• Timers execute a method at a given interval

• Three to choose from:– System.Timers.Timer– System.Threading.Timer– System.Windows.Forms.Timer

Page 3: C# Threading1 CNS 3260 C#.NET Software Development

C# Threading 3

System.Timers.Timer

• Constructors:– Timer()– Timer(double interval)

• Members:– bool AutoReset– bool Enabled– double Interval

• in milliseconds– Close()– Start()– Stop()– Elapsed

• System.Timers.ElapsedEventHandler delegate

Page 4: C# Threading1 CNS 3260 C#.NET Software Development

C# Threading 4

System.Threading.Timer

• Constructor:– Timer(TimerCallBack, object, long, long)

• Members:– Change(long, long)

• TimerCallBack– System.Threading.TimerCallBack(object state)– cannot be changed once set

Page 5: C# Threading1 CNS 3260 C#.NET Software Development

C# Threading 5

System.Windows.Forms.Timer

• Has to be used with Windows Forms• Members:

– bool Enabled– int Interval– Start()– Stop()– Tick

• System.EventHandler delegate

(See Timers Demo)

Page 6: C# Threading1 CNS 3260 C#.NET Software Development

C# Threading 6

Threading: Advantages and Dangers

• Reasons to use Threading– Timers– UI Responsiveness– Multiple processors

• Dangers– Race Conditions– Deadlock

MainThread

ChildThread

Page 7: C# Threading1 CNS 3260 C#.NET Software Development

C# Threading 7

Spawning a new Thread

• IO

• Delegates

• System.Thread

Page 8: C# Threading1 CNS 3260 C#.NET Software Development

C# Threading 8

Threading IO

• FileStream.BeginRead(byte[] buffer, int offset, int numBytes, ASyncCallBack userCallBack, object stateObject)– returns IAsyncResult object

• userCallBack– delegate void ASyncCallBack(IAsyncResult ar)– gets executed when the operation is finished

• FileStream.EndRead(IAsyncResult ar)– Waits (blocks until the read is complete)– returns (int) the number of bytes read

Page 9: C# Threading1 CNS 3260 C#.NET Software Development

C# Threading 9

Threading a Delegate

• BeginInvoke(AsyncCallBack userCallBack, object stateObject)– Returns an IAsyncResult object

– executes userCallBack when finished

• EndInvoke(IAsyncResult ar)– Blocks until the delegate thread is finished

• Use AsyncResult object unless you have specific needs

(See DelegateThreading Demo)

Page 10: C# Threading1 CNS 3260 C#.NET Software Development

C# Threading 10

IAsyncResult

• Members:– AsyncState

• User Object passed in– AsynchWaitHandle

• Gets a thread handle that can be used to block– CompletedSynchronously

• Indicates that the operation was fast enough that the system didn’t actually spawn a thread

• Use with IO operations– IsCompleted

• Indicates whether the operation has completed

• AsyncResult object– extend if you need specialized behavior

Page 11: C# Threading1 CNS 3260 C#.NET Software Development

C# Threading 11

System.Threading.Thread

• For more control over your threading situation• Members:

– static CurrentThread– static Sleep()– Constructor

• Thread(ThreadStart start)– IsBackground– Priority– ThreadState– Abort()– Interrupt()– Join()– Resume()– Start()– Suspend()

Page 12: C# Threading1 CNS 3260 C#.NET Software Development

C# Threading 12

Thread Static Members

• CurrentThread– Returns a thread object for the current thread

• Sleep(int ticks)– 1 tick == 1 ms– Sleep(0) just releases the CPU– Never use a busy-wait

Page 13: C# Threading1 CNS 3260 C#.NET Software Development

C# Threading 13

Thread Constructor

• Takes a ThreadStart delegate

• ThreadStart(void () target)– Takes a method which returns void and has no params:

• void MyMethod()

• Thread begins execution when Start() is called• Thread executes method passed to ThreadStart

Page 14: C# Threading1 CNS 3260 C#.NET Software Development

C# Threading 14

Thread Properties

• IsBackground– get or set– Once all foreground threads are finished, the runtime calls Abort on all

background threads– Default is false (or foreground)

• Priority– ThreadPriority Enum– Lowest, BelowNormal, Normal, AboveNormal, Highest

• ThreadState– New Thread: ThreadState.Unstarted– Started Thread: ThreadState.Running– Sleep called: ThreadState.WaitSleepJoin– Suspend called: ThreadState.Suspended– See ThreadState Enumeration

Page 15: C# Threading1 CNS 3260 C#.NET Software Development

C# Threading 15

Thread Methods

• Start()– Begins execution of the thread– Once a thread is finished, it cannot be restarted

• Suspend()– Suspends the thread– If the thread is already suspended, there is no effect

• Resume()– Resumes a suspended thread

• Interrupt()– Resumes a thread that is in a WaitSleepJoin state– If the thread is not in WaitSleepJoin state it will be interrupted next time it is blocked

Page 16: C# Threading1 CNS 3260 C#.NET Software Development

C# Threading 16

Thread Methods (Continued)

• Abort()– Attempts to abort the thread– Throws a ThreadAbortException

• Join()– Blocks the calling thread until the owning thread terminates

Page 17: C# Threading1 CNS 3260 C#.NET Software Development

C# Threading 17

Exceptions Between threads

• Must be handled in the thread it was thrown in

• Exceptions not handled in the thread are considered unhandled and will terminate that thread

Page 18: C# Threading1 CNS 3260 C#.NET Software Development

C# Threading 18

Threading Dangers

• volatile Fields– The CPU often stores variables in registers for optimization– Other threads accessing that variable may not get the true value

• Race Conditions– When multiple threads access the same memory– A thread is interrupted while updating memory– Solution: “lock” memory in a mutex, monitor, etc.

• Deadlock– Two or more threads waiting on each other to release resources

Page 19: C# Threading1 CNS 3260 C#.NET Software Development

C# Threading 19

volatile Fields

• volatile fields may not be cached• Types that may be volatile are:

– Any reference type– Any pointer (unsafe code)– sbyte, byte, short, ushort, int, uint– An enum with one of the above base types

• Example:

• The Thread class also contains 2 static methods:– VolatileRead()– VolatileWrite()

public volatile int myChangingInt = 0;

Page 20: C# Threading1 CNS 3260 C#.NET Software Development

C# Threading 20

Race Conditions

• Covered thoroughly in CNS 3060

• Solved using a Mutex or Monitor– Called Synchronization

Page 21: C# Threading1 CNS 3260 C#.NET Software Development

C# Threading 21

Synchronization Classes and Constructs

• lock keyword

• Mutex class

• Monitor class

• Interlocked class

• ReaderWriterLock class

Page 22: C# Threading1 CNS 3260 C#.NET Software Development

C# Threading 22

The lock Keyword

• Marks a statement as a critical section• Is a Monitor underneath• Example:

• lockObject must be a reference-type instance• Typically you will lock on:

– ‘this’• locks the current instance

– typeof(MyClass)• global lock for the given type

– A collection instance• locks access to a specific collection

lock(lockObject){ // critical section}

Page 23: C# Threading1 CNS 3260 C#.NET Software Development

C# Threading 23

The lock Mutex

• lock defines one Mutex for each object locked on

• Blocks until the current thread is finished

(See Lock Demo)

Page 24: C# Threading1 CNS 3260 C#.NET Software Development

C# Threading 24

Mutex Class

• Stands for Mutual-Exclusion

• Blocking synchronization object

• WaitOne()– Begins the critical section

• ReleaseMutex()– Ends critical section– Call ReleaseMutex() in a finally block

Page 25: C# Threading1 CNS 3260 C#.NET Software Development

C# Threading 25

Monitor Class (static)• Enter(object obj)

– Begins a critical section– Blocks if another thread has the same lock

• Exit(object obj)– Ends a critical section– Releases the lock

• TryEnter(object obj)– Returns true if it obtains the lock– Returns false if it can’t obtain the lock– Avoids blocking if you can’t obtain the lock

• Wait(object obj)– Releases the lock on an object and blocks until it reaquires the lock

• Pulse(object obj)– Signals the next waiting thread that the lock may be free

• PulseAll(object obj)– Signals all waiting threads that the lock may be free

Page 26: C# Threading1 CNS 3260 C#.NET Software Development

C# Threading 26

Interlocked Class (static)

• Provides atomic operations for variables• CompareExchange(ref int dest, int source, int compare)

– Replaces dest with source if dest == compare– Overloaded

• Exchange(ref int dest, int source)– places source into dest– returns the original value of dest

• Increment(ref int value)– increments value

• Decrement(ref int value)– decrements value

Page 27: C# Threading1 CNS 3260 C#.NET Software Development

C# Threading 27

ReaderWriterLock Class

• Allows a single writer or multiple readers• Members:

– IsReaderLockHeld– IsWriterLockHeld– WriterSeqNum– AcquireReaderLock()

• Increments the reader count– AcquireWriterLock()

• Blocks until the lock is obtained– ReleaseReaderLock()

• Decrements the reader count– ReleaseWriterLock()

• Releases the writer lock– ReleaseLock()

• Unconditionally releases the lock

Page 28: C# Threading1 CNS 3260 C#.NET Software Development

C# Threading 28

Deadlock

• Your job to avoid it

object1

object2

Resource1Resource1

has

has

waiting for

waiting for

Page 29: C# Threading1 CNS 3260 C#.NET Software Development

C# Threading 29

Windows Threading

• Windows disallows updating many GUI components across threads

• To update from the worker thread to the GUI thread– InvokeRequired()– Invoke(System.Delegate)

(See Invoke Demo)

Page 30: C# Threading1 CNS 3260 C#.NET Software Development

C# Threading 30

Mediator Pattern Specialization

GUI Thread Mediator Worker Thread

Exposed Event

public FireEvent()

subscribes

fires whenneeded