Sharing Objects  Synchronization  Atomicity  Specifying critical sections  Memory visibility  One thread’s modification seen by the other  Visibility.

Slides:



Advertisements
Similar presentations
50.003: Elements of Software Construction Week 6 Thread Safety and Synchronization.
Advertisements

Background Concurrent access to shared data can lead to inconsistencies Maintaining data consistency among cooperating processes is critical What is wrong.
5.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 5: CPU Scheduling.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
Mutability SWE 332 Fall 2011 Paul Ammann. SWE 3322 Data Abstraction Operation Categories Creators Create objects of a data abstraction Producers Create.
1 CSCI 6900: Design, Implementation, and Verification of Concurrent Software Eileen Kraemer August 24 th, 2010 The University of Georgia.
CSC Multiprocessor Programming, Spring, 2011 Outline for Chapters 15, 16 & Appendix A, Week 3, Dr. Dale E. Parson.
Concurrency 101 Shared state. Part 1: General Concepts 2.
Designing a thread-safe class  Store all states in public static fields  Verifying thread safety is hard  Modifications to the program hard  Design.
Threading Part 3 CS221 – 4/24/09. Teacher Survey Fill out the survey in next week’s lab You will be asked to assess: – The Course – The Teacher – The.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Immutable Objects and Classes.
Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)
Assignment – no class Wednesday All: watch the Google Techtalk “Getting C++ Threads Right” by Hans Boehm at the following link in place of Wednesday’s.
The Zen of Threads When you can snatch the lock from the object, grasshopper, then you are ready to spawn the thread...
1 Sharing Objects – Ch. 3 Visibility What is the source of the issue? Volatile Dekker’s algorithm Publication and Escape Thread Confinement Immutability.
29-Jun-15 Java Concurrency. Definitions Parallel processes—two or more Threads are running simultaneously, on different cores (processors), in the same.
CS510 Concurrent Systems Class 5 Threads Cannot Be Implemented As a Library.
Week 9 Building blocks.
Concurrency in Java (Shooting yourself in the foot) n.
© 2009 Matthew J. Sottile, Timothy G. Mattson, and Craig E Rasmussen 1 Concurrency in Programming Languages Matthew J. Sottile Timothy G. Mattson Craig.
Java Programming: Advanced Topics
!!! Global Variables!!! are EVIL SSimply because you just write a school boy/gal?
50.003: Elements of Software Construction Week 8 Composing Thread-safe Objects.
The HDF Group Multi-threading in HDF5: Paths Forward Current implementation - Future directions May 30-31, 2012HDF5 Workshop at PSI 1.
Java Threads 11 Threading and Concurrent Programming in Java Introduction and Definitions D.W. Denbo Introduction and Definitions D.W. Denbo.
SPL/2010 Safety 1. SPL/2010 system designing for concurrent execution environments ● system: collection of objects and their interactions ● system properties:
Internet Software Development Controlling Threads Paul J Krause.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
Session 7 Methods Strings Constructors this Inheritance.
Java Thread and Memory Model
SPL/2010 Synchronization 1. SPL/2010 Overview ● synchronization mechanisms in modern RTEs ● concurrency issues ● places where synchronization is needed.
COMPSCI 230 S2C 2015 Software Design and Construction Synchronization (cont.) Lecture 4 of Theme C.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
Multiprocessor Cache Consistency (or, what does volatile mean?) Andrew Whitaker CSE451.
Fundamentals of Parallel Computer Architecture - Chapter 71 Chapter 7 Introduction to Shared Memory Multiprocessors Yan Solihin Copyright.
System Programming Practical Session 4: Concurrency / Safety.
ICFEM 2002, Shanghai Reasoning about Hardware and Software Memory Models Abhik Roychoudhury School of Computing National University of Singapore.
Lecture 6: Monitors & Semaphores. Monitor Contains data and procedures needed to allocate shared resources Accessible only within the monitor No way for.
Access Modifiers Control which classes use a feature Only class-level variables may be controlled by access modifiers Modifiers 1. public 2. protected.
Java Threads 11 Threading and Concurrent Programming in Java Synchronization D.W. Denbo Synchronization D.W. Denbo.
The Flyweight Pattern (Structural) ©SoftMoore ConsultingSlide 1.
More on Thread Safety CSE451 Andrew Whitaker. Review: Thread Hazards Safety hazards  “Program does the wrong thing” Liveness hazards  “Program never.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
CSC Multiprocessor Programming, Spring, 2012 Outline for Chapters 1-3 and 13 Dr. Dale E. Parson.
System Programming Practical Session 4: Concurrency / Safety.
Specifying Multithreaded Java semantics for Program Verification Abhik Roychoudhury National University of Singapore (Joint work with Tulika Mitra)
CSC Multiprocessor Programming, Spring, 2012 Outline for Chapter 4 – Composing Objects – thread-safe object-oriented composition, Dr. Dale E. Parson,
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.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
Monitor Pattern Read only Collection views Synchronized Collections views Concurrent HashMap The Vehicle Tracker Example.
Sun Proprietary/Confidential: Internal Use Only 1 Multi-Threading Primer Byron Nevins December 10, 2007.
Concurrency 2 CS 2110 – Spring 2016.
Threads Cannot Be Implemented As a Library
Lecture 8 Thread Safety.
The C++ Memory model Implementing synchronization)
Implementing synchronization
More on Thread Safety CSE451 Andrew Whitaker.
Threads and Memory Models Hal Perkins Autumn 2009
Java Concurrency 17-Jan-19.
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
Java Concurrency.
Java Concurrency.
Lecture 8 Thread Safety.
Java Concurrency 29-May-19.
Problems with Locks Andrew Whitaker CSE451.
CSE 332: Concurrency and Locks
Threads and concurrency / Safety
Presentation transcript:

Sharing Objects  Synchronization  Atomicity  Specifying critical sections  Memory visibility  One thread’s modification seen by the other  Visibility  T1: X = 5; print(X); will result in 5.  T1: X = 5; T2: print(X); may or may not print 5.  Use synchronization

Example

Problems with the example  Infinite loop  ready not visible to ReaderThread  Reordering  Not detectable from within the thread  Apparent to other threads  Insufficiently synchronized multithreaded programs  Reasoning on order of memory actions can be incorrect  Stale data  Not all-or-nothing  Unexpected exceptions  Corrupted data structures  Inaccurate computations  Infinite loops

Stale data – Example

Fixed example

Locking and visibility  Use locking for visibility other than mutual exclusion

Volatile variables  Weaker form of synchronization  Updates propagated to other threads  Ensures  Variables are not cached in registers  Not reordered with other memory operations  Ensures visibility for other variables  Not recommended however volatile boolean asleep; … while(!asleep) doSomething();

Volatile variables (contd.) volatile int x = 0; x++;  Volatile variables  Visibility  Locking  Visibility and atomicity

Publication and escape  Object available to code outside current scope  Storing a reference  Returning from a non-private method  Passing it to a method in another class  Escape  Publish when it should not have been  public static Set knownSecrets; public void initialize() { knownSecrets = new HashSet (); }

Escape of internal mutable states  Class UnsafeStates { private String[] states = …. public String[] getStates() { return states; } }  Passing to alien methods

Safe construction practices  Do not allow this to escape during construction  Start thread from a constructor  Incomplete object seen by thread  Create but not start.  Private constructor and public factory method

Thread confinement  Ad-hoc thread confinement  Implementation takes care of confinement  Fragile  Stack confinement  Reachable through local variables  See next slide for example  Maintenance harder  ThreadLocal (Language support)  ThreadLocal approx. equals Map  Port single-threaded application to multithreaded  Same caution as with global variables

Example for stack confinement

Immutability  Mutability  Stale values, losing updates, inconsistent state  Immutable  State cannot be changed after construction  Inherently thread-safe  Reasoning about state of immutable objects – trivial  Passable to untrusted code  Definition (in Java)  State cannot be modified after construction  All fields are final  Properly constructed (this does not escape)

Safe publication

Immutable objects and initialization safety  Object reference becomes visible to another thread  State of that object is not necessarily visible  Synchronization is needed  Immutable objects  Synchronization not needed (in Java)

Safe publication idioms  Reference to the object and object’s state made visible simultaneously  Safe publication using  Initializing object reference from a static initializer  Class initialization time  Storing reference to it into a volatile field (or AtomicReference)  Storing reference into a final field  Storing reference to a field that is properly guarded by a lock

Safe publication (contd.)  Effectively immutable  Not technically immutable  State will not be modified after publication  No need for synchronization  Improve performance  Publication requirements  Immutable objects published via any mechanism  Effectively immutable objects must be safely published  Mutable objects – safely published and  Guarded by a lock or  Thread-safe

Sharing objects safely  What to do with an object reference  Acquire a lock before using it?  Can state be modified?  Is it read-only object?  Policies for using and sharing objects  Thread-confined  Shared read-only  Immutable and effectively immutable  No addl. synchronization  Shared thread-safe  No addl. synchronization by user  Guarded  Accessed only with a specific lock