Chapter 14 Multithreading Yingcai Xiao. Multithreading is a mechanism for performing two or more tasks concurrently.  In the managed world of the common.

Slides:



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

Threads, SMP, and Microkernels Chapter 4. Process Resource ownership - process is allocated a virtual address space to hold the process image Scheduling/execution-
Threads in C# Threads in C#.
Lecture 4 Thread Concepts. Thread Definition A thread is a lightweight process (LWP) which accesses a shared address space provided by by the parent process.
Multithreading The objectives of this chapter are:
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
Threads 1 CS502 Spring 2006 Threads CS-502 Spring 2006.
1 Threads Chapter 4 Reading: 4.1,4.4, Process Characteristics l Unit of resource ownership - process is allocated: n a virtual address space to.
Operating Systems Lecture 09: Threads (Chapter 4)
1 Lecture 4: Threads Operating System Fall Contents Overview: Processes & Threads Benefits of Threads Thread State and Operations User Thread.
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.
Multithreading in Java Project of COCS 513 By Wei Li December, 2000.
Lecture 2 Foundations and Definitions Processes/Threads.
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.
 2004 Deitel & Associates, Inc. All rights reserved. 1 Chapter 4 – Thread Concepts Outline 4.1 Introduction 4.2Definition of Thread 4.3Motivation for.
Threads, SMP, and Microkernels Chapter 4. Process Resource ownership - process is allocated a virtual address space to hold the process image Scheduling/execution-
Chapter 2 Processes and Threads Introduction 2.2 Processes A Process is the execution of a Program More specifically… – A process is a program.
1 Threads, SMP, and Microkernels Chapter Multithreading Operating system supports multiple threads of execution within a single process MS-DOS.
Upcoming Presentations ILM Professional Service – Proprietary and Confidential ( DateTimeTopicPresenter March PM Distributed.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
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.
Operating Systems: Internals and Design Principles
Lecture 6: Monitors & Semaphores. Monitor Contains data and procedures needed to allocate shared resources Accessible only within the monitor No way for.
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.
Lecture 3 Concurrency and Thread Synchronization     Mutual Exclusion         Dekker's Algorithm         Lamport's Bakery Algorithm.
Multithreading & Synchronized Algoritma Pemrograman 3 Sistem Komputer – S1 Universitas Gunadarma 1.
Multithreading. Multitasking The multitasking is the ability of single processor to perform more than one operation at the same time Once systems allowed.
1 Threads in Java Jingdi Wang. 2 Introduction A thread is a single sequence of execution within a program Multithreading involves multiple threads of.
CIS NET Applications1 Chapter 8 – Multithreading and Concurrency Management.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
V 1.0 OE-NIK HP 1 Advanced Programming Fundamentals of parallel execution Processes Threads.
Demonstrating Multithreading Concepts. Overvie w Multithreading or free-threading is the ability of an operating system to concurrently run programs that.
Advanced Operating Systems CS6025 Spring 2016 Processes and Threads (Chapter 2)
Java Thread Programming
Multithreading The objectives of this chapter are:
Chapter 4 – Thread Concepts
Segments Introduction: slides minutes
Chapter 3: Windows7 Part 5.
A brief intro to: Parallelism, Threads, and Concurrency
Threading Lecture 11 M. Ipalakova.
Multithreading Lec 23.
PROCESS MANAGEMENT IN MACH
Background on the need for Synchronization
Jim Fawcett CSE681 – Software Modeling & Analysis Fall 2002
Thread Fundamentals Header Advanced .NET Threading, Part 1
Chapter 4 – Thread Concepts
Lecture 21 Concurrency Introduction
Jim Fawcett CSE681 – Software Modeling and Analysis Fall 2005
Chapter 3: Windows7 Part 5.
Threads, SMP, and Microkernels
Chapter 15, Exploring the Digital Domain
Multithreading.
Threading using C# and .Net
Multithreaded Programming
Android Topics UI Thread and Limited processing resources
Lecture 4- Threads, SMP, and Microkernels
Threads and Concurrency
Threads Chapter 4.
Multithread Programming
Multithreading in java.
Foundations and Definitions
Visual Programming COMP-315
Multithreading The objectives of this chapter are:
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Chapter 14 Multithreading Yingcai Xiao

Multithreading is a mechanism for performing two or more tasks concurrently.  In the managed world of the common language runtime, the fundamental unit of execution is the thread.  A managed application begins its life as a single thread but can spawn additional threads.  Threads running concurrently share the CPU (or CPUs) by using scheduling algorithms provided by the system.  To an observer, it appears as if all the threads are running at once. In reality, they simply share processor time.. Multithreading

 A single-threaded application uses just one processor at a time. A multithreaded application can have different threads running on different processors.  Windows NT kernel (NT, 2000, XP, Vista, 7, 8) supports multiple processors using a strategy called symmetric multiprocessing (SMP). Versions that derive from Windows 95 (95, 98, Millennium) do not. .NET Framework’s threading API: to start, stop, manipulate, prioritize and synchronize threads.

Example using System; using System.Threading; class MyApp { static void Main () { for (int i=0; i<10; i++) { Thread thread = new Thread( new ThreadStart(ThreadFunc)); thread.Name = "My thread # " + i; thread.Start (); }

Example static void ThreadFunc () { String name = Thread.CurrentThread.Name; for (int i=0; i<10; i++) { for( int j=0; j< ; j++); //work System.Console.WriteLine (name + " running at "+ DateTime.Now); }

Multithreading  Starting a thread: 1.Create a thread object. 2.Setup a function for the thread to run. 3.Start the thread. Thread thread = new Thread (new ThreadStart (ThreadFunc)); thread.Start (); 4. Start simply makes the thread eligible to be allotted CPU time. The system decides when the thread begins running and how often it’s accorded processor time. 5. When a thread function returns, the corresponding thread ends.  Alive and Suspend if (thread.IsAlive) { thread.Suspend (); }

Foreground Threads vs. Background Threads  An application doesn’t end until all of its foreground threads have ended.  It can, however, end with background threads running.  The IsBackground property defaults to false, which means that threads are foreground threads by default.  Up to the application to determine a thread is foreground or background.  Example, application ends as soon as it’s started because it changes the auxiliary threads from foreground threads to background threads:

Foreground Threads vs. Background Threads using System; using System.Threading; class MyApp { static void Main () { for (int i=0; i<10; i++) { Thread thread = new Thread (new ThreadStart (ThreadFunc)); thread.IsBackground = true; thread.Start (); } static void ThreadFunc () { DateTime start = DateTime.Now; while ((DateTime.Now - start).Seconds < 5); }

Threads Priorities  Once a thread is started, the amount of processor time it’s allotted is determined by the thread scheduler.  Thread.Priority can influence how much or how little CPU time a thread receives relative to other threads in the same process. Statistical not precise.  Setting a thread to higher priority impacts not only other threads in the same application but also slows down other applications too.  Thread Priorities ThreadPriority.Highest ThreadPriority.AboveNormal ThreadPriority.Normal (the default) ThreadPriority.BelowNormal ThreadPriority.Lowest thread.Priority = ThreadPriority.AboveNormal;

Multithreading in Action  Many applications intrinsically have two threads of execution: (1) Computation thread carries on the main computational task. It runs the computational loop. (2) User interface thread waits for user interaction. It may abort the computational thread if the user elect to do so. It runs the event loop.

Multithreading in Action  The Sieve and MultiSieve are examples of such applications: a computational component for searching prime numbers and a GUI for user interactions.  The Sieve example is not multi threaded, the user can’t interact with the application whence the computation starts. Examples\C14\Sieve  The MultiSieve example is multi threaded, the user can cancel the computation after starting it and the application can exit in the middle of the computation. Examples\C14\MultiSieve

Thread Synchronization  The hard part of designing a multithreaded program is figuring out where concurrently running threads might clash and using thread synchronization logic to prevent clashes (e.g., reading while writing to the same variable) from occurring.  NET Framework provides the synchronization primitives to support applications’ synchronization logic. AutoResetEvent: Blocks a thread until another thread sets the event Interlocked:Enables simple operations such as incrementing and decrementing integers to be performed in a thread-safe manner ManualResetEvent:Blocks one or more threads until another thread sets the event Monitor:Prevents more than one thread at a time from accessing a resource Mutex:Prevents more than one thread at a time from accessing a resource and has the ability to span application and process boundaries ReaderWriterLock:Enables multiple threads to read a resource simultaneously but prevents overlapping reads and writes as well as overlapping writes

Thread Pooling  Instead of launching threads yourself, you pass requests to a thread pool manager. The thread pool manager maintains a pool of threads that’s sized as needed to satisfy incoming requests.  NET Framework provides the ThreadPool class as an API to the managed thread pooling.  You must never terminate a pooled thread. The thread pool manager creates pooled threads, and it terminates them, too.  You can use Thread’s IsThreadPoolThread property to determine whether a thread is a pooled thread.

Thread Pooling class MyApp { static int count = 0; static void Main () { WaitCallback callback = new WaitCallback (ProcessRequest); // start the threads from the pool to run the callbacks ThreadPool.QueueUserWorkItem (callback); ThreadPool.QueueUserWorkItem (callback); Thread.Sleep (5000); // Give the requests a chance to execute }

Thread Pooling static void ProcessRequest (object state) { int n = Interlocked.Increment (ref count); // lock then increment count Console.WriteLine (n); }