You are on page 1of 39

How to Build Multithreaded 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

Session Prerequisites

Component-Based or Object-Oriented
At least one of:

Microsoft® C#
Visual C++®

Overview knowledge of the .NET Framework

Level 300

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

NET Why use threads? Performance Perceived performance Simplify coding .Multi-threading in .

Multi-threading in .NET When 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 .

NET Threading Issues  If threads are so good.Multi-threading in . why not use them everywhere?    Contention for shared resources Need to co-operate to avoid corruption Writing is more of a problem than reading .

Multi-threading in .NET Threading on the Windows® Platform  Win32 threads available from C/C++ COM threads and apartments Single Threaded Apartment (STA)  Multi-Threaded Apartment (MTA)       Developer does not worry about threads Limitation on performance Create thread-aware components Marshal interface pointers across apartments .

{ // Do some work .Threading.. public void myMethod() P1.. ThreadStart ts = new ThreadStart(myMethod). . Thread p1 = new Thread(ts). } .Multi-threading in . p1...Name = "MyThread".NET Simplifies Threading Choices  Common access from all language variants using System.NET .Start().

Multi-threading in .NET Using 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 .

Multi-threading in .NET Simple Sample Application in C# Consumer object and thread Producer object and thread Work queue Work units are arrays of integers Completed queue Completed queue holds sorted work units .

Demonstration 1 Multi-threaded application in C# Tour of Application Running the Application .

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

Synchronization From 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 .

Synchronization Managing 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 .

nextFreeElement++.Synchronization Using a lock Block  Example: lock block in C# SyncLock In VB.NET Object whose lock is obtained lock (this) { workQueue[nextFreeElement] = unit. } Lock released at end of block } Critical code .

Demonstration 2 Adding Synchronization Adding Basic Synchronization .

Exit(this). nextFreeElement++.Enter(this). workQueue[nextFreeElement] = unit.Synchronization Obtaining Locks with Monitors  Example: Monitor usage in C# Monitor. Must remember to explicitly release the lock Critical code . } Monitor.

Demonstration 3 Using Monitors Using Monitors .

ReaderWriterLock .Synchronization Choosing a Synchronization Method  Things to consider    Complexity Ability to split locks Specialized mechanisms.g. e.

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

Inter-thread Communication When Threads Need to Talk  Waiting for a shared resource to become free Notification of the arrival of some work  Waiting for clients     Producer and consumer model Network server model Generally important application events  For example: A long-running task has finished .

Inter-thread Communication Thread lifecycle   Waiting and ready queues States Running Suspended Unstarted Aborted WaitSleepJoin .

Inter-thread Communication  Monitor.Wait()     Choice between them Can only call them when you have the lock   Supply object to wait on Enter WaitSleepJoin Timed waits Monitor.PulseAll()   Simple communication with Wait/Pulse Wait() call gives up the lock until it resumes What happens when Pulse() is called before Wait()? .Pulse() and Monitor.

ManualResetEvent) Timers (server and windows) Interlocked increment and decrement .Inter-thread Communication Notification Alternatives     Pausing/sleeping Events (incl.

Inter-thread Communication Producer Wait/Pulse object and thread in Sample Application Add work unit Pulse Work queue Queue full Carry on .Wait Remove work unit Consumer object and thread .

Demonstration 4 Notification Between Threads Waiting for an Event .

Inter-thread Communication Manipulating other threads   Finding threads Thread information    ThreadState IsAlive Joining another thread  Thread.Join() .

Inter-thread Communication Terminating threads  Stopping a thread  Abort and inconsistent state     Interrupt + InterruptedException Abort As locks are released. who will clean up objects? Alternative approaches using flags  Use join() to check if thread has exited yet .

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

Thread Pools What 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 .

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 .Thread Pools .

Thread Pools Queuing 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 .

NET Event    Define a callback method Register with event producer Event producer then notifies all listeners .Thread Pools Coding 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 .

Use RegisterWaitForSingleObject .Thread Pools Delaying execution    Timed execution of delegates Useful for timer events etc.

Thread Pools Sample Application with thread pool Producer Assign thread to delegate Queue work item Consumer Thread pool .

Demonstration 5 Using a Thread Pool Changing the Consumer Changing the Producer Evaluating the Changes .

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 .

Thank You! .