How to Build Multithreaded Applications in NET Mazen
How to Build Multithreaded Applications in. NET Mazen S. Alzogbi Technology Specialist Microsoft Corporation
What we will cover u u 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 u u Component-Based or Object-Oriented Development At least one of: Ø Ø Ø u u Microsoft® C# VB. NET Visual C++® Overview knowledge of the. NET Framework MTS, COM, DCOM, COM+ Level 300
Agenda u u Multi-threading in. NET Synchronization Inter-thread communication Improving Performance with Thread Pools
Multi-threading in. NET Why use threads? Performance Perceived performance Simplify coding
Multi-threading in. NET When Should You Use Threads? u u Take advantage of multi-processor systems for intensive computation Separate threads for different tasks Ø Ø Ø u Keeping the user interface "live" Waiting for I/O Split out complex test conditions and loops Creating servers Ø Local and network
Multi-threading in. NET Threading Issues u u 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
Multi-threading in. NET Threading on the Windows® Platform u u u Win 32 threads available from C/C++ COM threads and apartments Single Threaded Apartment (STA) Ø Ø u Developer does not worry about threads Limitation on performance Multi-Threaded Apartment (MTA) Ø Ø Create thread-aware components Marshal interface pointers across apartments
Multi-threading in. NET Simplifies Threading Choices u Common access from all language variants using System. Threading; . . . Thread. Start ts = new Thread. Start(my. Method); Thread p 1 = new Thread(ts); p 1. Name = "My. Thread"; public void my. Method() P 1. Start(); { // Do some work. . . }
Multi-threading in. NET Using Threads u u u Threads started by application Pass in parameters as properties on target object Threads stop when method is exited Ø Ø u 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 holds sorted work units
Demonstration 1 Multi-threaded application in C# Tour of Application Running the Application
Agenda u u Multi-threading in. NET Synchronization Inter-thread communication Improving Performance with Thread Pools
Synchronization From STA to MTA u u Developer must consider threading issues Multiple threads contend for resources Ø Ø Ø u u u 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 u u Protect critical regions with synchronization Objects have a lock that can be obtained Ø Ø Ø u 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
Synchronization Using a lock Block u Example: lock block in C# Sync. Lock In VB. NET Object whose lock is obtained lock (this) { work. Queue[next. Free. Element] = unit; next. Free. Element++; } Lock released at end of block } Critical code
Demonstration 2 Adding Synchronization Adding Basic Synchronization
Synchronization Obtaining Locks with Monitors u Example: Monitor usage in C# Monitor. Enter(this); work. Queue[next. Free. Element] = unit; next. Free. Element++; } Monitor. Exit(this); Must remember to explicitly release the lock Critical code
Demonstration 3 Using Monitors
Synchronization Choosing a Synchronization Method u Things to consider Ø Ø Ø Complexity Ability to split locks Specialized mechanisms, e. g. Reader. Writer. Lock
Agenda u u Multi-threading in. NET Synchronization Inter-thread Communication Improving Performance with Thread Pools
Inter-thread Communication When Threads Need to Talk u u Waiting for a shared resource to become free Notification of the arrival of some work Ø u Waiting for clients Ø u Producer and consumer model Network server model Generally important application events Ø For example: A long-running task has finished
Inter-thread Communication Thread lifecycle u u Waiting and ready queues States Running Suspended Unstarted Aborted Wait. Sleep. Join
Inter-thread Communication Simple communication with Wait/Pulse u Monitor. Wait() Ø Ø Ø u Monitor. Pulse() and Monitor. Pulse. All() Ø u Choice between them Can only call them when you have the lock Ø u Supply object to wait on Enter Wait. Sleep. Join Timed waits Wait() call gives up the lock until it resumes What happens when Pulse() is called before Wait()?
Inter-thread Communication Notification Alternatives u u Pausing/sleeping Events (incl. Manual. Reset. Event) Timers (server and windows) Interlocked increment and decrement
Inter-thread Communication Wait/Pulse in Sample Application Producer object and thread 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 u u Finding threads Thread information Ø Ø u Thread. State Is. Alive Joining another thread Ø Thread. Join()
Inter-thread Communication Terminating threads u Stopping a thread Ø Ø u Abort and inconsistent state Ø u Interrupt + Interrupted. Exception Abort As locks are released, who will clean up objects? Alternative approaches using flags Ø Use join() to check if thread has exited yet
Agenda u u Multi-threading in. NET Synchronization Inter-thread communication Improving Performance with Thread Pools
Thread Pools What are thread pools? u u Set of "worker" threads Thread controller assigns them to tasks Submit task to thread controller Number of threads in pool can grow or shrink Ø Ø u 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
Thread Pools. NET thread pools u u 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 Queuing work items u Thread. Pool class Ø u Work item consists of Ø Ø u Queue. User. Work. Item static method Method to run is defined by Wait. Callback delegate (public void (object state)) State to be passed as object Still pass other state by setting variables on the method's object
Thread Pools Coding to work with thread pools u u 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
Thread Pools Delaying execution u u u Timed execution of delegates Useful for timer events etc. Use Register. Wait. For. Single. Object
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 u u . NET has powerful, cross-language threading capabilities. NET makes it easy to use multiple threads Ø u Benefits of multi-threading include Ø Ø Ø u Comprehensive library of classes and delegates Simpler code Faster execution Improved user perception of performance Developer must think more about thread interaction
Thank You!
- Slides: 39