Programming Language Basics Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
CSCC69: Operating Systems
Chapter 6: Process Synchronization
Ade Azurat, Advanced Programming 2004 (Based on LYS Stefanus’s slides) Advanced Programming 2004, Based on LYS Stefanus’s slides Slide 2.1 Multithreading.
Monitors & Blocking Synchronization 1. Producers & Consumers Problem Two threads that communicate through a shared FIFO queue. These two threads can’t.
Introduction Companion slides for
Concurrent Queues Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
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?
CS 11 java track: lecture 7 This week: Web tutorial:
Shared Counters and Parallelism Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
ESE Einführung in Software Engineering X. CHAPTER Prof. O. Nierstrasz Wintersemester 2005 / 2006.
Synchronization in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
Concurrency - 1 Tasking Concurrent Programming Declaration, creation, activation, termination Synchronization and communication Time and delays conditional.
שירן חליבה Concurrent Queues. Outline: Some definitions 3 queue implementations : A Bounded Partial Queue An Unbounded Total Queue An Unbounded Lock-Free.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
Multicore Programming
Java Programming: Advanced Topics
Atomic Operations David Monismith cs550 Operating Systems.
Lecture 5 : JAVA Thread Programming Courtesy : MIT Prof. Amarasinghe and Dr. Rabbah’s course note.
Today’s Agenda  Quick Review  Finish Java Threads  The CS Problem Advanced Topics in Software Engineering 1.
Producer-Consumer Problem The problem describes two processes, the producer and the consumer, who share a common, fixed-size buffer used as a queue.bufferqueue.
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
ICS 313: Programming Language Theory Chapter 13: Concurrency.
Java Thread and Memory Model
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
Threads in Java1 Concurrency Synchronizing threads, thread pools, etc.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Software Systems Advanced Synchronization Emery Berger and Mark Corner University.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
Monitors and Blocking Synchronization Dalia Cohn Alperovich Based on “The Art of Multiprocessor Programming” by Herlihy & Shavit, chapter 8.
Concurrent Queues Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
Threads. Objectives You must be able to answer the following questions –What code does a thread execute? –What states can a thread be in? –How does a.
Multiprocessor Architecture Basics Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
1 Based on: The art of multiprocessor programming Maurice Herlihy and Nir Shavit, 2008 Appendix A – Software Basics Appendix B – Hardware Basics Introduction.
COSC 3407: Operating Systems Lecture 9: Readers-Writers and Language Support for Synchronization.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists.
Threads in Java Threads Introduction: After completing this chapter, you will be able to code your own thread, control them efficiently without.
Implementing Lock. From the Previous Lecture  The “too much milk” example shows that writing concurrent programs directly with load and store instructions.
CSCI1600: Embedded and Real Time Software Lecture 17: Concurrent Programming Steven Reiss, Fall 2015.
CSC CSC 143 Threads. CSC Introducing Threads  A thread is a flow of control within a program  A piece of code that runs on its own. The.
The Relative Power of Synchronization Operations Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
Concurrency: Locks and synchronization Slides by Prof. Cox.
Implementing Mutual Exclusion Andy Wang Operating Systems COP 4610 / CGS 5765.
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.
CS162 Section 2. True/False A thread needs to own a semaphore, meaning the thread has called semaphore.P(), before it can call semaphore.V() False: Any.
Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists.
Distributed and Parallel Processing George Wells.
Review Array Array Elements Accessing array elements
Multithreading / Concurrency
Multi Threading.
CS703 – Advanced Operating Systems
Concurrent Objects Companion slides for
Simulations and Reductions
Multithreading.
Producer-Consumer Problem
Multithreading.
Programming with Shared Memory Java Threads and Synchronization
Programming with Shared Memory Java Threads and Synchronization
Implementing Mutual Exclusion
Implementing Mutual Exclusion
Computer Science 2 06A-Java Multithreading
CS333 Intro to Operating Systems
Threads and Multithreading
Review The Critical Section problem Peterson’s Algorithm
Presentation transcript:

Programming Language Basics Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Art of Multiprocessor Programming 2 Computer Science isn’t really About Programming Any more than –Astronomy is about telescopes –French Literature is about unfiltered cigarettes

Art of Multiprocessor Programming 3 Nevertheless We need a programming language to state algorithms We use Java –High-level Sometimes a little too high level –Most of you have seen it before And may see it again

Art of Multiprocessor Programming 4 Other Languages PThreads –C and C++ C# MPI Etc…

Art of Multiprocessor Programming 5 Threads Execution of a sequential program You can tell a thread –What to do –When to start You can –Wait for it to finish Other stuff: –Interrupt it, give it priority, etc.

Art of Multiprocessor Programming 6 Threads in Java Class java.lang.Thread Each thread has a method –Void run() Executes when it starts Thread vanishes when it returns You must provide this method

Art of Multiprocessor Programming 7 Creating a Thread Create a Runnable object –Runnable is an interface –Provides run() method Pass Runnable object to thread constructor

Art of Multiprocessor Programming 8 A Runnable Class public class Hello implements Runnable { String message; public Hello(m) { message = m; } public void run() { System.out.println(message); }

Art of Multiprocessor Programming 9 A Runnable Class public class Hello implements Runnable { String message; public Hello(m) { message = m; } public void run() { System.out.println(message); } Runnable interface

Art of Multiprocessor Programming 10 Creating a Thread String m = “Hello from ” + i; Runnable h = new Hello(m); Thread t = new Thread(h);

Art of Multiprocessor Programming 11 Creating a Thread String m = “Hello from ” + i; Runnable h = new Hello(m); Thread t = new Thread(h); Create a Runnable object

Art of Multiprocessor Programming 12 Creating a Thread String m = “Hello from ” + i; Runnable h = new Hello(m); Thread t = new Thread(h); Create the thread

Art of Multiprocessor Programming 13 Syntactic Help Defining a single-use class like Hello can be a nuisance Java provides special syntax Anonymous inner classes –May be more trouble than it’s worth –You should recognize it

Art of Multiprocessor Programming 14 Anonymous Inner Class t = new Thread( new Runnable() { public void run() { System.out.println(m); } );

Art of Multiprocessor Programming 15 Anonymous Inner Class t = new Thread( new Runnable() { public void run() { System.out.println(m); } ); Creates object of anonymous Runnable class

Art of Multiprocessor Programming 16 Anonymous Inner Class t = new Thread( new Runnable() { public void run() { System.out.println(m); } ); Calls Thread constructor with anonymous object

Art of Multiprocessor Programming 17 Starting a Thread Starts the new thread Caller returns immediately Caller & thread run in parallel t.start();

Art of Multiprocessor Programming 18 Joining a Thread Blocks the caller Waits for the thread to finish Returns when the thread is done t.join();

Art of Multiprocessor Programming 19 Monitors Each object has an implicit lock Managed by synchronized modifer –Methods –Code blocks OK for easy cases Not always for hard cases

Art of Multiprocessor Programming 20 Call Center Scenario Calls arrive faster than they can be answered –Play recorded message “your call is very important to us …” –Put call in queue Play insipid music … –Operators dequeue call when ready

Art of Multiprocessor Programming 21 Bad Queue Implementation class Queue { int head = 0, tail = 0; T[] items = new T[QSIZE]; public enq(T x) { items[(tail++) % QSIZE] = x; } public T deq() { return items[(head++) % QSIZE] }}

Art of Multiprocessor Programming 22 class Queue { int head = 0, tail = 0; T[] items = new T[QSIZE]; public enq(T x) { items[(tail++) % QSIZE] = x; } public T deq() { return items[(head++) % QSIZE] }} Bad Queue Implementation Works for generic type T

Art of Multiprocessor Programming 23 class Queue { int head = 0, tail = 0; T[] items = new T[QSIZE]; public enq(T x) { items[(tail++) % QSIZE] = x; } public T deq() { return items[(head++) % QSIZE] }} Bad Queue Implementation Array of T items

Art of Multiprocessor Programming 24 class Queue { int head = 0, tail = 0; T[] items = new T[QSIZE]; public enq(T x) { items[(tail++) % QSIZE] = x; } public T deq() { return items[(head++) % QSIZE] }} Bad Queue Implementation next slot to dequeue, 1 st empty slot

Art of Multiprocessor Programming 25 class Queue { int head = 0, tail = 0; T[] items = new T[QSIZE]; public enq(T x) { items[(tail++) % QSIZE] = x; } public T deq() { return items[(head++) % QSIZE] }} Bad Queue Implementation Put in empty slot, advance head

Art of Multiprocessor Programming 26 Of course, this doesn’t work head

Art of Multiprocessor Programming 27 Mutual Exclusion Only one thread modifying queue fields at a time Use synchronized methods –Locks object on call –Releases lock on return

Art of Multiprocessor Programming 28 Mutual Exclusion head

Art of Multiprocessor Programming 29 Synchronized Method class Queue { … public synchronized enq(T x) { items[(tail++) % QSIZE]; }... }

Art of Multiprocessor Programming 30 Synchronized Method class Queue { … public synchronized enq(T x) { items[(head++) % QSIZE]; }... } Lock acquired on entry, released on exit

Art of Multiprocessor Programming 31 Syntactic Sugar class Queue { … public enq(T x) { synchronized (this) { items[(tail++) % QSIZE]; }... }

Art of Multiprocessor Programming 32 Syntactic Sugar class Queue { … public enq(T x) { synchronized (this) { items[(tail++) % QSIZE]; }... } Same meaning, more verbose

Art of Multiprocessor Programming 33 Vocabulary A synchronized method locks the object No other thread can call another synchronized method for that same object Code in middle is critical section

Art of Multiprocessor Programming 34 Re-entrant Locks What happens if you lock the same object twice? –In Java, no deadlock –Keeps track of number of times locked and unlocked –Unlock occurs when they balance out

Art of Multiprocessor Programming 35 Still Doesn’t Work class Queue { … public synchronized enq(T x) { items[(tail++) % QSIZE] = x; }... }

Art of Multiprocessor Programming 36 Still Doesn’t Work class Queue { … public synchronized enq(T x) { items[(tail++) % QSIZE] = x; }... } What if the array is full?

Art of Multiprocessor Programming 37 Waiting What if –Enqueuer finds a full array? –Dequeuer finds an empty array? Throw an exception? –What can caller do? –Repeated retries wasteful Wait for something to happen

Art of Multiprocessor Programming 38 Waiting Synchronized Method class Queue { … public synchronized enq(T x) { while (tail - head == QSIZE) {}; items[(tail++) % QSIZE] = x; }... }

Art of Multiprocessor Programming 39 class Queue { … public synchronized enq(T x) { while (tail - head == QSIZE) {}; items[(tail++) % QSIZE] = x; }... } Waiting Synchronized Method Spin while the array is full

Art of Multiprocessor Programming 40 Deadlock Enqueuer is –Waiting for a dequeuer –While holding the lock Dequeuer –Waiting for enqueuer to release lock Nothing will ever happen

Art of Multiprocessor Programming 41 Waiting Thread Release lock while waiting When “something happens” –Reacquire lock –Either Rerelease lock & resume waiting Finish up and return

Art of Multiprocessor Programming 42 Styles of Waiting Spinning –Repeatedly retest condition Blocking –Ask OS to run someone else

Art of Multiprocessor Programming 43 Styles of Waiting Spinning –Good for very short intervals –Expensive to call OS –Works only on multiprocessors! Blocking –Good for longer intervals –Processor can do work Clever libraries sometimes mix

Art of Multiprocessor Programming 44 The wait() Method Releases lock on q Sleeps (gives up processor) Awakens (resumes running) Reacquires lock & returns q.wait();

Art of Multiprocessor Programming 45 The wait() Method class Queue { … public synchronized enq(T x) { while (tail – head == QSIZE) { wait(); }; items[(head++) % QSIZE] = x; }... }

Art of Multiprocessor Programming 46 class Queue { … public synchronized enq(T x) { while (tail – head == QSIZE) { wait(); }; items[(head++) % QSIZE] = x; }... } Waiting Synchronized Method Keep retesting condition

Art of Multiprocessor Programming 47 class Queue { … public synchronized enq(T x) { while (tail – head == QSIZE) { wait(); }; items[(head++) % QSIZE] = x; }... } Waiting Synchronized Method Keep retesting condition Release lock & sleep

Art of Multiprocessor Programming 48 Wake up and Smell the Coffee When does a waiting thread awaken? –Must be notified by another thread –when something has happened Failure to notify in a timely way is called a “lost wakeup”

Art of Multiprocessor Programming 49 The wait() Method Awakens one waiting thread Which will reacquire lock & returns q.notify();

Art of Multiprocessor Programming 50 The wait() Method Awakens all waiting threads Which will reacquire lock & return q.notifyAll();

Art of Multiprocessor Programming 51 The wait() Method public synchronized enq(T x) { while (tail - head == QSIZE) { wait(); }; items[(head++) % QSIZE] = x; if (tail-head == QSIZE-1) { notify(); }

Art of Multiprocessor Programming 52 public synchronized enq(T x) { while (tail - head == QSIZE) { wait(); }; items[(head++) % QSIZE] = x; if (tail-head == QSIZE-1) { notify(); } The wait() Method Wait for empty slot

Art of Multiprocessor Programming 53 public synchronized enq(T x) { while (tail - head == QSIZE) { wait(); }; items[(head++) % QSIZE] = x; if (tail-head == QSIZE-1) { notify(); } The wait() Method Stuff item into array

Art of Multiprocessor Programming 54 public synchronized enq(T x) { while (tail - head == QSIZE) { wait(); }; items[(head++) % QSIZE] = x; if (tail-head == QSIZE-1) { notify(); } The wait() Method If the queue was empty, wake up a dequeuer

Art of Multiprocessor Programming 55 Lost Wakeup This code has a lost wakeup bug Possible to have –Waiting dequeuer –Non-empty queue Because not enough threads awakened

Art of Multiprocessor Programming 56 Empty queue, waiting dequeuers Zzz …

Art of Multiprocessor Programming 57 Enqueuer puts item in queue Zzz …

Art of Multiprocessor Programming 58 Since queue was empty, wakes dequeuer Zzz … Ouch! notify() this!

Art of Multiprocessor Programming 59 1 st Dequeuer slow, overtaken by enqueuers Zzz … Must have coffee …

Art of Multiprocessor Programming 60 1 st Dequeuer finishes Zzz …

Art of Multiprocessor Programming 61 Solutions Don’t write buggy code d’oh! Always call notifyAll() Can also use timed waits –Wake up after specified time

Art of Multiprocessor Programming 62 Thread-Local Data In many of our examples we assume –Threads have unique ids –In range 0,…,n-1 Where do they come from? –Long-lived data –Unique to a thread

Art of Multiprocessor Programming 63 Thread-Local Data in Java ThreadLocal class No built-in language support Library classes –Syntax is awkward –Very useful anyway

Art of Multiprocessor Programming 64 ThreadLocal methods Changes calling thread’s version of object Other threads’ versions unaffected ThreadLocal local; T x = …; local.set(x);

Art of Multiprocessor Programming 65 ThreadLocal methods Returns calling thread’s version of object ThreadLocal local; T x = local.get();

Art of Multiprocessor Programming 66 Initializing ThreadLocals Called by get() method the first time the thread-local variable is accessed. T x = local.initialValue();

Art of Multiprocessor Programming 67 Example Return unique thread id Take a number first time called int me = ThreadID.get()

Art of Multiprocessor Programming 68 Thread-Local IDs public class ThreadID { static volatile int nextID = 0; private static LocalID threadID = new LocalID(); public static int get() { return threadID.get(); } … // define LocalID here }

Art of Multiprocessor Programming 69 Thread-Local IDs public class ThreadID { static volatile int nextID = 0; private static LocalID threadID = new LocalID(); public static int get() { return threadID.get(); } … // define LocalID here } Next ID to assign

Art of Multiprocessor Programming 70 Thread-Local IDs public class ThreadID { static volatile int nextID = 0; private static LocalID threadID = new LocalID(); public static int get() { return threadID.get(); } … // define LocalID here } Declare & initialize thead-local ID

Art of Multiprocessor Programming 71 Thread-Local IDs public class ThreadID { static volatile int nextID = 0; private static LocalID threadID = new LocalID(); public static int get() { return threadID.get(); } … // define LocalID here } Return value of thread-local ID

Art of Multiprocessor Programming 72 The Inner Class static class LocalID extends ThreadLocal { synchronized Integer initialValue() { return nextID++; }

Art of Multiprocessor Programming 73 The Inner Class static class LocalID extends ThreadLocal { synchronized Integer initialValue() { return nextID++; } Subclass of ThreadLocal

Art of Multiprocessor Programming 74 The Inner Class static class LocalID extends ThreadLocal { synchronized Integer initialValue() { return nextID++; } Overrides initialValue()

Art of Multiprocessor Programming 75 Summary Threads –And how to control them Synchronized methods –Wait, notify, and NotifyAll Thread-Local objects

Art of Multiprocessor Programming 76 This work is licensed under a Creative Commons Attribution- ShareAlike 2.5 License.Creative Commons Attribution- ShareAlike 2.5 License You are free: –to Share — to copy, distribute and transmit the work –to Remix — to adapt the work Under the following conditions: –Attribution. You must attribute the work to “The Art of Multiprocessor Programming” (but not in any way that suggests that the authors endorse you or your use of the work). –Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under the same, similar or a compatible license. For any reuse or distribution, you must make clear to others the license terms of this work. The best way to do this is with a link to – Any of the above conditions can be waived if you get permission from the copyright holder. Nothing in this license impairs or restricts the author's moral rights.