How to Build Multi- threaded Applications in.NET Mazen S. Alzogbi Technology Specialist Microsoft...

39
How to Build Multi- threaded Applications in .NET Mazen S. Alzogbi Technology Specialist Microsoft Corporation

Transcript of How to Build Multi- threaded Applications in.NET Mazen S. Alzogbi Technology Specialist Microsoft...

Page 1: How to Build Multi- threaded Applications in.NET Mazen S. Alzogbi Technology Specialist Microsoft Corporation.

How to Build Multi-threaded Applications in .NET

Mazen S. AlzogbiTechnology SpecialistMicrosoft Corporation

Page 2: How to Build Multi- threaded Applications in.NET Mazen S. Alzogbi Technology Specialist Microsoft Corporation.

What we will cover The purpose of building multi-threading applications When multi-threading should be used, the pros and cons for multi-threaded applications The various multi-threading implementations, the pros and cons for each. How .NET simplifies building multi-threaded applications

Page 3: How to Build Multi- threaded Applications in.NET Mazen S. Alzogbi Technology Specialist Microsoft Corporation.

Session Prerequisites

Component-Based or Object-Oriented Development

At least one of: Microsoft® C# VB.NET Visual C++®

Overview knowledge of the .NET Framework MTS, COM, DCOM, COM+

Level 300Level 300

Page 4: How to Build Multi- threaded Applications in.NET Mazen S. Alzogbi Technology Specialist Microsoft Corporation.

Agenda

Multi-threading in .NET Synchronization Inter-thread communication Improving Performance with Thread Pools

Page 5: How to Build Multi- threaded Applications in.NET Mazen S. Alzogbi Technology Specialist Microsoft Corporation.

Multi-threading in .NETWhy use threads?

PerformancePerformance

PerceivedPerceivedperformanceperformance

Simplify codingSimplify coding

Page 6: How to Build Multi- threaded Applications in.NET Mazen S. Alzogbi Technology Specialist Microsoft Corporation.

Multi-threading in .NETWhen Should You Use Threads?

Take advantage of multi-processor systems for intensive computation Separate threads for different tasks

Keeping the user interface "live" Waiting for I/O Split out complex test conditions

and loops Creating servers

Local and network

Page 7: How to Build Multi- threaded Applications in.NET Mazen S. Alzogbi Technology Specialist Microsoft Corporation.

Multi-threading in .NETThreading Issues

If threads are so good, why not use them everywhere?

Contention for shared resources Need to co-operate to avoid corruption Writing is more of a problem than reading

Page 8: How to Build Multi- threaded Applications in.NET Mazen S. Alzogbi Technology Specialist Microsoft Corporation.

Multi-threading in .NETThreading on the Windows® Platform

Win32 threads available from C/C++ COM threads and apartments Single Threaded Apartment (STA)

Developer does not worry about threads Limitation on performance

Multi-Threaded Apartment (MTA) Create thread-aware components Marshal interface pointers across apartments

Page 9: How to Build Multi- threaded Applications in.NET Mazen S. Alzogbi Technology Specialist Microsoft Corporation.

Multi-threading in .NET.NET Simplifies Threading Choices

Common access from all language variants

using System.Threading;using System.Threading;......ThreadStart ts = new ThreadStart(myMethod);ThreadStart ts = new ThreadStart(myMethod);Thread p1 = new Thread(ts);Thread p1 = new Thread(ts);p1.Name = "MyThread";p1.Name = "MyThread";P1.Start();P1.Start();

public void myMethod()public void myMethod(){{

// Do some work// Do some work......

}}

Page 10: How to Build Multi- threaded Applications in.NET Mazen S. Alzogbi Technology Specialist Microsoft Corporation.

Multi-threading in .NETUsing Threads

Threads started by application Pass in parameters as properties on target object Threads stop when method is exited

Thread object cannot be re-used Can stop thread with Abort method

Distinguish between thread and object

Page 11: How to Build Multi- threaded Applications in.NET Mazen S. Alzogbi Technology Specialist Microsoft Corporation.

Multi-threading in .NETSimple Sample Application in C#

CompletedCompletedqueuequeue

WorkWorkqueuequeue

ProducerProducerobject andobject and

threadthread

ConsumerConsumerobject andobject and

threadthread

Work unitsWork unitsare arrays ofare arrays of

integersintegers

CompletedCompletedqueue holds queue holds sorted worksorted work

unitsunits

Page 12: How to Build Multi- threaded Applications in.NET Mazen S. Alzogbi Technology Specialist Microsoft Corporation.

Demonstration 1Multi-threaded application

in C#

Tour of ApplicationRunning the Application

Page 13: How to Build Multi- threaded Applications in.NET Mazen S. Alzogbi Technology Specialist Microsoft Corporation.

Agenda

Multi-threading in .NET Synchronization Inter-thread communication Improving Performance with Thread Pools

Page 14: How to Build Multi- threaded Applications in.NET Mazen S. Alzogbi Technology Specialist Microsoft Corporation.

SynchronizationFrom STA to MTA

Developer must consider threading issues Multiple threads contend for resources

Instance variables Database connections GUI controls

Writing is more of a problem than reading Identify critical regions that access resources Some more critical than others

Page 15: How to Build Multi- threaded Applications in.NET Mazen S. Alzogbi Technology Specialist Microsoft Corporation.

SynchronizationManaging Contention

Protect critical regions with synchronization Objects have a lock that can be obtained

Only one thread can own the lock at one time Other threads can block and wait… … or test the lock and take alternative action

Very important to release locks Deadlock and "liveness problems" can ensue

Page 16: How to Build Multi- threaded Applications in.NET Mazen S. Alzogbi Technology Specialist Microsoft Corporation.

SynchronizationUsing a lock Block

Example: lock block in C#

lock (this)lock (this){{ workQueue[nextFreeElement] = unit;workQueue[nextFreeElement] = unit; nextFreeElement++;nextFreeElement++;}}

Object whose lock is obtainedObject whose lock is obtained

CriticalCriticalcodecode

Lock released at end of blockLock released at end of block

}}

SyncLockSyncLockIn VB.NETIn VB.NET

Page 17: How to Build Multi- threaded Applications in.NET Mazen S. Alzogbi Technology Specialist Microsoft Corporation.

Demonstration 2Adding Synchronization

Adding Basic Synchronization

Page 18: How to Build Multi- threaded Applications in.NET Mazen S. Alzogbi Technology Specialist Microsoft Corporation.

SynchronizationObtaining Locks with Monitors

Example: Monitor usage in C#

Monitor.Enter(this);Monitor.Enter(this);

workQueue[nextFreeElement] = unit;workQueue[nextFreeElement] = unit;nextFreeElement++;nextFreeElement++;

Monitor.Exit(this);Monitor.Exit(this);

CriticalCriticalcodecode}}

Must remember to explicitly release the lockMust remember to explicitly release the lock

Page 19: How to Build Multi- threaded Applications in.NET Mazen S. Alzogbi Technology Specialist Microsoft Corporation.

Demonstration 3Using Monitors

Using Monitors

Page 20: How to Build Multi- threaded Applications in.NET Mazen S. Alzogbi Technology Specialist Microsoft Corporation.

SynchronizationChoosing a Synchronization Method

Things to consider Complexity Ability to split locks Specialized mechanisms, e.g. ReaderWriterLock

Page 21: How to Build Multi- threaded Applications in.NET Mazen S. Alzogbi Technology Specialist Microsoft Corporation.

Agenda

Multi-threading in .NET Synchronization Inter-thread Communication Improving Performance with Thread

Pools

Page 22: How to Build Multi- threaded Applications in.NET Mazen S. Alzogbi Technology Specialist Microsoft Corporation.

Inter-thread CommunicationWhen Threads Need to Talk

Waiting for a shared resource to become free Notification of the arrival of some work

Producer and consumer model Waiting for clients

Network server model Generally important application events

For example: A long-running task has finished

Page 23: How to Build Multi- threaded Applications in.NET Mazen S. Alzogbi Technology Specialist Microsoft Corporation.

Inter-thread CommunicationThread lifecycle

Waiting and ready queues States

UnstartedUnstarted AbortedAborted

RunningRunning SuspendedSuspended

WaitSleepJoinWaitSleepJoin

Page 24: How to Build Multi- threaded Applications in.NET Mazen S. Alzogbi Technology Specialist Microsoft Corporation.

Inter-thread CommunicationSimple communication with Wait/Pulse

Monitor.Wait() Supply object to wait on Enter WaitSleepJoin Timed waits

Monitor.Pulse() and Monitor.PulseAll() Choice between them

Can only call them when you have the lock Wait() call gives up the lock until it resumes

What happens when Pulse() is called before Wait()?

Page 25: How to Build Multi- threaded Applications in.NET Mazen S. Alzogbi Technology Specialist Microsoft Corporation.

Inter-thread CommunicationNotification Alternatives

Pausing/sleeping Events (incl. ManualResetEvent) Timers (server and windows) Interlocked increment and decrement

Page 26: How to Build Multi- threaded Applications in.NET Mazen S. Alzogbi Technology Specialist Microsoft Corporation.

Inter-thread Communication

Wait/Pulse in Sample Application

WorkWorkqueuequeue

ProducerProducerobject andobject and

threadthread

ConsumerConsumerobject andobject and

threadthread

Add workAdd workunitunit

Queue fullQueue full- Wait- Wait

RemoveRemovework unitwork unit

PulsePulse

Carry onCarry on

Add workAdd workunitunit

Page 27: How to Build Multi- threaded Applications in.NET Mazen S. Alzogbi Technology Specialist Microsoft Corporation.

Demonstration 4Notification Between

Threads

Waiting for an Event

Page 28: How to Build Multi- threaded Applications in.NET Mazen S. Alzogbi Technology Specialist Microsoft Corporation.

Inter-thread CommunicationManipulating other threads

Finding threads Thread information

ThreadState IsAlive

Joining another thread Thread.Join()

Page 29: How to Build Multi- threaded Applications in.NET Mazen S. Alzogbi Technology Specialist Microsoft Corporation.

Inter-thread CommunicationTerminating threads

Stopping a thread Interrupt + InterruptedException Abort

Abort and inconsistent state As locks are released, who will clean up objects?

Alternative approaches using flags Use join() to check if thread has exited yet

Page 30: How to Build Multi- threaded Applications in.NET Mazen S. Alzogbi Technology Specialist Microsoft Corporation.

Agenda

Multi-threading in .NET Synchronization Inter-thread communication Improving Performance with Thread

Pools

Page 31: How to Build Multi- threaded Applications in.NET Mazen S. Alzogbi Technology Specialist Microsoft Corporation.

Thread PoolsWhat are thread pools?

Set of "worker" threads Thread controller assigns them to tasks Submit task to thread controller Number of threads in pool can grow or shrink

Thread controller can use common metrics Developer does not have to worry about starting and terminating threads

Useful for optimizing thread usage where there are many clients or units of work

Page 32: How to Build Multi- threaded Applications in.NET Mazen S. Alzogbi Technology Specialist Microsoft Corporation.

Thread Pools.NET thread pools

One thread pool per-process Pool is created when first work item is added Developer has no control over number of threads created Do NOT Abort pooled threads

Page 33: How to Build Multi- threaded Applications in.NET Mazen S. Alzogbi Technology Specialist Microsoft Corporation.

Thread PoolsQueuing work items

ThreadPool class QueueUserWorkItem static method

Work item consists of Method to run is defined by WaitCallback delegate (public void (object state)) State to be passed as object

Still pass other state by setting variables on the method's object

Page 34: How to Build Multi- threaded Applications in.NET Mazen S. Alzogbi Technology Specialist Microsoft Corporation.

Thread PoolsCoding to work with thread pools

Still need all synchronization as before Detection of when job is finished Events work well in this environment Use a .NET Event

Define a callback method Register with event producer Event producer then notifies all listeners

Page 35: How to Build Multi- threaded Applications in.NET Mazen S. Alzogbi Technology Specialist Microsoft Corporation.

Thread PoolsDelaying execution

Timed execution of delegates Useful for timer events etc. Use RegisterWaitForSingleObject

Page 36: How to Build Multi- threaded Applications in.NET Mazen S. Alzogbi Technology Specialist Microsoft Corporation.

Thread PoolsSample Application with thread pool

Consumer

Thread poolThread pool

Producer

QueueQueuework itemwork item

Assign threadAssign threadto delegateto delegate

Page 37: How to Build Multi- threaded Applications in.NET Mazen S. Alzogbi Technology Specialist Microsoft Corporation.

Demonstration 5Using a Thread Pool

Changing the Consumer Changing the ProducerEvaluating the Changes

Page 38: How to Build Multi- threaded Applications in.NET Mazen S. Alzogbi Technology Specialist Microsoft Corporation.

Session Summary .NET has powerful, cross-language threading capabilities .NET makes it easy to use multiple threads

Comprehensive library of classes and delegates Benefits of multi-threading include

Simpler code Faster execution Improved user perception of performance

Developer must think more about thread interaction

Page 39: How to Build Multi- threaded Applications in.NET Mazen S. Alzogbi Technology Specialist Microsoft Corporation.

Thank You!