Lecture 8: Threads. Lisa (Ling) Liu

Similar documents
Processes and Threads. Industrial Programming. Processes and Threads (cont'd) Processes and Threads (cont'd)

Systems Programming & Scripting

C# Threading. Hans-Wolfgang Loidl School of Mathematical and Computer Sciences, Heriot-Watt University, Edinburgh

Concurrency - II. Recitation 3/24 Nisarg Raval Slides by Prof. Landon Cox and Vamsi Thummala

Performance Throughput Utilization of system resources

G52CON: Concepts of Concurrency

CSE 374 Programming Concepts & Tools

d i g i t a l An Introduction to Programming with Threads by Andrew D. Birrell January 6, 1989

Overview. CMSC 330: Organization of Programming Languages. Concurrency. Multiprocessors. Processes vs. Threads. Computation Abstractions

Resource management. Real-Time Systems. Resource management. Resource management

Advanced Programming C# Lecture 13. dr inż. Małgorzata Janik

Real-Time Systems. Lecture #4. Professor Jan Jonsson. Department of Computer Science and Engineering Chalmers University of Technology

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring 2004

Advanced Programming C# Lecture 12. dr inż. Małgorzata Janik

Concurrency: a crash course

A Sophomoric Introduction to Shared-Memory Parallelism and Concurrency Lecture 5 Programming with Locks and Critical Sections

SFDV3006 Concurrent Programming

Lecture 21: Concurrency in Other Environments (Part 2)

CS 571 Operating Systems. Midterm Review. Angelos Stavrou, George Mason University

Operating Systems Overview. Chapter 2

Concurrency, Thread. Dongkun Shin, SKKU

Operating Systems : Overview

Last Class: CPU Scheduling! Adjusting Priorities in MLFQ!

Multiple processes can run in parallel on a single computer and multiple threads can run in parallel in a single process.

CSL373: Lecture 5 Deadlocks (no process runnable) + Scheduling (> 1 process runnable)

Java Threads. Written by John Bell for CS 342, Spring 2018

Deadlock. Concurrency: Deadlock and Starvation. Reusable Resources

Concurrency in Java Prof. Stephen A. Edwards

Operating Systems: William Stallings. Starvation. Patricia Roy Manatee Community College, Venice, FL 2008, Prentice Hall

MultiThreading 07/01/2013. Session objectives. Introduction. Introduction. Advanced Java Programming Course

Midterm 1, CSE 451, Winter 2001 (Prof. Steve Gribble)

Advanced Java Programming Course. MultiThreading. By Võ Văn Hải Faculty of Information Technologies Industrial University of Ho Chi Minh City

Process Management And Synchronization

Chapter 32 Multithreading and Parallel Programming

Chapters 5 and 6 Concurrency

Concurrent Programming using Threads

wait with priority An enhanced version of the wait operation accepts an optional priority argument:

Need for synchronization: If threads comprise parts of our software systems, then they must communicate.

Principles of Software Construction: Concurrency, Part 2

Concurrency, Mutual Exclusion and Synchronization C H A P T E R 5

CSE332: Data Abstractions Lecture 23: Programming with Locks and Critical Sections. Tyler Robison Summer 2010

Concurrency: Deadlock and Starvation. Chapter 6

Lecture 10: Multi-Object Synchronization

Operating Systems ECE344

Introduction to Locks. Intrinsic Locks

Animation Part 2: MoveableShape interface & Multithreading

User Space Multithreading. Computer Science, University of Warwick

CS 351 Design of Large Programs Threads and Concurrency

ECE 462 Object-Oriented Programming using C++ and Java. Scheduling and Critical Section

Chapter 6 Concurrency: Deadlock and Starvation

Computation Abstractions. Processes vs. Threads. So, What Is a Thread? CMSC 433 Programming Language Technologies and Paradigms Spring 2007

Multitasking Multitasking allows several activities to occur concurrently on the computer. A distinction is usually made between: Process-based multit

Virtual Machine Design

CS533 Concepts of Operating Systems. Jonathan Walpole

Advanced Topic: Efficient Synchronization

Opera&ng Systems ECE344

Operating Systems. Designed and Presented by Dr. Ayman Elshenawy Elsefy

Today: Synchronization. Recap: Synchronization

Concurrent Programming

CS 5523 Operating Systems: Midterm II - reivew Instructor: Dr. Tongping Liu Department Computer Science The University of Texas at San Antonio

Concurrent Programming Issues & Readers/Writers

SSC - Concurrency and Multi-threading Java multithreading programming - Synchronisation (II)

CS 4410 Operating Systems. Review 1. Summer 2016 Cornell University

Lecture 7: Process & Thread Introduction

Last Class: Synchronization Problems. Need to hold multiple resources to perform task. CS377: Operating Systems. Real-world Examples

Synchronization SPL/2010 SPL/20 1

Operating Systems. Lecture 4 - Concurrency and Synchronization. Master of Computer Science PUF - Hồ Chí Minh 2016/2017

CSCE Operating Systems Scheduling. Qiang Zeng, Ph.D. Fall 2018

27/04/2012. We re going to build Multithreading Application. Objectives. MultiThreading. Multithreading Applications. What are Threads?

Introducing Shared-Memory Concurrency

CS11 Java. Fall Lecture 7

Thread-Local. Lecture 27: Concurrency 3. Dealing with the Rest. Immutable. Whenever possible, don t share resources

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Steve Gribble. Synchronization. Threads cooperate in multithreaded programs

POSIX / System Programming

Solved MCQs on Operating System Principles. Set-1

Process & Thread Management II. Queues. Sleep() and Sleep Queues CIS 657

Process & Thread Management II CIS 657

Concurrency: Deadlock and Starvation

Concurrency - Topics. Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads

JAVA CONCURRENCY FRAMEWORK. Kaushik Kanetkar

High Performance Computing Course Notes Shared Memory Parallel Programming

CSE 153 Design of Operating Systems

CS 2112 Lecture 20 Synchronization 5 April 2012 Lecturer: Andrew Myers

Process Synchronization(2)

2. The system of... generally ran one job at a time. These were called single stream batch processing.

Synchronization I. Jo, Heeseung

CMSC 433 Programming Language Technologies and Paradigms. Concurrency

Contribution:javaMultithreading Multithreading Prof. Dr. Ralf Lämmel Universität Koblenz-Landau Software Languages Team

Chair of Software Engineering. Languages in Depth Series: Java Programming. Prof. Dr. Bertrand Meyer. Exercise Session 9.

Concurrency: Mutual Exclusion and Synchronization. Concurrency

Techno India Batanagar Department of Computer Science & Engineering. Model Questions. Multiple Choice Questions:

Intro to Threads. Two approaches to concurrency. Threaded multifinger: - Asynchronous I/O (lab) - Threads (today s lecture)

CS 160: Interactive Programming

ECE519 Advanced Operating Systems

CMSC 132: Object-Oriented Programming II. Threads in Java

CS 455: INTRODUCTION TO DISTRIBUTED SYSTEMS [THREADS] Frequently asked questions from the previous class survey

Threads and Parallelism in Java

Monitors; Software Transactional Memory

Main Points of the Computer Organization and System Software Module

Multi-core Architecture and Programming

Transcription:

Chair of Software Engineering C# Programming in Depth Prof. Dr. Bertrand Meyer March 2007 May 2007 Lecture 8: Threads Lisa (Ling) Liu

What is a thread? A thread is an independent execution path, able to run simultaneously with other threads A C# program starts in a single thread created automatically by the CLR and operating system (the main thread), and is made multithreaded by creating additional threads CLR assigns each thread its own memory stack so that local variables are kept separate Threads share data if they have a common reference to the same data C# programming lecture 8: Threads 2

How threading works Multithreading is managed internally by a thread scheduler, a function the CLR typically delegates to the operating system. On a single-processor computer, a thread scheduler performs time-slicing rapidly switching execution between each of the active threads On a multi-processor computer, multithreading is implemented with a mixture of time-slicing and genuine concurrency where different threads run code simultaneously on different CPUs. C# programming lecture 8: Threads 3

Threads vs. Processes All threads within a single application are logically contained within a process the operating system unit in which an application runs The key difference between threads and processes: Processes are fully isolated from each other; Threads share memory with other threads running in the same application C# programming lecture 8: Threads 4

Why use concurrency? Making use of multiprocessors Driving slow devices, such as disks, networks, terminals and printers Achieving timely response to the GUI s users Building a distributed system C# programming lecture 8: Threads 5

Thread facilities Thread creation Mutual exclusion Waiting for events Getting a thread out of an unwanted long-term wait C# facility: The System.Threading namespace C# lock statement C# programming lecture 8: Threads 6

Thread creation 1. Create a type method to be the entry point for the new thread 2. Create a new ParameterizedThreadStart (or Legacy ThreadStart) delegate, passing the address of the method defined in step 1 to the constructor 3. Create a Thread object, passing the ParameterizedThreadStart / ThreadStart delegate as a constructor argument 4. Establish any initial thread characteristics (name, priority, etc.) 5. Call the Thread.Start() method. This starts the thread at the method referenced by the delegate created in step 2 as soon as possible C# programming lecture 8: Threads 7

Starting a thread using System.Threading; Printer p = new Printer(); int[] a = 1, 2, 3, 4, 5, 6,7,8,9,10; Thread mythread = new Thread (new ParameterizedThreadStart (p.printnumbers)); mythread.name = "Secondary"; mythread.start (a); Function executed in the created thread Establish the thread characteristics public class Printer public void PrintNumbers(object a)... Take object argument, no return value... C# programming lecture 8: Threads 8

Foreground and background threads using System.Threading;... mythread.start (a); a foreground thread prevents the current application from terminating using System.Threading;... mythread.isbackground = true; mythread.start (a); mythread.join(); a background thread can be terminated after all foreground threads terminates Blocking calling thread until it terminates C# programming lecture 8: Threads 9

Thread safety The simplest way that threads interact is through access to shared memory. Thread safety means the shared memory is always in a correct state even when used concurrently by multiply thread To achieve the thread safety we need to synchronize the threads accessing the shared memory In C#, this is achieved with the class Monitor and the language s lock statement C# programming lecture 8: Threads 10

Thread synchronization: lock Specify for a critical section that can only be executed by one thread at any time Syntax: lock (expression) embedded-statement Can be any object The lock statement locks the given object, then executes the contained statements, then unlock the object. C# programming lecture 8: Threads 11

public class KV string k, v; public void SetKV (string nk, string nv) lock (this) this.k = nk; this.v = nv; shared variables are instance fields, lock object critical section

static KV head = null; KV next = null; public void AddToList() lock (typeof (KV)) shared variables static fields, lock the type of the class System.Diagnostics.Debug.Assert (this.next == null); this.next = head; head = this;

Thread synchronization: Monitor type The C# lock statement is really just a shorthand notation for working with the System.Threading.Monitor type public class KV string k; public void SetK (string nk) lock (this) this.k = nk; public class KV string k; public void SetK (string nk) Monitor.Enter(this); try this.k = nk; finally Monitor.Exit (this); C# programming lecture 8: Threads 14

Waiting for a condition Resource scheduling policy embodied by lock() : Only one thread can access the shared resources at a time Requirement for more complicated resource scheduling policy Allow a thread to block until some condition is true In C# and Java, there is no separate type for this mechanism Every object inherently implements one condition variable The Monitor class provides static Wait, Pulse and PulseAll methods to manipulate an object s condition variables C# programming lecture 8: Threads 15

Manipulate condition variables A thread that calls Wait must already hold the object s lock The Wait operation automatically unlocks the object and blocks the thread (a thread is blocked in this way is said to be waiting on the object ) The Pulse method awakes at least one thread that is waiting on the object (possibly more than one) The PulseAll method awakes all threads that are awaiting on the object When a thread is awoken it relocks the object C# programming lecture 8: Threads 16

public static KV GetFromList() KV res; unlock typeof(kv) and blocks lock (typeof(kv)) while (head = null) Monitor.Wait (typeof(kv)) res = head; head = res.next; res.next = null return res; public void AddToList() wake up a thread that was lock (typeof(kv)) waiting for the locked variable this.next = head; head = this; Monitor.Pulse(typeof(KV));

Interrupting a thread Interrupt a thread to bring it out of a long-term wait Calling Interrupt on a blocked thread forcibly release it, throwing a ThreadInterruptException. public sealed class Thread public void Interrupt ()...... C# programming lecture 8: Threads 18

Using locks: accessing shared data Basic rule: in a multi-threaded program all shared mutable data must be protected by associating it with some object s lock, and you must access the data only from a thread that is holding that lock (i.e., from a thread executing a lock statement that locked the object). C# programming lecture 8: Threads 19

Unprotected data The simplest bug related to locks occurs when you fail to protect some mutable data and then you access it without the benefits of synchronization. public class Table object[] table = new object [1000] int i = 0; i i+1 null public void Insert (object obj) i+2 if (obj!= null) table[i] = obj; thread A: Insert (x) thread B: Insert (y) i++; thread A: Insert (x) thread B: Insert (y)... y C# programming lecture 8: Threads 20

public class Table object[] table = new object [1000] int i = 0; public void Insert (object obj) if (obj!= null) lock (this) table[i] = obj; i++;

Locking granularity Simple and coarse rule: Use object instance s lock to protect all the instance fields of a class Use typeof(theclass) to protect the static fields C# programming lecture 8: Threads 22

Deadlock A deadlock is a bug when two threads are trying to access resources, which are locked by each other. public class DeadLock static object a = new object(); static object b = new object();... public static void Get() lock (a) lock (b)...... public static void Give() lock (b) lock (a)... To avoid this deadlock... C# programming lecture 8: Threads 23

Have a partial order for the acquisition of locks in your program Can avoid deadlocks involving only locks Partition the locked data into smaller pieces protected by separate locks But: The smaller of your lock granularity, the more complicated your locking becomes, and the more likely you are to become confused about which lock is protecting which data, and end up with some unsynchronized access to shared data.

Poor performance through lock conflicts Whenever a thread is holding a lock, it is potentially stopping another thread from making progress When the thread that is holding a lock ceases to make progress, the total throughput of your program is degraded Protect different fields of an object with different locks, in order to get better efficiency by accessing them simultaneously from different threads C# programming lecture 8: Threads 25

public class F static F head = null; //protected by typeof(f) string myname; //immutable F next = null; //protected by typeof(f) D data; //protected by this public static F Open (string name) lock (typeof(f)) for (F f = head; f!= null; f = f.next) if (name.equals(f.myname)) return f; //Else get a new F, enqueue it on head and return it return...; public void Write (F f, string msg) lock (this)...// access f.data use one lock for operations on global file list lock per file for operations only affecting that file

Using wait an pulse: scheduling shared resources Using Wait while (!expression) Monitor.Wait(obj); Using PulseAll PulseAll is useful in the schedule policy known as shared/exclusive locking (or readers/writers locking) C# programming lecture 8: Threads 27

readers/writers locking policy example public class RW int i = 0; //protected by this public void AcquireExclusive () lock (this) while (i!= 0) Monitor.Wait (this); i = -1; public void ReleaseExclusive() lock (this) i = 0; Monitor.PulseAll (this);... Any thread wanting to modify the data calls AcquireExcusive, then modifies the data, then calls ReleaseExclusive C# programming lecture 8: Threads 28

public void AcquireShared () lock (this) while (i < 0) Monitor.Wait (this); i ++; public void ReleaseShared() lock (this) i --; if (i == 0) Monitor.Pulse(this); // class RW Any thread wanting to read the data calls AcquireShared, then modifies the data, then calls ReleaseShared When the variable i is greater than zero, it counts the number of active readers. When it is negative there is an active writer. When it is zero, no thread is using the data

Starvation The resource scheduling decisions should be fair so that all thread are equal or some more to access the resources Starvation: some thread will never make progress. Consider following thread schedule in readers/writers locking example : Thread A calls AcquireShared ; i = 1; Thread B calls AcquireShared ; i = 2; Thread A calls ReleaseShared ; i = 1; Thread C calls AcquireShared ; i = 2 Thread B calls Release Shared ; i = 1; a blocked potential writer thread is starving C# programming lecture 8: Threads 30

... int writewaiters = 0; public void AcquireExclusive () lock (this) writewaiters++; while (i!= 0) Monitor.Wait (this); writewaiters--; i = -1; public void AcquireShared () lock (this) if (writewaiters > 0) Moniter.Wait (this); while (i < 0) Monitor.Wait (this); i ++;

Deadlock You can introduce deadlocks by waiting on objects, even though you have been careful to have a partial order on acquiring locks public class DeadLock static object a = new object(); static object b = new object(); static bool ready = false public static void Get() lock (a) lock (b) while (!ready) Monitor.Wait (b); unlock b, but a is still locked... C# programming lecture 8: Threads 32

... public static void Give() lock (b) lock (a) ready = true; Monitor.Pulse(b); deadlock happens when Thread A call Get; Thread B call Give;

Pipelining Build a chain of producer-consumer relationships, known as pipeline, for example (three-stage pipeline): Thread A initiates an action, all it does is enqueue a requrest in a buffer Thread B takes the action from the buffer, performs part of the work, then enqueues it in a second buffer Thread C takes it from there and does the rest of the work C# programming lecture 8: Threads 34

Using interrupt: diverting the flow of control The purpose of the Interrupt method of a thread is to tell the thread that it should abandon what is doing, and let control return to a higher level abstraction, presumably the one that make the call of interrupt. C# programming lecture 8: Threads 35

Reference Andrew D. Birrell, An introduction to programming with C# threads C# programming lecture 8: Threads 36