Week 9, Class 3: Java’s Happens-Before Memory Model (Slides used and skipped in class) SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:

Slides:



Advertisements
Similar presentations
Threads Cannot be Implemented As a Library Andrew Hobbs.
Advertisements

50.003: Elements of Software Construction Week 6 Thread Safety and Synchronization.
(C) 2001 Daniel Sorin Correctly Implementing Value Prediction in Microprocessors that Support Multithreading or Multiprocessing Milo M.K. Martin, Daniel.
D u k e S y s t e m s Time, clocks, and consistency and the JMM Jeff Chase Duke University.
Concurrency 101 Shared state. Part 1: General Concepts 2.
Week 9, Class 3: Model-View-Controller Final Project Worth 2 labs Happens-Before ( SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:
Threading Part 2 CS221 – 4/22/09. Where We Left Off Simple Threads Program: – Start a worker thread from the Main thread – Worker thread prints messages.
“THREADS CANNOT BE IMPLEMENTED AS A LIBRARY” HANS-J. BOEHM, HP LABS Presented by Seema Saijpaul CS-510.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
1 Sharing Objects – Ch. 3 Visibility What is the source of the issue? Volatile Dekker’s algorithm Publication and Escape Thread Confinement Immutability.
CS510 Concurrent Systems Class 5 Threads Cannot Be Implemented As a Library.
1 Thread II Slides courtesy of Dr. Nilanjan Banerjee.
1 Concurrent Languages – Part 1 COMP 640 Programming Languages.
CDP 2012 Based on “C++ Concurrency In Action” by Anthony Williams and The C++11 Memory Model and GCC WikiThe C++11 Memory Model and GCC Created by Eran.
CDP 2013 Based on “C++ Concurrency In Action” by Anthony Williams, The C++11 Memory Model and GCCThe C++11 Memory Model and GCC Wiki and Herb Sutter’s.
CS2852 Week 2, Class 1 Today Generics (Section 051) Big-O runtime analysis Muddiest Point Lab Quiz Includes writing a method from ArrayList class (See.
Optimistic Design 1. Guarded Methods Do something based on the fact that one or more objects have particular states  Make a set of purchases assuming.
Internet Software Development Controlling Threads Paul J Krause.
JAVA MEMORY MODEL AND ITS IMPLICATIONS Srikanth Seshadri
Week 9, Class 3: Model-View-Controller Today Happens-Before Adapter and Façade Pattern (high-level) Tuesday: Project code due, 11pm Wednesday: Quiz Choose.
Consider the program fragment below left. Assume that the program containing this fragment executes t1() and t2() on separate threads running on separate.
Week 3, Day 2: Threads Questions about Threads “Multithreading” in Swing Lab tomorrow: Quiz Lab 3: Threading! SE-2811 Slide design: Dr. Mark L. Hornick.
Java Thread and Memory Model
The Singleton Pattern SE-2811 Dr. Mark L. Hornick 1.
COMPSCI 230 S2C 2015 Software Design and Construction Synchronization (cont.) Lecture 4 of Theme C.
Multiprocessor Cache Consistency (or, what does volatile mean?) Andrew Whitaker CSE451.
CS2852 Week 3, Class 2 Today Stacks Queues SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 1.
Today Return Quiz Class Climate Survey Review SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 1 SE3910 Week 10, Class.
ICFEM 2002, Shanghai Reasoning about Hardware and Software Memory Models Abhik Roychoudhury School of Computing National University of Singapore.
Threads and Singleton. Threads  The JVM allows multiple “threads of execution”  Essentially separate programs running concurrently in one memory space.
Java Threads 11 Threading and Concurrent Programming in Java Synchronization D.W. Denbo Synchronization D.W. Denbo.
CS2852 Week 6, Class 1 Today The run-time stack Writing and proving recursive methods SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:
CS2852 Week 5, Class 2 Today Queue Applications Circular Queue Implementation Testing SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:
CS2852 Week 7, Class 1 Today Binary Search Tree Implementing add Implementing find Return Quiz 4 (second attempt) Both sections are graded SE-2811 Slide.
Today Return Quiz First release of final project template Multithreading Options Qt socket buffers between threads Tomorrow: Quiz Interthread communication.
CS2852 Week 6, Class 2 Today Class exercise: Implementing a recursive method Binary Search Trees Tomorrow: Quiz at start of lab Implementing a recursive.
Today Quiz not yet graded Final report requirements posted More on Multithreading Happens-Before in Java SE-2811 Slide design: Dr. Mark L. Hornick Content:
CSCI1600: Embedded and Real Time Software Lecture 17: Concurrent Programming Steven Reiss, Fall 2015.
Week 10, Day 3 Review for the quarter SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 1.
Today Quiz not yet graded (Sorry!) More on Multithreading Happens-Before in Java Other langauges with Happens-Before Happens-Before in C++ SE-2811 Slide.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
Week 8, Class 3: Model-View-Controller Final Project Worth 2 labs Cleanup of Ducks Reducing coupling Finishing FactoryMethod Cleanup of Singleton SE-2811.
CS2910 Week 6, Lab Today Dictionaries in Python SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 1.
Java Thread Programming
Concurrency 2 CS 2110 – Spring 2016.
Slide design: Dr. Mark L. Hornick
Slide design: Dr. Mark L. Hornick
Week 2, Day 1: The Factory Method Pattern
The Singleton Pattern SE-2811 Dr. Mark L. Hornick.
Threads Cannot Be Implemented As a Library
Lecture 25 More Synchronized Data and Producer/Consumer Relationship
Atomicity CS 2110 – Fall 2017.
Type Systems Terms to learn about types: Related concepts: Type
Threads and Memory Models Hal Perkins Autumn 2011
Week 6, Class 2: Observer Pattern
The C++ Memory model Implementing synchronization)
Implementing synchronization
Threads and Memory Models Hal Perkins Autumn 2009
Dr. Mustafa Cem Kasapbaşı
CSCI1600: Embedded and Real Time Software
Slide design: Dr. Mark L. Hornick
Relaxed Consistency Finale
Slide design: Dr. Mark L. Hornick
Week 8, Class 3: Model-View-Controller
Slide design: Dr. Mark L. Hornick
Slide design: Dr. Mark L. Hornick
CSCI1600: Embedded and Real Time Software
Problems with Locks Andrew Whitaker CSE451.
More concurrency issues
Threads and concurrency / Safety
Presentation transcript:

Week 9, Class 3: Java’s Happens-Before Memory Model (Slides used and skipped in class) SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 1

Surprising Fact: Compiler can re-order instructions When compiled, A = B+1; B = 1; May be executed as tmp = B; B = 1; tmp++; A = tmp; Source: Preshing (see notes for this slide) SE-2811 Dr.Yoder 2

Why is this OK? A = B+1; B = 1; tmp = B; A = tmp + 1; B = 1; SE-2811 Dr.Yoder 3 SAME

Why is this OK? A = B+1; B = 1; tmp = B; tmp++; A = tmp; B = 1; SE-2811 Dr.Yoder 4 SAME

Why is this OK? A = B+1; B = 1; tmp = B; tmp++; A = tmp; B = 1; SE-2811 Dr.Yoder 5 SAME tmp = B; B = 1; tmp++; A = tmp; SAME

Why does the compiler do this? Machine instructions are like this. tmp = B; tmp++; A = tmp; B = 1; Exercise: What machine instructions are these? 6

Why does the compiler do this? We can read and write faster to “tmp” tmp = B; B = 1; tmp++; A = tmp; Exercise: What is tmp? Why can we read and write faster to it? SE-2811 Dr.Yoder 7

Result: Seemingly impossible things can happen A = 0; … r1 = A; B = 2; B = 0; … r2 = B; A = 1; SE-2811 Dr.Yoder 8 Exercise: How could we get r1 = 1 and r2 = 0? Java Lang. Spec Example

It is possible for both r1 = 1 and r2 = 2! A = 0; … r1 = A; B = 2; B = 0; … A = 1; r2 = B; SE-2811 Dr.Yoder 9 Exercise: How could we get r1 = 1 and r2 = 2? (Would this be possible without reordering?) Java Lang. Spec Example

How to ensure multithreaded applications work as expected? Avoid problems due to reordering caching (as seen yesterday) While still being fast! SE-2811 Dr. Yoder 10

We need a “Memory Model” “The Java programming language memory model works by examining each read in an execution trace and checking that the write observed by that read is valid according to certain rules.” /jls-17.html#jls-17.4 SE-2811 Dr.Yoder 11

Shared Variables Shared instance fields static fields array elements Never shared local variables method parameters exception parameters e.g. catch(Exception e) SE-2811 Dr.Yoder html#jls

Happens-Before “If one action happens-before another, then the first is visible to and ordered before the second.” /jls-17.html#jls SE-2811 Dr. Yoder 13

Important caveat: “An implementation is free to produce any code it likes, as long as all resulting executions of a program produce a result that can be predicted by the memory model.” /jls-17.html#jls-17.4 SE-2811 Dr.Yoder 14

Some happens-before relationships “Each action in a thread happens- before every action in that thread that comes later in the program's order. “An unlock (synchronized block or method exit) of a monitor happens-before every subsequent lock (synchronized block or method entry) of that same monitor. “A write to a volatile field happens- before every subsequent read of that same field. SE-2811 Dr. Yoder 15

Some happens-before relationships “A call to start on a thread happens- before any action in the started thread. “All actions in a thread happen-before any other thread successfully returns from a join on that thread.” il/concurrent/package- summary.html#MemoryVisibility SE-2811 Dr.Yoder 16

“Happens-Before Does Not Imply Happening Before” “It should be noted that the presence of a happens-before relationship between two actions does not necessarily imply that they have to take place in that order in an implementation. If the reordering produces results consistent with a legal execution, it is not illegal.” /jls-17.html#jls SE-2811 Dr.Yoder 17

“Happening Before Does Not Imply Happens-Before” “More specifically, if two actions share a happens-before relationship, they do not necessarily have to appear to have happened in that order to any code with which they do not share a happens-before relationship. Writes in one thread that are in a data race with reads in another thread may, for example, appear to occur out of order to those reads.” /jls-17.html#jls SE-2811 Dr.Yoder 18

“Happening Before Does Not Imply Happens-Before” void publishMessage() { answer = 42; // (1) isReady = true; // (2) } void consumeMessage() { if (isReady) // (3) sout(answer); // (4) } before-relation/ before-relation/ SE-2811 Dr.Yoder 19

“Happening Before Does Not Imply Happens-Before” void publishMessage() { answer = 42; isReady = 1; } void consumeMessage() { if (isReady) sout("%d\n", answer); } 2/the-happens-before- relation/ Exercise: How might this code execute to print 0 instead of 42 ? (You may use reordering and/or caching in your solution) SE-2811 Dr.Yoder 20

Happens-Before “If one action happens-before another, then the first is visible to and ordered before the second.” /jls-17.html#jls SE-2811 Dr. Yoder 21

Important caveat: “An implementation is free to produce any code it likes, as long as all resulting executions of a program produce a result that can be predicted by the memory model.” /jls-17.html#jls-17.4 SE-2811 Dr.Yoder 22

“Happening Before Does Not Imply Happens-Before” void publishMessage() { answer = 42; isReady = 1; } void consumeMessage() { if (isReady) printf("%d\n", answer); } 2/the-happens-before- relation/ 2/the-happens-before- relation/ Exercise: How might this code execute to print 0 instead of 42 ? (You may use reordering and/or caching in your solution) SE-2811 Dr.Yoder 23

Some happens-before relationships “Each action in a thread happens- before every action in that thread that comes later in the program's order. “An unlock (synchronized block or method exit) of a monitor happens-before every subsequent lock (synchronized block or method entry) of that same monitor. “A write to a volatile field happens- before every subsequent read of that same field. SE-2811 Dr. Yoder 24

Some happens-before relationships “A call to start on a thread happens- before any action in the started thread. “All actions in a thread happen-before any other thread successfully returns from a join on that thread.” il/concurrent/package- summary.html#MemoryVisibility SE-2811 Dr.Yoder 25

Some definitions “Two accesses to (reads of or writes to) the same variable are said to be conflicting if at least one of the accesses is a write.” (§17.4.1)§ “When a program contains two conflicting accesses that are not ordered by a happens- before relationship, it is said to contain a data race.” (§17.4.5)§ SE-2811 Dr.Yoder 26

What is a “Memory Model”? “A memory model describes, given a program and an execution trace of that program, whether the execution trace is a legal execution of the program.” /jls-17.html#jls-17.4 SE-2811 Dr.Yoder 27

Total and Partial Order Partial OrderOne Total Order SE-2811 Dr.Yoder 28

Total and Partial Order Partial OrderOne Total Order SE-2811 Dr.Yoder 29

Total and Partial Order Partial OrderAnother Total Order SE-2811 Dr.Yoder 30

Total and Partial Order Each thread has a Total Order SE-2811 Dr.Yoder 31

My conclusion If a program has no data races, then we can treat that program as if it behaved according to our simple “switch-back-and-forth- between-threads” model which we used to illustrate multithreading at the beginning of the class. SE-2811 Dr.Yoder 32

Caveat “Sequential consistency and/or freedom from data races still allows errors arising from groups of operations that need to be perceived atomically and are not.” (§17.4.3)§ SE-2811 Dr.Yoder 33

A set of operations that happen all at once; they cannot be interrupted if(theInstance == null) { theInstance = new MySingleton(); } Atomic SE-2811 Dr.Yoder 34 Example: Should be atomic

The double-locked Singleton if(theInstance == null) { synchronized(MySingleton.class) { if(theInstance == null){ theInstance = new MySingleton(); } return theInstance; SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 35

The faster double-locked Singleton MySingleton local = theInstance; if(local == null) { synchronized(MySingleton.class) { local = theInstance; if(local == null) { local = theInstance = new MySingleton(); } return local; SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 36

DO VARIABLES STORE OBJECTS? You may be wondering… SE-2811 Dr.Yoder 37

Volatile caveat “A write to a volatile field happens- before every subsequent read of that same field.” A field is an instance variable or a static variable A variable stores either a primitive type or a reference type A variable does not store an object. SE-2811 Dr. Yoder 38

What does this mean? Although writes to references are protected by volatile, writes to objects are not. If you want a volatile object, you have to declare all its fields volatile (not recommended) Even then… You can’t do this for objects in the standard library Your class may still have parts that need to be atomic but are not If you are calling a method that is not designed to be used without synchronization, you should synchronize around it. SE-2811 Dr.Yoder 39

Exercise: Determine if this implementation of the double- locked Singleton is correct. Explain. if(loggers.get(path)==null){ synchronized (loggers){ if(loggers.get(path) == null){ EventLogger n = new EventLogger(path); } SE-2811 Dr.Yoder 40

Exercise Loggers is a map. If this map is not designed to be used from multiple threads, and we run this program, it isn’t safe. Explain what might go wrong. if(loggers.get(path)==null){ synchronized (loggers){ if(loggers.get(path) == null){ EventLogger n = new EventLogger(path); } SE-2811 Dr.Yoder 41

An alternative: java.util.concurrent Lock-free multi-threaded data structures: ConcurrentHashMap Like HashMap, only “concurrent” ConcurrentSkipListMap Like TreeMap, only “concurrent” Discussed in Dean & Dean SE1011 book recommended by a student for studying data structures SE-2811 Dr. Yoder 42

The rigormeter is at 2 today SE-2811 Dr.Yoder 43

Ex. public void a() { System.out.println(“x”); System.out.println(“y”); } public void b() { System.out.println(“x”); System.out.println(“y”); } SE-2811 Dr.Yoder 44 Prove the following code is(n’t) free of data races:

Ex. public void a() { x = 5; System.out.println(“x”); } public void a() { x = 5; System.out.println(“x”); } SE-2811 Dr.Yoder 45 Prove the following code is(n’t) free of data races:

Ex. public void a() { synchronized { x = 5; System.out.println(“x”); } SE-2811 Dr.Yoder 46 Prove the following code is(n’t) free of data races:

Ex. if(theLogger==null){ synchronized (EventLogger.class){ if(theLogger == null){ theLogger = new EventLogger(path); } SE-2811 Dr.Yoder 47 Write whether this code contains any data races. Explain your answer. Assume loggers is not volatile.

Ex. if(theLogger==null){ synchronized (EventLogger.class){ if(theLogger == null){ theLogger = new EventLogger(path); } SE-2811 Dr.Yoder 48 Write whether this code contains any data races. Explain your answer. Assume loggers is volatile.

Ex. if(loggers.get(path)==null){ synchronized (loggers){ if(loggers.get(path) == null){ Logger logger = new EventLogger(path); loggers.put(path, logger); } SE-2811 Dr.Yoder 49 Write whether this code contains any data races. Explain your answer. Assume loggers is not thread safe.

Ex. if(loggers.get(path)==null){ synchronized (loggers){ if(loggers.get(path) == null){ Logger logger = new EventLogger(path); loggers.put(path, logger); } SE-2811 Dr.Yoder 50 Write whether this code contains any data races. Explain your answer. Assume loggers is thread safe.