Principle 1 If more than one thread accesses a given state variable and one of them might write to it then All accesses to the variable must be correctly.

Slides:



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

Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
1 CSCI 6900: Design, Implementation, and Verification of Concurrent Software Eileen Kraemer August 24 th, 2010 The University of Georgia.
Concurrency 101 Shared state. Part 1: General Concepts 2.
Replication and Consistency Chapter 6. Data-Centric Consistency Models The general organization of a logical data store, physically distributed and replicated.
Designing a thread-safe class  Store all states in public static fields  Verifying thread safety is hard  Modifications to the program hard  Design.
CSE332: Data Abstractions Lecture 23: Programming with Locks and Critical Sections Dan Grossman Spring 2010.
A Sophomoric Introduction to Shared-Memory Parallelism and Concurrency Lecture 5 Programming with Locks and Critical Sections Dan Grossman Last Updated:
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.
© Andy Wellings, 2004 Roadmap  Introduction  Concurrent Programming  Communication and Synchronization  Completing the Java Model  Overview of the.
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.
Nov 1 Deadlock Hunter Project Software Engineering Lab. Winter 07/08 Supervised by: Yonatan Kaspi Yonatan Kaspi Introduced by: Jamil Shehadeh Husam.
1 Where did the synchronized methods go? Yes, there still exists the synchronized keyword. public synchronized void foo() {} public void foo(){ aLock.lock();
16-Jun-15 Java Threads Fine grained, shared state.
Chapter 6 – Concurrent Programming Outline 6.1 Introduction 6.2Monitors 6.2.1Condition Variables 6.2.2Simple Resource Allocation with Monitors 6.2.3Monitor.
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.
1 MATERI PENDUKUNG SINKRONISASI Matakuliah: M0074/PROGRAMMING II Tahun: 2005 Versi: 1/0.
A. Frank - P. Weisberg Operating Systems Introduction to Cooperating Processes.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Topics in Software Bug Detection Instructor: Murali Krishna Ramanathan.
Week 9 Building blocks.
Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.
Advanced Inheritance Concepts. In this chapter, we will cover: Creating and using abstract classes Using dynamic method binding Creating arrays of subclass.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Principles of Parallel Programming First Edition by Calvin Lin Lawrence Snyder.
1 Thread II Slides courtesy of Dr. Nilanjan Banerjee.
50.003: Elements of Software Construction Week 8 Composing Thread-safe Objects.
Threading and Concurrency Issues ● Creating Threads ● In Java ● Subclassing Thread ● Implementing Runnable ● Synchronization ● Immutable ● Synchronized.
The HDF Group Multi-threading in HDF5: Paths Forward Current implementation - Future directions May 30-31, 2012HDF5 Workshop at PSI 1.
SPL/2010 Safety 1. SPL/2010 system designing for concurrent execution environments ● system: collection of objects and their interactions ● system properties:
111 © 2002, Cisco Systems, Inc. All rights reserved.
November 15, 2007 A Java Implementation of a Lock- Free Concurrent Priority Queue Bart Verzijlenberg.
Operating Systems CSE 411 Multi-processor Operating Systems Multi-processor Operating Systems Dec Lecture 30 Instructor: Bhuvan Urgaonkar.
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.
Synchronized and Monitors. synchronized is a Java keyword to denote a block of code which must be executed atomically (uninterrupted). It can be applied.
ICS 313: Programming Language Theory Chapter 13: Concurrency.
Sharing Objects  Synchronization  Atomicity  Specifying critical sections  Memory visibility  One thread’s modification seen by the other  Visibility.
Parallelism Can we make it faster? 25-Apr-17.
DOUBLE INSTANCE LOCKING A concurrency pattern with Lock-Free read operations Pedro Ramalhete Andreia Correia November 2013.
SPL/2010 Synchronization 1. SPL/2010 Overview ● synchronization mechanisms in modern RTEs ● concurrency issues ● places where synchronization is needed.
Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 6: Using Design Patterns.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Concurrency & Dynamic Programming.
Monitors and Blocking Synchronization Dalia Cohn Alperovich Based on “The Art of Multiprocessor Programming” by Herlihy & Shavit, chapter 8.
13-1 Chapter 13 Concurrency Topics Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads C# Threads.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
PROGRAMMING PRE- AND POSTCONDITIONS, INVARIANTS AND METHOD CONTRACTS B MODULE 2: SOFTWARE SYSTEMS 13 NOVEMBER 2013.
More on Thread Safety CSE451 Andrew Whitaker. Review: Thread Hazards Safety hazards  “Program does the wrong thing” Liveness hazards  “Program never.
CSC Multiprocessor Programming, Spring, 2012 Outline for Chapters 1-3 and 13 Dr. Dale E. Parson.
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,
Banaras Hindu University. A Course on Software Reuse by Design Patterns and Frameworks.
DBC NOTES. Design By Contract l A contract carries mutual obligations and benefits. l The client should only call a routine when the routine’s pre-condition.
Software Design 13.1 From controller to threads l Threads are lightweight processes (what’s a process?)  Threads are part of a single program, share state.
Monitor Pattern Read only Collection views Synchronized Collections views Concurrent HashMap The Vehicle Tracker Example.
Background on the need for Synchronization
More on Thread Safety CSE451 Andrew Whitaker.
Threads and Memory Models Hal Perkins Autumn 2009
Java Concurrency 17-Jan-19.
Advanced Inheritance Concepts
Java Concurrency.
Java Concurrency.
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
Parallelism Can we make it faster? 8-May-19.
Java Concurrency 29-May-19.
Presentation transcript:

Principle 1 If more than one thread accesses a given state variable and one of them might write to it then All accesses to the variable must be correctly coordinated using some synchronization mechanism If this rule is not followed then the program is broken! What is “correct” depends on the chosen mechanism

Making Individual Objects Thread-safe What is thread-safety? Invariants Encapsulation Immutability

Def. 1: Class thread-safety An class is thread-safe if it behaves correctly regardless of scheduling decisions and without any synchronization or coordination by its client code.

Invariants An invariant is a property of an object that holds except when its state is being actively modified  Sorted  Balanced  Exists a path to every element  tree.count == # of elements stored in the tree

Invariants (2) An invariant is a relationship between the values of state variables Stateless objects are always thread-safe (no synchronization is required)  What is required to be called “stateless”?

Encapsulation The idea that maintaining invariants is the job of an object’s code, not the job of its callers Thread-safe classes include any required synchronization code

Atomic/Atomicity An operation is atomic iff it is indivisible  Concurrent operations see no intermediate states  Concurrent operations affect no intermediate states Compound actions are the opposite of atomicity  Read-modify-write  Check-then-act Compound actions can be made atomic using synchronization java.util.concurrent.atomic classes

Principle 2 To preserve state consistency (i.e. invariants) update related state variables in a single atomic operation  Variables are related if they contribute to operands of a relation in an invariant:, element-of, etc. Example invariant: product(lastFactors)==lastNumber (Fig. 2.5)

Achieving atomicity using Java intrinsic locks synchronized(object) { … } The compound action … above will be atomic with respect to any other action that also uses synchronized(object) synchronized foo (…) {…} is equivalent to foo (…) synchronized(this) {…}

Correct use of intrinsic locks Every access to a mutable state variable accessed by more than one thread must be guarded by the same lock (document which one) (Principle 1) All variables that are related by an invariant must be guarded by the same lock (Principle 2)

Intrinsic lock re-entrancy If a thread already holds an intrinsic lock, can it enter another synchronized block protected by that lock?  It’s a language or library design decision  Java – yes; locks are re- entrant  Some languages – no Implementation: owner (thread) and locked count for each lock

Argument for reentrancy Allows overriding synchronized method to call overridden synchronized method: synchronized foo(…) { … super.foo(…); … } Convenient if part of the work of a synchronized method is interesting in its own right as a synchronized method synchronized foo(…) {… bar(…); …} synchronized bar(…) {…} Argue that it is better to use bar() in foo() than to repeat the code

Argument against re-entrancy If the invariant holds whenever the lock is released (which is a basic correctness criterion) and Locks are not re-entrant, then at the beginning of any synchronized block you can trust that the invariant guarded by that lock holds Re-entrancy violates encapsulation

For next time Read Chapter 3