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

Slides:



Advertisements
Similar presentations
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Advertisements

Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
CS 5704 Fall 00 1 Monitors in Java Model and Examples.
Chapter 14 Multithreading Yingcai Xiao. Multithreading is a mechanism for performing two or more tasks concurrently.  In the managed world of the common.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
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.
Concurrency CS 510: Programming Languages David Walker.
Inter Process Communication:  It is an essential aspect of process management. By allowing processes to communicate with each other: 1.We can synchronize.
Definitions Process – An executing program
Fundamentals of Python: From First Programs Through Data Structures
(C) 2009 J. M. Garrido1 Object Oriented Simulation with Java.
Internet Software Development More stuff on Threads Paul Krause.
Experience with Processes and Monitors in Mesa
Nachos Phase 1 Code -Hints and Comments
Windows Programming Using C# Threading. 2 Contents Threading Thread class Interlocked class Monitor class Semaphore class Thread Pools.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
Overview of Threading with the.NET Framework  Wallace B. McClure  Scalable Development, Inc. Scalable Development, Inc. Building systems today that perform.
Threaded Applications Introducing additional threads in a Delphi application is easy.
Threads in Java. History  Process is a program in execution  Has stack/heap memory  Has a program counter  Multiuser operating systems since the sixties.
Multi-Threaded Application CSNB534 Asma Shakil. Overview Software applications employ a strategy called multi- threaded programming to split tasks into.
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.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Practical OOP using Java Basis Faqueer Tanvir Ahmed, 08 Jan 2012.
1 Web Based Programming Section 8 James King 12 August 2003.
1 Concurrency Architecture Types Tasks Synchronization –Semaphores –Monitors –Message Passing Concurrency in Ada Java Threads.
Concurrent Programming and Threads Threads Blocking a User Interface.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Threaded Programming in Python Adapted from Fundamentals of Python: From First Programs Through Data Structures CPE 401 / 601 Computer Network Systems.
1 Program5 Due Friday, March Prog4 user_thread... amount = … invoke delegate transact (amount)... mainThread... Total + = amount … user_thread...
Concurrency in Java Brad Vander Zanden. Processes and Threads Process: A self-contained execution environment Thread: Exists within a process and shares.
Upcoming Presentations ILM Professional Service – Proprietary and Confidential ( DateTimeTopicPresenter March PM Distributed.
Multithreading Chapter Introduction Consider ability of human body to ___________ –Breathing, heartbeat, chew gum, walk … In many situations we.
Java Thread and Memory Model
15.1 Threads and Multi- threading Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.
Developing Applications with the CSI Framework A General Guide.
Multi-Threading in Java
1 Prog4 user_thread... amount = … invoke delegate transact (amount)... mainThread... Total + = amount … user_thread... amount = … invoke delegate transact.
13-1 Chapter 13 Concurrency Topics Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads C# Threads.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
1 G53SRP: Java Concurrency Control (2) – wait/notify Chris Greenhalgh School of Computer Science.
CIS NET Applications1 Chapter 8 – Multithreading and Concurrency Management.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
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.
Distributed and Parallel Processing George Wells.
Java Thread Programming
Segments Introduction: slides minutes
Multithreading / Concurrency
Threaded Programming in Python
PROCESS MANAGEMENT IN MACH
Multi Threading.
Background on the need for Synchronization
Advanced Topics in Concurrency and Reactive Programming: Asynchronous Programming Majeed Kassis.
Jim Fawcett CSE681 – Software Modeling & Analysis Fall 2002
Other Important Synchronization Primitives
Jim Fawcett CSE681 – Software Modeling and Analysis Fall 2005
Threads Chate Patanothai.
Definitions Concurrent program – Program that executes multiple instructions at the same time. Process – An executing program (the running JVM for Java.
Multithreading Chapter 23.
Multithreading.
Threading using C# and .Net
Multithread Programming
Threaded Programming in Python
NETWORK PROGRAMMING CNET 441
Threads and Multithreading
CSE 451 Section 1/27/2000.
Lecture 19 Threads CSE /6/2019.
More concurrency issues
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

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

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 300

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

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

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

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

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

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

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# Completedqueue Workqueue Producer object and thread Consumer thread 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 Multi-threading in.NET Synchronization Inter-thread communication Improving Performance with Thread Pools

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

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

Demonstration 2 Adding Synchronization Adding Basic Synchronization

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

Demonstration 3 Using Monitors Using Monitors

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

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

Inter-thread Communication When 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

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

Inter-thread Communication Simple 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()?

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

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

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

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

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

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

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

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

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

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

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!