An Intro to Programming with C# Threads Presentation by: Jason Bender, Garrett Lund, Ben Gamble, Michael Calvo, and Jeff Corbell.

Slides:



Advertisements
Similar presentations
Practice Session 7 Synchronization Liveness Deadlock Starvation Livelock Guarded Methods Model Thread Timing Busy Wait Sleep and Check Wait and Notify.
Advertisements

Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
CS 5704 Fall 00 1 Monitors in Java Model and Examples.
COS 461 Fall 1997 Concurrent Programming u Traditional programs do one thing at a time. u Concurrent programs do several things at once. u Why do this?
IT Systems Multiprocessor System EN230-1 Justin Champion C208 –
Java How to Program, 9/e CET 3640 Professor: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 7 Threads  Threads & Processes  Multi Threading  Class Thread  Synchronized Methods  Wait & Notify.
Computer Systems/Operating Systems - Class 8
Algorithm Programming Concurrent Programming in Java Bar-Ilan University תשס"ח Moshe Fresko.
CS444/CS544 Operating Systems Synchronization 2/16/2006 Prof. Searleman
Multithreading The objectives of this chapter are:
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
Concurrency CS 510: Programming Languages David Walker.
© Andy Wellings, 2004 Roadmap  Introduction  Concurrent Programming  Communication and Synchronization  Completing the Java Model  Overview of the.
©Brooks/Cole, 2003 Chapter 7 Operating Systems Dr. Barnawi.
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.
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.
Java How to Program, 9/e CET 3640 Professor: Dr. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.
Collage of Information Technology University of Palestine Advanced programming MultiThreading 1.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
CS 153 Design of Operating Systems Spring 2015 Lecture 11: Scheduling & Deadlock.
Threads in Java. History  Process is a program in execution  Has stack/heap memory  Has a program counter  Multiuser operating systems since the sixties.
Threading and Concurrency Issues ● Creating Threads ● In Java ● Subclassing Thread ● Implementing Runnable ● Synchronization ● Immutable ● Synchronized.
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
Java Threads 11 Threading and Concurrent Programming in Java Introduction and Definitions D.W. Denbo Introduction and Definitions D.W. Denbo.
111 © 2002, Cisco Systems, Inc. All rights reserved.
1 Web Based Programming Section 8 James King 12 August 2003.
Games Development 2 Concurrent Programming CO3301 Week 9.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
2001 Networking Operating Systems (CO32010) 1. Operating Systems 2. Processes and scheduling 4.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
Lecture 8 Page 1 CS 111 Online Other Important Synchronization Primitives Semaphores Mutexes Monitors.
Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread.
Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1.
SPL/2010 Guarded Methods and Waiting 1. SPL/2010 Reminder! ● Concurrency problem: asynchronous modifications to object states lead to failure of thread.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Concurrency & Dynamic Programming.
Multi-Threading in Java
Multithreaded programming  Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run.
13-1 Chapter 13 Concurrency Topics Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads C# Threads.
CS333 Intro to Operating Systems Jonathan Walpole.
Multithreading. Multitasking The multitasking is the ability of single processor to perform more than one operation at the same time Once systems allowed.
Concurrency (Threads) Threads allow you to do tasks in parallel. In an unthreaded program, you code is executed procedurally from start to finish. In a.
1 Threads in Java Jingdi Wang. 2 Introduction A thread is a single sequence of execution within a program Multithreading involves multiple threads of.
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.
Multithreading The objectives of this chapter are:
Multithreading / Concurrency
Operating systems Deadlocks.
Outline Other synchronization primitives
Critical sections, locking, monitors, etc.
Other Important Synchronization Primitives
Lecture 21 Concurrency Introduction
Definitions Concurrent program – Program that executes multiple instructions at the same time. Process – An executing program (the running JVM for Java.
Operating Systems.
Multithreading.
Multithreading.
Multithreaded Programming
Operating systems Deadlocks.
Threads Chapter 4.
Multithreaded Programming
CSE 451: Operating Systems Autumn 2003 Lecture 7 Synchronization
CSE 451: Operating Systems Autumn 2005 Lecture 7 Synchronization
CSE 451: Operating Systems Winter 2003 Lecture 7 Synchronization
Multithreading in java.
CSE 153 Design of Operating Systems Winter 19
Threads and Multithreading
Multithreading The objectives of this chapter are:
Chapter 3: Process Management
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

An Intro to Programming with C# Threads Presentation by: Jason Bender, Garrett Lund, Ben Gamble, Michael Calvo, and Jeff Corbell

Outline Introduction ▫The Basics ▫Why use Concurrency ▫The Design of a Thread Facility Using Locks: Accessing Shared Data Using Wait and Pulse: Scheduling Shared Resources Using Threads: Working in Parallel Using Interrupt: Diverting the Flow of Control

Threads: The Basics What is a thread? ▫Threads allow you to write programs with simultaneous points of execution, synchronizing through shared memory. Threads are lightweight ▫Because thread creation, existence, destruction, and synchronization primitives are cheap, programmers will use them for all their concurrency needs.

Single-threaded vs. Multithreaded

Why use concurrency? Use of multiprocessors Driving slow devices ▫Disks, networks, terminals, printers Human users need concurrency Distributed systems Reduce Latency

The Design of a Thread Facility Four Major Mechanisms 1.Thread Creation 2.Mutual Exclusion 3.Waiting for Events 4.Getting out of unwanted long-term wait

Thread Creation In C# you create a thread by: 1.Creating an object of type “Thread” 2.Giving its constructor a “ThreadStart” delegate 3.Calling the thread’s “Start” method Once Run method is called: 1. Starts executing asynchronously with invocation of delegate's method 2. Method returns 3. Thread dies

Code Example Thread t = new Thread(new ThreadStart(foo.A)); t.Start(); foo.B(); t.Join();

Mutual Exclusion Used to avoid errors with multiple threads accessing shared variables Using locks is the simplest tool to accomplish this You must only access data from a thread that is holding that lock

Using Locks (in C#) General Form: lock(expression) { embedded-statement(s) }

Locking instance fields of an object class KV { string k, v; public void SetKV(string newk, string newv) { lock (this) { this.k = newk; this.v = newv; }

Locking Objects by Type static KV head = null; KV next = null; public void AddToList() { lock (typeof(KV)) { this.next = head; head = this; } {

Deadlocks involving only locks In some systems your program will deadlock if a thread tries to lock a locked object C# and Java allow an object to be locked multiple times by the same thread The object remains locked until the object is unlocked the same number of times

Deadlocks involving only locks Simplest case: thread A locks object M1; thread B locks object M2; thread A blocks trying to lock M2; thread B blocks trying to lock M1. Simple solution: lock objects in the same order. Make it so all threads do not try to lock M2 until you have obtained the lock to M1

Poor Performance due to locks The simple solution is not always the best one If threads A and B operate on separate subsets of the data, locking the whole object would decrease performance

Lock Granularity Take for example a class that manages a bunch of open buffered files, you should not lock all of the files if you want to write to just one of them Solution: lock granularity, only lock what you need to ▫Drawback – Locking becomes more difficult and you might get confused

Wait and Pulse Allows for scheduling multiple threads to share a common resource. ▫Used when mutual exclusion and locks is not enough ▫Called from within a lock statement

Example readonly object key = new object(); // thread A lock ( key ) Monitor.Wait( key ); // thread B lock ( key ) Monitor.Pulse( key );

Using “PulseAll” “PulseAll” awakens all threads that have called “Wait”. ▫Trades slightly poorer performance for greater simplicity ▫Two main reasons to use “PulseAll”

Spurious Wake-ups Awakening threads that cannot make useful progress ▫Happens when the use of “Wait” is kept simple ▫Happens when “PulseAll” is used when “Pulse” would have been sufficient ▫Happens when multiple threads “Wait” on a single object for multiple reasons.

Spurious Lock Conflicts A thread is awakened from “Waiting” on an object, and before doing useful work the thread blocks trying to lock an object. C# avoids this problem in simple cases: calling “Monitor.Pulse” which doesn’t actually let the awakened thread start executing. Instead, it is transferred to a “ready queue” on the object.

Starvation When a program making scheduling decisions does not allow a thread to run. ▫The thread will never make progress ▫Ex. 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 “ReleaseShared”; i := 1; … etc.

Deadlocks Deadlocks can be introduced by waiting on objects, even though you have been careful to have a partial order on acquiring locks. ▫Ex. Thread A acquires resource (1); Thread B acquires resource (2); Thread A wants (2), so it calls “Monitor.Wait” to wait for (2); Thread B wants (1), so it calls “Monitor.Wait” to wait for (1).

Using Threads: Working in Parallel Different situations to split a thread ► Using a multi-processor ► Multitasking ► Allowing access to multiple clients

Using Threads in User Interfaces If a program is processing, UI should respond. ► Issues:  Longest delay in the system  Keeping user input relevant

Using Threads in Network Servers Threads allow a server to assist multiple clients. ► Not everyone is on the same page. RPC-based systems create new threads with every concurrent call. Other systems only make a new thread for every connection. ► Less clutter.

Using Threads in Deferring Work Users don't want to wait. Solutions ► Simplest: System returns to caller with result to a method. New thread handles remaining work. ► Better: Same as before, but a single thread handles remaining work for all processes. ► Best: Clean-up thread doesn't need input from any of the main threads. Merges similar requests into the same action.

Using Threads in Pipelining “Many hands make a burden light.” ► Assembly line of data. Benefit: Makes the most out of multi-processors Issues: ► Balancing equal work amongst all threads. ► Knowing how many steps you can use.

Impact of Programming Environs Some factors to take into account when deciding to use threads. ► Capabilities of calling a method: Static vs Instance  Some instance methods require a lock.  Some languages provide a syncronization wrapper around an object instead of needing a lock. ► System cost  Significantly more threads than processors results in slowdown due to the stress of constant rescheduling.

Interrupts Definition: ▫stop a thread and return control to higher level ▫usually to the level that called the interrupt Uses: ▫run competing threads and end after one finishes ▫an algorithm takes too long and offer user cancel option

Interrupts class PipelinedRasterizer: IDisposable { public void Dispose(){ lock(this){ if(t1 != null) t1.Interrupt(); if(t2 != null) t2.Interrupt(); t1 = null; t2 = null; }

Interrupts calling interrupt requires thread to be in wait, sleep, or join state in C# have thread call Thread.Sleep(0) occasionally earlier designs of Java and Modula included easy ways to do this

Interrupts Cautions ▫can make code less structured ▫harder to debug sections of code Exercise restraint ▫use interrupts rarely ▫only use with the abstraction that created the thread

Interrupts Most useful when you don't really know what's going on ▫ex. you don't know where a thread could be blocked Don't confuse with exceptions and abort