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.

Slides:



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

Ch 7 B.
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Process Synchronization Continued 7.2 The Critical-Section Problem.
Mutual Exclusion By Shiran Mizrahi. Critical Section class Counter { private int value = 1; //counter starts at one public Counter(int c) { //constructor.
Lecture 5 Concurrency and Process/Thread Synchronization     Mutual Exclusion         Dekker's Algorithm         Lamport's Bakery Algorithm.
Previously… Processes –Process States –Context Switching –Process Queues Threads –Thread Mappings Scheduling –FCFS –SJF –Priority scheduling –Round Robin.
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
Concurrent Programming James Adkison 02/28/2008. What is concurrency? “happens-before relation – A happens before B if A and B belong to the same process.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
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.
Parallel Processing (CS526) Spring 2012(Week 6).  A parallel algorithm is a group of partitioned tasks that work with each other to solve a large problem.
Concurrency (3) CSE 132. iClicker/WUTexter Question The following four numbers are in 8-bit 2’s complement form: Which.
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.
Race Conditions Critical Sections Deker’s Algorithm.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
Software Testing and Quality Assurance
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.
University of Pennsylvania 9/19/00CSE 3801 Concurrent Processes CSE 380 Lecture Note 4 Insup Lee.
CS444/CS544 Operating Systems Synchronization 2/21/2007 Prof. Searleman
1 Sharing Objects – Ch. 3 Visibility What is the source of the issue? Volatile Dekker’s algorithm Publication and Escape Thread Confinement Immutability.
02/17/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
29-Jun-15 Java Concurrency. Definitions Parallel processes—two or more Threads are running simultaneously, on different cores (processors), in the same.
Hardware solutions So far we have looked at software solutions for the critical section problem. –algorithms whose correctness does not rely on any other.
02/19/2007CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
1 Thread Synchronization: Too Much Milk. 2 Implementing Critical Sections in Software Hard The following example will demonstrate the difficulty of providing.
© 2009 Matthew J. Sottile, Timothy G. Mattson, and Craig E Rasmussen 1 Concurrency in Programming Languages Matthew J. Sottile Timothy G. Mattson Craig.
!!! 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.
Concurrency, Mutual Exclusion and Synchronization.
Threading and Concurrency Issues ● Creating Threads ● In Java ● Subclassing Thread ● Implementing Runnable ● Synchronization ● Immutable ● Synchronized.
1 Chapter years of CS and SE devoted to issues of composability: how to create solutions to big problems by combining (composing) solutions to smaller.
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.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Sharing Objects  Synchronization  Atomicity  Specifying critical sections  Memory visibility  One thread’s modification seen by the other  Visibility.
CY2003 Computer Systems Lecture 04 Interprocess Communication.
Java Thread and Memory Model
Multiprocessor Cache Consistency (or, what does volatile mean?) Andrew Whitaker CSE451.
1 Concurrent Processes. 2 Cooperating Processes  Operating systems allow for the creation and concurrent execution of multiple processes  concurrency.
CIS 842: Specification and Verification of Reactive Systems Lecture INTRO-Examples: Simple BIR-Lite Examples Copyright 2004, Matt Dwyer, John Hatcliff,
System Programming Practical Session 4: Concurrency / Safety.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-5 Process Synchronization Department of Computer Science and Software.
PROGRAMMING PRE- AND POSTCONDITIONS, INVARIANTS AND METHOD CONTRACTS B MODULE 2: SOFTWARE SYSTEMS 13 NOVEMBER 2013.
CSC Multiprocessor Programming, Spring, 2012 Outline for Chapters 1-3 and 13 Dr. Dale E. Parson.
System Programming Practical Session 4: Concurrency / Safety.
CSC Multiprocessor Programming, Spring, 2012 Outline for Chapter 4 – Composing Objects – thread-safe object-oriented composition, Dr. Dale E. Parson,
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.
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.
Synchronization Questions answered in this lecture: Why is synchronization necessary? What are race conditions, critical sections, and atomic operations?
Sun Proprietary/Confidential: Internal Use Only 1 Multi-Threading Primer Byron Nevins December 10, 2007.
Critical sections, locking, monitors, etc.
Lecture 8 Thread Safety.
Designing Parallel Algorithms (Synchronization)
More on Thread Safety CSE451 Andrew Whitaker.
Module 7a: Classic Synchronization
Background and Motivation
Dr. Mustafa Cem Kasapbaşı
Java Concurrency 17-Jan-19.
Monitor Giving credit where it is due:
Java Concurrency.
Java Concurrency.
Java Concurrency 29-May-19.
Problems with Locks Andrew Whitaker CSE451.
CIS 720 Lecture 5.
Threads and concurrency / Safety
Presentation transcript:

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 lecture Grad Students: read Boehm and Adve: “Foundations of the C++ concurrency model”, ACM PLDI Conference, June 2008 (if you are an ACM member the following link should work) boehm.pdf?key1= &key2= &coll=GUIDE&dl=GUIDE&CFID= &CFTOKEN= boehm.pdf?key1= &key2= &coll=GUIDE&dl=GUIDE&CFID= &CFTOKEN= Non-ACM members have to go through the WSU library web page to get it. Access the ACM Digital library. Ask for help at the library if you don’t know how.

Videostreams of Lectures Ref= Ref=5245 The first few are not there – they start with 1/26/09. You need a username and password (I have not had good luck pasting the password – type it in). Username: cpts483 Password: ask me for the password Existence of the videostreams is not a reason to skip class

Dekker’s Algorithm Properties – mutual exclusion boolean enter1 = false; boolean enter2 = false; int turn = 1; { while(true) { /* Thread 1 */ enter1 = true; while (enter2) { if (turn==2) { enter1 = false; while (turn==2) skip; enter1 = true; } /* critical section */ enter1 = false; turn = 2; /* non-critical section */ }} /* Thread 2 */ { while(true) { enter2 = true; while (enter1) { if (turn==1) { enter2 = false; while (turn==1) skip; enter2 = true; } /* critical section */ enter2 = false; turn = 1; /* non-critical section */ }}

Dekker’s Algorithm Properties – eventual entry boolean enter1 = false; boolean enter2 = false; int turn = 1; { while(true) { /* Thread 1 */ enter1 = true; while (enter2) { if (turn==2) { enter1 = false; while (turn==2) skip; enter1 = true; } /* critical section */ enter1 = false; turn = 2; /* non-critical section */ }} /* Thread 2 */ { while(true) { enter2 = true; while (enter1) { if (turn==1) { enter2 = false; while (turn==1) skip; enter2 = true; } /* critical section */ enter2 = false; turn = 1; /* non-critical section */ }}

Dekker’s Algorithm Properties – non blocking boolean enter1 = false; boolean enter2 = false; int turn = 1; { while(true) { /* Thread 1 */ enter1 = true; while (enter2) { if (turn==2) { enter1 = false; while (turn==2) skip; enter1 = true; } /* critical section */ enter1 = false; turn = 2; /* non-critical section */ }} /* Thread 2 */ { while(true) { enter2 = true; while (enter1) { if (turn==1) { enter2 = false; while (turn==1) skip; enter2 = true; } /* critical section */ enter2 = false; turn = 1; /* non-critical section */ }}

Dekker’s Algorithm Properties – no deadlock boolean enter1 = false; boolean enter2 = false; int turn = 1; { while(true) { /* Thread 1 */ enter1 = true; while (enter2) { if (turn==2) { enter1 = false; while (turn==2) skip; enter1 = true; } /* critical section */ enter1 = false; turn = 2; /* non-critical section */ }} /* Thread 2 */ { while(true) { enter2 = true; while (enter1) { if (turn==1) { enter2 = false; while (turn==1) skip; enter2 = true; } /* critical section */ enter2 = false; turn = 1; /* non-critical section */ }}

Two key points from last time Safe Construction – this reference does not escape during construction  Remember – an object is not fully constructed until its constructor returns Thread-confined objects are safe  References are only on stack; references are only in ThreadLocal objects  Ad-hoc thread-confinement – only one thread accesses an object by agreement (but no language enforcement)

Immutable objects are thread-safe Object is immutable if  It’s state cannot be modified after construction “Cannot be modified” vs “is not modified” Private object references are ok (if referenced object is not modified by the class (and not leaked)) Public object references are not ok (because no guarantee that the referenced object cannot be changed)  Its data fields are declared final: cannot be changed after construction  It’s properly constructed Volatile references + immutable objects => simple thread safety w/o locks  Beware inadvertant publishing of private values as in Fig. 3.6

Unsafe Publication public holder Holder; public void init() { holder = new Holder(42); } public class Holder { private int n; public Holder(int n) { this.n = n; } public void sanityCheck() { if (n != n) … } } Holder is properly constructed but not properly published. sanityCheck() can see two different values for holder.n!

Safe Publication Recall: publication is the action that makes an object visible outside the current scope – typically an assignment of the reference Safe publication is all about making sure that the reference and the properly initialized fields in the object that it refers to become visible at the same time Immutable objects: No special requirements. What would it take to make the Holder object above immutable?

Safe publication idioms Initializing an object reference in a static initializer  i.e., static class-level data fields Assigning to a volatile field or AtomicReference object Assigning to a final field of a properly constructed object Assigning to a field that is properly guarded by a lock

Safe Publication Summary Immutable objects can be published by any mechanism Effectively immutable objects (ones that in fact are not changed after construction though they do not meet the strict test) must be safely published – but then can be accessed without further synchronization Mutable thread-safe objects must be safely published (necessary synchronization is invoked internally by the object) Mutable thread-unsafe objects must be safely published then accessed using proper synchronization

Chapter 3 Summary Visibility of changes is the main issue Correct synchronization, proper construction and safe publication are three requirements for programs to have well-defined behavior Rules are simpler for immutable objects There are no concurrency concerns for thread- confined objects, mutable or immutable

Chapter years of CS and SE devoted to issues of composability: how to create solutions to big problems by combining (composing) solutions to smaller problems  Mechanisms – e.g. intrinsic locks; GC  Techniques – Invariants, pre- and post- conditions; confinement; delegation; etc.  Libraries – working code embodying the techniques for specific domains This chapter mainly about techniques

Invariants – the fundamental technique What property is always true of an object when it is in a correct state? For sequential programming, the class invariant gives you critical information about what each public method needs to achieve For concurrent programming, the class invariant tells you which fields are related and therefore have to be protected by a single lock

Post-conditions and Pre-conditions The post-condition of a method tells you what that method is supposed to accomplish  (A no-op will preserve the invariant but that’s not very interesting or useful!) The pre-condition tells you what (beyond the invariant) is supposed to be true for a method to successfully reach the post-condition  In sequential programming calling a method when its precondition is false is an error  In concurrent programming we can wait for the precondition to become true