Thread Fundamentals Header Advanced .NET Threading, Part 1

Slides:



Advertisements
Similar presentations
Processes and Threads Chapter 3 and 4 Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College,
Advertisements

Why Concurrency? Allows multiple applications to run at the same time  Analogy: juggling.
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#.
Chapter 4 Threads, SMP, and Microkernels Patricia Roy Manatee Community College, Venice, FL ©2008, Prentice Hall Operating Systems: Internals and Design.
ECE 526 – Network Processing Systems Design Software-based Protocol Processing Chapter 7: D. E. Comer.
OS Fall ’ 02 Introduction Operating Systems Fall 2002.
Avishai Wool lecture Priority Scheduling Idea: Jobs are assigned priorities. Always, the job with the highest priority runs. Note: All scheduling.
Introduction to Operating Systems – Windows process and thread management In this lecture we will cover Threads and processes in Windows Thread priority.
OS Spring’03 Introduction Operating Systems Spring 2003.
Real-Time Kernels and Operating Systems. Operating System: Software that coordinates multiple tasks in processor, including peripheral interfacing Types.
1 Chapter 4 Threads Threads: Resource ownership and execution.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
A. Frank - P. Weisberg Operating Systems Introduction to Tasks/Threads.
OS Spring’04 Introduction Operating Systems Spring 2004.
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.
Threads Chapter 4. Modern Process & Thread –Process is an infrastructure in which execution takes place  (address space + resources) –Thread is a program.
Basics of Operating Systems March 4, 2001 Adapted from Operating Systems Lecture Notes, Copyright 1997 Martin C. Rinard.
Chapter 3 Operating Systems Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Chapter 8 Windows Outline Programming Windows 2000 System structure Processes and threads in Windows 2000 Memory management The Windows 2000 file.
1 Lecture 4: Threads Operating System Fall Contents Overview: Processes & Threads Benefits of Threads Thread State and Operations User Thread.
Concurrency: Threads, Address Spaces, and Processes Andy Wang Operating Systems COP 4610 / CGS 5765.
Processes and Threads Processes have two characteristics: – Resource ownership - process includes a virtual address space to hold the process image – Scheduling/execution.
Chapter 101 Multiprocessor and Real- Time Scheduling Chapter 10.
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.
Threads, SMP, and Microkernels Chapter 4. Process Resource ownership - process is allocated a virtual address space to hold the process image Scheduling/execution-
Threads G.Anuradha (Reference : William Stallings)
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.
1 Lecture 4: Threads Advanced Operating System Fall 2010.
Concurrency & Context Switching Process Control Block What's in it and why? How is it used? Who sees it? 5 State Process Model State Labels. Causes of.
Multithreaded Programing. Outline Overview of threads Threads Multithreaded Models  Many-to-One  One-to-One  Many-to-Many Thread Libraries  Pthread.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
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.
Managing Processors Jeff Chase Duke University. The story so far: protected CPU mode user mode kernel mode kernel “top half” kernel “bottom half” (interrupt.
1 Threads, SMP, and Microkernels Chapter 4. 2 Process Resource ownership - process includes a virtual address space to hold the process image Scheduling/execution-
CIS NET Applications1 Chapter 8 – Multithreading and Concurrency Management.
Chapter 4: Threads 羅習五. Chapter 4: Threads Motivation and Overview Multithreading Models Threading Issues Examples – Pthreads – Windows XP Threads – Linux.
Embedded Real-Time Systems Processing interrupts Lecturer Department University.
Threads, SMP, and Microkernels Chapter 4. Processes and Threads Operating systems use processes for two purposes - Resource allocation and resource ownership.
Advanced Operating Systems CS6025 Spring 2016 Processes and Threads (Chapter 2)
Introduction to Operating Systems Concepts
Processes and threads.
Process concept.
Process Management Process Concept Why only the global variables?
OPERATING SYSTEMS CS3502 Fall 2017
Multithreading .NET a practical perspective
Async or Parallel? No they aren’t the same thing!
Chapter 4: Multithreaded Programming
Chapter 4: Threads 羅習五.
Threads and Locks.
Operating System Concepts
Concurrency: Threads, Address Spaces, and Processes
Chapter 4: Threads.
Threads, SMP, and Microkernels
Concurrency: Threads, Address Spaces, and Processes
CS 143A Quiz 1 Solution.
Process & its States Lecture 5.
Mid Term review CSC345.
Lecture Topics: 11/1 General Operating System Concepts Processes
Lecture 4- Threads, SMP, and Microkernels
Threads and Concurrency
Threads Chapter 4.
Multithread Programming
Threads and Concurrency
Chapter 3: Processes.
CS149D Elements of Computer Science
Foundations and Definitions
Chapter-1 Computer is an advanced electronic device that takes raw data as an input from the user and processes it under the control of a set of instructions.
Concurrency: Threads, Address Spaces, and Processes
Presentation transcript:

Thread Fundamentals Header Advanced .NET Threading, Part 1 Microsoft Virtual Academy Header Advanced .NET Threading, Part 1 Thread Fundamentals Jeffrey Richter Produced by

Thread Fundamentals Early OSes didn’t support threads (there was just 1 thread) Problem: Long-running tasks affected all apps and the OS Solution: Windows supports 1+ threads/process for robustness Threads have space & time overhead Kernel object (contains thread’s properties & register set context) Context size in bytes: x86 = ~700, x64 = ~1240, ARM = ~350 User-mode data (Thread Environment Block) 4KB, exception-handling chain, TLS, GDI/OpenGL stuff Stacks: user-mode (1MB) & kernel-mode (12KB/24KB) DLL thread attach/detach notifications 1 CPU can only run 1 thread at a time After quantum, Windows context switches to another thread

Thread Fundamentals Every context switch requires that Windows Save registers from CPU to running thread’s kernel object Determine which thread to schedule next If thread owned by other process, switch address space Load registers from selected thread’s kernel object into CPU After the switch, CPU suffers cache misses repopulating its cache All of this is pure overhead and hurts performance But required for a robust OS Conclusion Avoid threads: incur time & memory overhead Use threads: responsiveness & scalability (on multi-CPU system) This class is about wrestling with this tension

Windows Schedules Threads All threads “appear” to run simultaneously Windows schedules a thread to each CPU Windows allows a thread to run for a time quantum When quantum expires, Windows performs a context switch A thread can voluntarily end its time quantum early Usually by waiting for input (keyboard, mouse, network, file) Windows won’t schedule a waiting thread In reality, most threads in the system are waiting for something

A look at the system overall & File Open Dialog

Programming .NET Framework Applications with C# Creating a Thread using System; using System.Threading; public static class Program { // 1. Define the thread method private static void ThreadMethod(Object state) { // Do whatever here... } public static void Main() { // 2. Construct Thread object Thread t = new Thread(ThreadMethod); // 3. Create a thread and let it run t.Start("Initialization data"); // Do whatever here... // 4. Optional: Wait for thread to die t.Join(); } } Copyright (c) 2000 - 2002 by Wintellect, LLC (Unauthorized duplication prohibited)

Thread overhead

Reasons to Create Threads Two reasons to create threads Isolate code from other code: Responsiveness/easier coding Concurrent execution: Scalability on multi-processor machines Benefits Keep the CPU busy User gets more features with no learning curve App reliability (no hangs, ability to cancel, responsive UI) Examples Indexing files for fast searching Defragmenting disk for better I/O speed Building your project when you stop typing Recalculating spreadsheet cells Spell/grammar checking documents

Temporarily Suspending a Thread Sleep suspends a thread for an approximate time Avoid this method as it blocks a thread wasting space (not time) Use a Timer instead to have a thread pool invoke a callback method periodically public class Thread { public static void Sleep(Int32 milliseconds); }

Thread Priorities Each thread has priority property Highest AboveNormal Normal BelowNormal Lowest Windows schedules threads to CPUs from Highest  Lowest until all CPUs are saturated It’s typically better to lower a thread’s priority Used for long-running compute-bound tasks Won’t adversely affect other processes You should avoid raising a thread’s priority Used for tasks that need to react immediately to something, execute for a very short time, and then block Can adversely affect other processes

Thread Priorities & System Responsiveness

WinRT Threading APIs & Windows Store Apps Programming .NET Framework Applications with C# WinRT Threading APIs & Windows Store Apps WinRT has a Windows.System.Threading namespace WinRT offers no API to create a dedicated thread or change a thread’s priority Instead, use ThreadPool.RunAsync(WorkItemHandler, WorkItemPriority) WorkItemPriority can be Low/Normal/High WinRT offers no API to put a thread to sleep Instead, use ThreadPoolTimer.Create(Periodic)Timer Background Windows Store apps have all threads suspended Conserves power Doesn’t interrupt foreground app improving responsiveness All I/O operations MUST be done asynchronously .NET APIs: System.Threading Absent: Thread, ThreadPool There: CancellationToken(Source), ReaderWriterLock(Slim) Sync objs: Auto/ManualResetEvent(Slim), Mutex, Semaphore(Slim), Volatile.Read/Write, Interlocked, Monitor, SpinLock Task: ContinueWith, Delay, Run, Wait(All/Any), WhenAll/Any, Yield, Parallel, PLINQ?? Copyright (c) 2000 - 2002 by Wintellect, LLC (Unauthorized duplication prohibited)