Overview of Threading with the.NET Framework Wallace B. McClure Scalable Development, Inc....
-
Upload
gabriel-glenn -
Category
Documents
-
view
219 -
download
0
Transcript of Overview of Threading with the.NET Framework Wallace B. McClure Scalable Development, Inc....
![Page 1: Overview of Threading with the.NET Framework Wallace B. McClure Scalable Development, Inc. Scalable Development, Inc. Building systems today that perform.](https://reader035.fdocuments.net/reader035/viewer/2022062518/56649e915503460f94b96082/html5/thumbnails/1.jpg)
Overview of Threadingwith the .NET Framework
Wallace B. McClure
Scalable Development, Inc.
Scalable Development, Inc.Building systems today that perform tomorrow.
![Page 2: Overview of Threading with the.NET Framework Wallace B. McClure Scalable Development, Inc. Scalable Development, Inc. Building systems today that perform.](https://reader035.fdocuments.net/reader035/viewer/2022062518/56649e915503460f94b96082/html5/thumbnails/2.jpg)
.NET Resources
ASP.NET – www.asp.netAspAdvice – www.aspadvice.comWindows Forms –
www.windowsforms.netArchitecture –
msdn.microsoft.com/architecture.NET News – www.dotnetwire.com
![Page 3: Overview of Threading with the.NET Framework Wallace B. McClure Scalable Development, Inc. Scalable Development, Inc. Building systems today that perform.](https://reader035.fdocuments.net/reader035/viewer/2022062518/56649e915503460f94b96082/html5/thumbnails/3.jpg)
What is MultiTasking /Multiprocessing?
Ability of multiple applications to work at the same time.
Cooperative multitasking. Windows 3.1 & Mac OS pre-X applications were responsible for passing control of processing to each other. One application can cause problems in the whole system by blocking other running apps.
Pre-emptive multitasking. Win32 & Mac OSX applications have processing capabilities granted to them by the OS. One application is less like to cause a problem. TimeSlice is given to the app.
![Page 4: Overview of Threading with the.NET Framework Wallace B. McClure Scalable Development, Inc. Scalable Development, Inc. Building systems today that perform.](https://reader035.fdocuments.net/reader035/viewer/2022062518/56649e915503460f94b96082/html5/thumbnails/4.jpg)
What is Threading?
Threading is the ability of a single application (process) to perform multiple units of work that are controlled by the parent application.
![Page 5: Overview of Threading with the.NET Framework Wallace B. McClure Scalable Development, Inc. Scalable Development, Inc. Building systems today that perform.](https://reader035.fdocuments.net/reader035/viewer/2022062518/56649e915503460f94b96082/html5/thumbnails/5.jpg)
Algorithms and Their Impact
Dividing an application into “threads” will not automatically make an application faster.
How an application runs will determine how well threading in an application will work.
Threading is not a magic “snake oil.”
![Page 6: Overview of Threading with the.NET Framework Wallace B. McClure Scalable Development, Inc. Scalable Development, Inc. Building systems today that perform.](https://reader035.fdocuments.net/reader035/viewer/2022062518/56649e915503460f94b96082/html5/thumbnails/6.jpg)
Serial Processes
Serial. One step within the algorithm must be completed before the next step.
![Page 7: Overview of Threading with the.NET Framework Wallace B. McClure Scalable Development, Inc. Scalable Development, Inc. Building systems today that perform.](https://reader035.fdocuments.net/reader035/viewer/2022062518/56649e915503460f94b96082/html5/thumbnails/7.jpg)
Parallel Processes Parallel. Steps within the algorithm are not
dependent on other steps to be completed first.
![Page 8: Overview of Threading with the.NET Framework Wallace B. McClure Scalable Development, Inc. Scalable Development, Inc. Building systems today that perform.](https://reader035.fdocuments.net/reader035/viewer/2022062518/56649e915503460f94b96082/html5/thumbnails/8.jpg)
Algorithms
Algorithms are neither Serial or Parallel but some combination.
Assume an app that is 50% parallel. Only improve by up to 25% by adding an
additional thread. Never double the speed.
Must understand your algorithm. Where are your bottlenecks and your
opportunities for improvement?
![Page 9: Overview of Threading with the.NET Framework Wallace B. McClure Scalable Development, Inc. Scalable Development, Inc. Building systems today that perform.](https://reader035.fdocuments.net/reader035/viewer/2022062518/56649e915503460f94b96082/html5/thumbnails/9.jpg)
Types of Suitable Apps /Algorithms
Long running algorithms.Highly Parallel algorithms (FFT is the
best parallel algorithm I know of).Responsive User Interface.Async operations.Windows Services (HTTP,
Database).
![Page 10: Overview of Threading with the.NET Framework Wallace B. McClure Scalable Development, Inc. Scalable Development, Inc. Building systems today that perform.](https://reader035.fdocuments.net/reader035/viewer/2022062518/56649e915503460f94b96082/html5/thumbnails/10.jpg)
Types of Threads
Managed Threads / Regular Threads.System.Threading.Thread() Class.
ThreadPool.System.Threading.ThreadPool() Class.
![Page 11: Overview of Threading with the.NET Framework Wallace B. McClure Scalable Development, Inc. Scalable Development, Inc. Building systems today that perform.](https://reader035.fdocuments.net/reader035/viewer/2022062518/56649e915503460f94b96082/html5/thumbnails/11.jpg)
Create a Thread
New System.Threading.Thread
(AddressOf(method)).Some Methods:
Start(). Start processing on the Thread.Sleep(). Wait X milliseconds.Join(). Wait on a thread to finish.Resume(). Start processing again.Abort(). Quit the thread.
![Page 12: Overview of Threading with the.NET Framework Wallace B. McClure Scalable Development, Inc. Scalable Development, Inc. Building systems today that perform.](https://reader035.fdocuments.net/reader035/viewer/2022062518/56649e915503460f94b96082/html5/thumbnails/12.jpg)
Priority
Normal is the default.
Threading priority tells the OS how much relative time to a thread.
![Page 13: Overview of Threading with the.NET Framework Wallace B. McClure Scalable Development, Inc. Scalable Development, Inc. Building systems today that perform.](https://reader035.fdocuments.net/reader035/viewer/2022062518/56649e915503460f94b96082/html5/thumbnails/13.jpg)
Performance Monitor Integration
.NET CLR LocksAndThreads
Track the number of Threads.
![Page 14: Overview of Threading with the.NET Framework Wallace B. McClure Scalable Development, Inc. Scalable Development, Inc. Building systems today that perform.](https://reader035.fdocuments.net/reader035/viewer/2022062518/56649e915503460f94b96082/html5/thumbnails/14.jpg)
Uniprocessor Threading Hiccup
On a uniprocessor, the thread does not get any processor time until the main thread yields. Call Thread.Sleep(0) for a thread to immediately start after calling the Thread.Start().
![Page 15: Overview of Threading with the.NET Framework Wallace B. McClure Scalable Development, Inc. Scalable Development, Inc. Building systems today that perform.](https://reader035.fdocuments.net/reader035/viewer/2022062518/56649e915503460f94b96082/html5/thumbnails/15.jpg)
Example #1
Create a thread and send a message to the UI.
![Page 16: Overview of Threading with the.NET Framework Wallace B. McClure Scalable Development, Inc. Scalable Development, Inc. Building systems today that perform.](https://reader035.fdocuments.net/reader035/viewer/2022062518/56649e915503460f94b96082/html5/thumbnails/16.jpg)
Passing Parameters
In: Set a property of a class.
In: Use the constructor to set initial value.
Out: Raise an event and pass a param to that event.
![Page 17: Overview of Threading with the.NET Framework Wallace B. McClure Scalable Development, Inc. Scalable Development, Inc. Building systems today that perform.](https://reader035.fdocuments.net/reader035/viewer/2022062518/56649e915503460f94b96082/html5/thumbnails/17.jpg)
Example #2
Instantiate a class.Set a property of the class.Thread runs.Raises event.Event is processed by the calling
thread.
![Page 18: Overview of Threading with the.NET Framework Wallace B. McClure Scalable Development, Inc. Scalable Development, Inc. Building systems today that perform.](https://reader035.fdocuments.net/reader035/viewer/2022062518/56649e915503460f94b96082/html5/thumbnails/18.jpg)
Problem(s) Debugging. Management overhead. Deadlocks. Race Conditions. Order of Execution. What happens when Threads must access
common variables? Exclusively lock access to any common
objects.
![Page 19: Overview of Threading with the.NET Framework Wallace B. McClure Scalable Development, Inc. Scalable Development, Inc. Building systems today that perform.](https://reader035.fdocuments.net/reader035/viewer/2022062518/56649e915503460f94b96082/html5/thumbnails/19.jpg)
Locking Access to Objects (Synchronization)
System.Threading.Monitor() Class.
Methods:Enter(obj).Exit(obj).Wait, Pulse, PulseAll.
![Page 20: Overview of Threading with the.NET Framework Wallace B. McClure Scalable Development, Inc. Scalable Development, Inc. Building systems today that perform.](https://reader035.fdocuments.net/reader035/viewer/2022062518/56649e915503460f94b96082/html5/thumbnails/20.jpg)
Other Ways to Lock Objects
Synchronization Attribute. Lock (C#) / SyncLock (VB). ReaderWriterLock() Class. Designed for reads
with a small number of writes. Mutex. Works for threads and processes.
![Page 21: Overview of Threading with the.NET Framework Wallace B. McClure Scalable Development, Inc. Scalable Development, Inc. Building systems today that perform.](https://reader035.fdocuments.net/reader035/viewer/2022062518/56649e915503460f94b96082/html5/thumbnails/21.jpg)
Example #3
Use the Monitor object to lock access to an object.
![Page 22: Overview of Threading with the.NET Framework Wallace B. McClure Scalable Development, Inc. Scalable Development, Inc. Building systems today that perform.](https://reader035.fdocuments.net/reader035/viewer/2022062518/56649e915503460f94b96082/html5/thumbnails/22.jpg)
Notes on the Monitor Object
Only works on Reference types. Value types are not exclusively locked.
The vbc and csc compilers put a try/catch/finally so that in the case of an error, the appropriate Exit() method is called.
![Page 23: Overview of Threading with the.NET Framework Wallace B. McClure Scalable Development, Inc. Scalable Development, Inc. Building systems today that perform.](https://reader035.fdocuments.net/reader035/viewer/2022062518/56649e915503460f94b96082/html5/thumbnails/23.jpg)
Managed ThreadRecommendations
Don't use Thread.Abort to terminate threads. Thread state is unknown.
Don't use Thread.Suspend and .Resume to synchronize threads. Use the appropriate objects.
Monitor.Enter and Monitor.Exit are both called. Threads are great for tasks involving different
resources.
![Page 24: Overview of Threading with the.NET Framework Wallace B. McClure Scalable Development, Inc. Scalable Development, Inc. Building systems today that perform.](https://reader035.fdocuments.net/reader035/viewer/2022062518/56649e915503460f94b96082/html5/thumbnails/24.jpg)
Overview of the Thread Pool
![Page 25: Overview of Threading with the.NET Framework Wallace B. McClure Scalable Development, Inc. Scalable Development, Inc. Building systems today that perform.](https://reader035.fdocuments.net/reader035/viewer/2022062518/56649e915503460f94b96082/html5/thumbnails/25.jpg)
ThreadPool
Pool of threads. Managed by the CLR. Per Process. Built into the CLR. # of Threads dependent on CPU usage. 25
threads per CPU default. Article on MSDN with ThreadPool guidelines.
![Page 26: Overview of Threading with the.NET Framework Wallace B. McClure Scalable Development, Inc. Scalable Development, Inc. Building systems today that perform.](https://reader035.fdocuments.net/reader035/viewer/2022062518/56649e915503460f94b96082/html5/thumbnails/26.jpg)
Of Interest
WaitCallback.QueueUserWorkItem.Monitor the pool.
GetAvailableThreads(out int Wthrds, out int iCompPortThrds).
GetMaxThreads(out int Wthrds,out int iCompPortThrds).
![Page 27: Overview of Threading with the.NET Framework Wallace B. McClure Scalable Development, Inc. Scalable Development, Inc. Building systems today that perform.](https://reader035.fdocuments.net/reader035/viewer/2022062518/56649e915503460f94b96082/html5/thumbnails/27.jpg)
ThreadPool Worries
Don't do operations that are not guaranteed to complete.
Remember that the ThreadPool has a maximum number of threads.
![Page 28: Overview of Threading with the.NET Framework Wallace B. McClure Scalable Development, Inc. Scalable Development, Inc. Building systems today that perform.](https://reader035.fdocuments.net/reader035/viewer/2022062518/56649e915503460f94b96082/html5/thumbnails/28.jpg)
Personal Experience:Overview of a Windows Service
Goal: Pull data from multiple sources.Multi-threaded Windows Service.Thread for each data source.Little contention for resources.Error processing is complicated.
![Page 29: Overview of Threading with the.NET Framework Wallace B. McClure Scalable Development, Inc. Scalable Development, Inc. Building systems today that perform.](https://reader035.fdocuments.net/reader035/viewer/2022062518/56649e915503460f94b96082/html5/thumbnails/29.jpg)
Things to Look at / Last Thoughts
Windows Services.EventLog.Weak References.Performance Monitor Integration.Nothing wrong with Interop, if it will
work for you.
![Page 30: Overview of Threading with the.NET Framework Wallace B. McClure Scalable Development, Inc. Scalable Development, Inc. Building systems today that perform.](https://reader035.fdocuments.net/reader035/viewer/2022062518/56649e915503460f94b96082/html5/thumbnails/30.jpg)
Questions?
Scalable Development, Inc.Consulting & Development Services.http://www.scalabledevelopment.com865-693-3004.wallym@scalabledevelopment.com
END
Scalable Development, Inc.Building systems today that perform tomorrow.