Overview of Threading with the.NET Framework  Wallace B. McClure  Scalable Development, Inc. Scalable Development, Inc. Building systems today that perform.

Slides:



Advertisements
Similar presentations
Multiple Processor Systems
Advertisements

How to Build Multi- threaded Applications in.NET Mazen S. Alzogbi Technology Specialist Microsoft Corporation.
Concurrent Programming Abstraction & Java Threads
Chapter 6: Process Synchronization
1 CSC321 §2 Concurrent Programming Abstraction & Java Threads Section 2 Concurrent Programming Abstraction & Java Threads.
Chapter 14 Multithreading Yingcai Xiao. Multithreading is a mechanism for performing two or more tasks concurrently.  In the managed world of the common.
IT Systems Multiprocessor System EN230-1 Justin Champion C208 –
Threads in C# Threads in C#.
Day 10 Threads. Threads and Processes  Process is seen as two entities Unit of resource allocation (process or task) Unit of dispatch or scheduling (thread.
Multithreading The objectives of this chapter are:
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L19 (Chapter 24) Multithreading.
490dp Synchronous vs. Asynchronous Invocation Robert Grimm.
UCoM Software Architecture Universal Communicator Research UCoM Programming Model The Problem  Multi-threaded code is difficult to write.
Server Architecture Models Operating Systems Hebrew University Spring 2004.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Multithreading in Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Programming Handheld and Mobile devices 1 Programming of Handheld and Mobile Devices Lecture 19 Microsoft’s Approach 1 –.NET Mobile Framework part 2 Rob.
 Wallace B. McClure  Scalable Development, Inc. Scalable Development, Inc. Building systems today that perform tomorrow. Designing & Building Windows.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
Threads. Overview Problem Multiple tasks for computer Draw & display images on screen Check keyboard & mouse input Send & receive data on network Read.
Dr. R R DOCSIT, Dr BAMU. Basic Java : Multi Threading 2 Objectives of This Session State what is Multithreading. Describe the life cycle of Thread.
111 © 2002, Cisco Systems, Inc. All rights reserved.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Introduction to Concurrency.
Java Threads. What is a Thread? A thread can be loosely defined as a separate stream of execution that takes place simultaneously with and independently.
C# I 1 CSC 298 Threads. C# I 2 Introducing Threads  A thread is a flow of control within a program  A piece of code that runs on its own. The execution.
4.1 Introduction to Threads Overview Multithreading Models Thread Libraries Threading Issues Operating System Examples Windows XP Threads Linux Threads.
CS 346 – Chapter 4 Threads –How they differ from processes –Definition, purpose Threads of the same process share: code, data, open files –Types –Support.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
Upcoming Presentations ILM Professional Service – Proprietary and Confidential ( DateTimeTopicPresenter March PM Distributed.
15.1 Threads and Multi- threading Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
SurfaceView.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Concurrency & Dynamic Programming.
Multi-Threading in Java
Copyright © Curt Hill Concurrent Execution An Overview for Database.
Multithreading. Multithreaded Programming A multithreaded program contains two or more parts that can run concurrently. Each part of such a program is.
Thread basics. A computer process Every time a program is executed a process is created It is managed via a data structure that keeps all things memory.
4.1 Introduction to Threads Overview Multithreading Models Thread Libraries Threading Issues Operating System Examples Windows XP Threads Linux Threads.
Mac OS X Cate Wardell Tim Miller Tina Han Kenan Shifflett Zach Debord.
L6: Threads “the future is parallel” COMP206, Geoff Holmes and Bernhard Pfahringer.
CIS NET Applications1 Chapter 8 – Multithreading and Concurrency Management.
Concurrent Programming in Java Based on Notes by J. Johns (based on Java in a Nutshell, Learning Java) Also Java Tutorial, Concurrent Programming in Java.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
CS203 Programming with Data Structures Introduction to Threads and Synchronization California State University, Los Angeles.
Lecture 5. Example for periority The average waiting time : = 41/5= 8.2.
Threads Some of these slides were originally made by Dr. Roger deBry. They include text, figures, and information from this class’s textbook, Operating.
Segments Introduction: slides minutes
Multithreading / Concurrency
Threading Lecture 11 M. Ipalakova.
Multi Threading.
Background on the need for Synchronization
Advanced Topics in Concurrency and Reactive Programming: Asynchronous Programming Majeed Kassis.
G.Anuradha Reference: William Stallings
Jim Fawcett CSE681 – Software Modeling & Analysis Fall 2002
Learning About Operating Systems
Day 12 Threads.
Async or Parallel? No they aren’t the same thing!
Other Important Synchronization Primitives
Writing Highly Available .Net Framework Applications
Jim Fawcett CSE681 – Software Modeling and Analysis Fall 2005
Modified by H. Schulzrinne 02/15/10 Chapter 4: Threads.
Multithreading.
Multithreaded Programming
Threads and Concurrency
Multithread Programming
Concurrency: Mutual Exclusion and Process Synchronization
Windows Development Dynadata Copyright, 2014 © DynaData S.A. 1/49.
Multithreading in java.
Outline Chapter 3: Processes Chapter 4: Threads So far - Next -
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Overview of Threading with the.NET Framework  Wallace B. McClure  Scalable Development, Inc. Scalable Development, Inc. Building systems today that perform tomorrow.

.NET Resources  ASP.NET –  AspAdvice –  Windows Forms –  Architecture – msdn.microsoft.com/architecture .NET News –

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.

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.

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.”

Serial Processes  Serial. One step within the algorithm must be completed before the next step.

Parallel Processes  Parallel. Steps within the algorithm are not dependent on other steps to be completed first.

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?

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).

Types of Threads  Managed Threads / Regular Threads.  System.Threading.Thread() Class.  ThreadPool.  System.Threading.ThreadPool() Class.

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.

Priority  Normal is the default.  Threading priority tells the OS how much relative time to a thread.

Performance Monitor Integration .NET CLR LocksAndThreads  Track the number of Threads.

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().

Example #1  Create a thread and send a message to the UI.

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.

Example #2  Instantiate a class.  Set a property of the class.  Thread runs.  Raises event.  Event is processed by the calling thread.

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.

Locking Access to Objects (Synchronization)  System.Threading.Monitor() Class.  Methods:  Enter(obj).  Exit(obj).  Wait, Pulse, PulseAll.

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.

Example #3  Use the Monitor object to lock access to an object.

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.

Managed Thread Recommendations  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.

Overview of the Thread Pool

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.

Of Interest  WaitCallback.  QueueUserWorkItem.  Monitor the pool.  GetAvailableThreads(out int Wthrds, out int iCompPortThrds).  GetMaxThreads(out int Wthrds, out int iCompPortThrds).

ThreadPool Worries  Don't do operations that are not guaranteed to complete.  Remember that the ThreadPool has a maximum number of threads.

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.

Things to Look at / Last Thoughts  Windows Services.  EventLog.  Weak References.  Performance Monitor Integration.  Nothing wrong with Interop, if it will work for you.

Questions?  Scalable Development, Inc.  Consulting & Development Services.    END Scalable Development, Inc. Building systems today that perform tomorrow.