Race Conditions & Synchronization

Similar documents
Race Conditions & Synchronization

Synchronization. Announcements. Concurrent Programs. Race Conditions. Race Conditions 11/9/17. Purpose of this lecture. A8 released today, Due: 11/21

Synchronization Lecture 23 Fall 2017

Results of prelim 2 on Piazza

Synchronization Lecture 24 Fall 2018

RACE CONDITIONS AND SYNCHRONIZATION

THREADS & CONCURRENCY

What is a Thread? Why Multicore? What is a Thread? But a fast computer runs hot THREADS AND CONCURRENCY. Concurrency (aka Multitasking)

CS 2112 Lecture 20 Synchronization 5 April 2012 Lecturer: Andrew Myers

THREADS & CONCURRENCY

Overview. CMSC 330: Organization of Programming Languages. Concurrency. Multiprocessors. Processes vs. Threads. Computation Abstractions

CS11 Java. Fall Lecture 7

THREADS AND CONCURRENCY

Synchronization. CS 416: Operating Systems Design, Spring 2011 Department of Computer Science Rutgers University

Prelim 1. CS 2110, 14 March 2017, 7:30 PM Total Question Name Short answer. OO Recursion Loop invariants Max Score Grader

More Synchronization; Concurrency in Java. CS 475, Spring 2018 Concurrent & Distributed Systems

Prelim 1. Solution. CS 2110, 14 March 2017, 7:30 PM Total Question Name Short answer

Monitors; Software Transactional Memory

An Introduction to Programming with Java Threads Andrew Whitaker University of Washington 9/13/2006. Thread Creation

Thread-Local. Lecture 27: Concurrency 3. Dealing with the Rest. Immutable. Whenever possible, don t share resources

Chapter 32 Multithreading and Parallel Programming

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Need for synchronization: If threads comprise parts of our software systems, then they must communicate.

Stacks and Queues

Synchronization COMPSCI 386

Monitors; Software Transactional Memory

CMSC 330: Organization of Programming Languages. Threads Classic Concurrency Problems

What's wrong with Semaphores?

Synchronization in Concurrent Programming. Amit Gupta

Week 7. Concurrent Programming: Thread Synchronization. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

COMP31212: Concurrency A Review of Java Concurrency. Giles Reger

Synchronization

OS06: Monitors in Java

The Dining Philosophers Problem CMSC 330: Organization of Programming Languages

Prelim 1. CS 2110, 14 March 2017, 5:30 PM Total Question Name Short answer. OO Recursion Loop invariants Max Score Grader

Recap. Contents. Reenterancy of synchronized. Explicit Locks: ReentrantLock. Reenterancy of synchronise (ctd) Advanced Thread programming.

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

CMSC 330: Organization of Programming Languages. The Dining Philosophers Problem

MultiThreading 07/01/2013. Session objectives. Introduction. Introduction. Advanced Java Programming Course

Advanced Java Programming Course. MultiThreading. By Võ Văn Hải Faculty of Information Technologies Industrial University of Ho Chi Minh City

Chapter 6: Process Synchronization

G Programming Languages Spring 2010 Lecture 13. Robert Grimm, New York University

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring 2004

Midterm on next week Tuesday May 4. CS 361 Concurrent programming Drexel University Fall 2004 Lecture 9

27/04/2012. We re going to build Multithreading Application. Objectives. MultiThreading. Multithreading Applications. What are Threads?

CMSC 330: Organization of Programming Languages

Lecture Topics. Announcements. Today: Concurrency (Stallings, chapter , 5.7) Next: Exam #1. Self-Study Exercise #5. Project #3 (due 9/28)

Deadlock and Monitors. CS439: Principles of Computer Systems September 24, 2018

More thread safety and Concurrent collections. Nasser Giacaman CompSci 230: Software Design and Construction

ENGR 3950U / CSCI 3020U UOIT, Fall 2012 Quiz on Process Synchronization SOLUTIONS

Summary Semaphores. Passing the Baton any await statement. Synchronisation code not linked to the data

Concurrency & Parallelism. Threads, Concurrency, and Parallelism. Multicore Processors 11/7/17

Synchronization. CS61, Lecture 18. Prof. Stephen Chong November 3, 2011

Lecture 8: September 30

Exercises and Labs. Part I. Exercises

Principles of Software Construction: Objects, Design and Concurrency. The Perils of Concurrency, Part 2 Can't live with it. Cant live without it.

Threads, Concurrency, and Parallelism

Lab. Lecture 26: Concurrency & Responsiveness. Assignment. Maze Program

Concurrency. Chapter 5

Java Threads. Introduction to Java Threads

COMP 346 Winter 2019 Programming Assignment 2

wait with priority An enhanced version of the wait operation accepts an optional priority argument:

Programming in Parallel COMP755

+ Today. Lecture 26: Concurrency 3/31/14. n Reading. n Objectives. n Announcements. n P&C Section 7. n Race conditions.

Operating Systems. Lecture 4 - Concurrency and Synchronization. Master of Computer Science PUF - Hồ Chí Minh 2016/2017

Synchronization. Disclaimer: some slides are adopted from the book authors slides with permission 1

CS 153 Design of Operating Systems Winter 2016

The Java Memory Model

CS 571 Operating Systems. Midterm Review. Angelos Stavrou, George Mason University

Process Synchronization

Java Threads. Written by John Bell for CS 342, Spring 2018

Memory system behavior: volatile variables. CS 361 Concurrent programming Drexel University Fall 2004 Lecture 6. Volatile variables and concurrency

What is a thread anyway?

CSE 153 Design of Operating Systems

Real-Time and Concurrent Programming Lecture 4 (F4): Monitors: synchronized, wait and notify

Lecture 29: Java s synchronized statement

Faculty of Computers & Information Computer Science Department

Concurrent Programming

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Steve Gribble. Synchronization. Threads cooperate in multithreaded programs

The New Java Technology Memory Model

Today: Synchronization. Recap: Synchronization

Process Synchronization

Principles of Software Construction: Objects, Design and Concurrency. The Perils of Concurrency, Part 2 (Can't live with it, can't live without it.

Page 1. CS162 Operating Systems and Systems Programming Lecture 8. Readers-Writers Language Support for Synchronization

CSE332: Data Abstractions Lecture 23: Programming with Locks and Critical Sections. Tyler Robison Summer 2010

Synchronization I. Jo, Heeseung

Synchronization II: EventBarrier, Monitor, and a Semaphore. COMPSCI210 Recitation 4th Mar 2013 Vamsi Thummala

Concurrency Control. Synchronization. Brief Preview of Scheduling. Motivating Example. Motivating Example (Cont d) Interleaved Schedules

CSE332: Data Abstractions Lecture 19: Mutual Exclusion and Locking

Producing Production Quality Software. Lecture 12: Concurrent and Distributed Programming Prof. Arthur P. Goldberg Fall, 2004

Handouts. 1 Handout for today! Recap. Homework #2 feedback. Last Time. What did you think? HW3a: ThreadBank. Today. Small assignment.

CMSC421: Principles of Operating Systems

Concurrent Programming Benoît Garbinato

Learning from Bad Examples. CSCI 5828: Foundations of Software Engineering Lecture 25 11/18/2014

Threads & Concurrency

CMSC 433 Programming Language Technologies and Paradigms. Spring 2013

SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY

Threads & Concurrency

CS 351 Design of Large Programs Threads and Concurrency

Atomicity CS 2110 Fall 2017

Transcription:

Gries office hours 2 A numer of students have asked for time to talk over how to study for the prelim. Gries will hold office hours at the times elow for this purpose. Also, read asiclearningmethods.pdf in the pinned Piazza note Supplementary material. Race Conditions & Synchronization Tues. (today) 1-4PM. Wed. (tomorrow) 10-noon Lecture 25 Spring 2017 How end of course works Purpose of this lecture 3 A8 due last day of classes: Wednesday, 10 May Firm deadline. Nothing after that. We grade A8 as quickly as possile, then figure out tentative course letter grades. Make them availale on the CMS. You complete a CMS assignment: Do you accept grade or take final. Taking the final can lower as well as raise your grade. Final is Sunday, 21 May, at 2PM. 4 Show you Java constructs for eliminating race conditions, allowing threads to access a data structure in a safe way ut allowing as much concurrency as possile. This requires (1) The locking of an oject so that others cannot access it, called synchronization. (2) Use of other Java methods: Wait() and NotifyAll() As an example, throughout, we use a ounded uffer. 5 Recap A race condition arises if two threads try to read and write the same data Might see the data in the middle of an update in a inconsistent stare A race condition : correctness depends on the update racing to completion without the reader managing to glimpse the in-progress update Synchronization (also known as mutual exclusion) solves this 6 Important example: ounded uffer A aker produces reads and puts them on the shelf, like a queue. Customers take them off the shelf. Threads A: produce loaves of read and put them in the queue Threads B: consume loaves y taking them off the queue This is the producer/consumer model, using a ounded uffer, the shelf (which can contain at most 20 (say) loaves of read). producer shelf consumer 1

7 Array [0..5] 5 3 6 2 4 push values 5 3 6 2 4 For later purposes, we show how to implement a ounded queue one with some maximum size in an array. A neat little implementation! We give you code for it on course wesite. 8 Array [0..5] 5 3 6 2 4 pushed values 5 3 6 2 4 Now, pop, pop, pop 10 Array implementation of a queue 9 Array [0..5] 3 5 2 4 1 Values wrap around!! push values 5 3 6 2 4 pop, pop, pop push value 1 3 5 h 3 5 2 4 1 Values wrap around!! Fields and the class invariant int[] ; // The n elements of the queue are in int n; // [h], [(h+1) %.length],... [(h+n-1) %.length] int h; // 0 <= h <.length Insert 7 (assuming there s space) [(h+n)%.length]= 5; n= n+1; Pop (assuming not empty) value= [h]; h= (h+1) %.length 11 pulic AQ<String> aq= new AQ<String>(); pulic void dosomethingto-aq() { code to change aq We need a way to prohiit other threads from using AQ aq while the code to change aq is eing executed. For this purpose, we use Java keyword synchronize 12 private AQ<String> aq= new AQ<String>(); pulic void dosomethingto-aq() { synchronize { code to change aq synchronized lock While this method is executing the synchronized lock, oject aq is locked. No other thread can otain the lock. Only one thread can own the lock at a time 2

Bounded Buffer 13 You can lock on any oject, including this pulic void dosomething() { synchronized (this) {... Why do this? Suppose method dosomething is within an oject of class ArrayQueue, and we want to prohiit other methods within ArrayQueue from eing executed. Below is syntactic sugar for the stuff elow. They mean the same thing. pulic synchronized void dosomething() {... 14 ArrayQueue aq; // ounded uffer is implemented in aq /** Constructor: empty ounded uffer of max size n*/ pulic BoundedBuffer(int n) { aq= new ArrayQueue(n); Separation of concerns: 1. How do you implement a queue in an array? 2. How do you implement a ounded uffer, which allows producers to add to it and consumers to take things from it, all in parallel? Bounded Buffer 15 ArrayQueue aq; // ounded uffer is implemented in aq We know more code will e put in method produce, and we want to e sure that no other method in this Bounded Buffer oject can do anything with this oject until method produce is finished. So stick in a synchronize keyword. 16 ArrayQueue aq; // ounded uffer is implemented in aq What happens of aq is full? We have to wait until it ecomes non-full until there is a place to put v. Someody has to uy a loaf of read efore we can put more read on the shelf. We use a while-loop to wait, and we also need to give p the lock so some other thread can uy (consume) a loaf of read. 17 ArrayQueue aq; // ounded uffer implemented in aq while (aq.isfull()) try { wait(); catch (InterruptedExecution e) { more to come wait(): put on a list of waiting threads, give up lock so another thread can have it Need a while-loop to wait. An if-statement will no work. If the wait is interrupted for some reason, just continue 18 ArrayQueue aq; // ounded uffer implemented in aq while (aq.isfull()) try { wait(); catch (InterruptedExecution e) { notifyall(); The consumer method consume is similar. Let s look at code. Another thread may e waiting ecause the uffer is empty ---no more read. Have to notify all waiting threads that it is not empty 3

Things to notice Aout wait(), wait(n), notify(), notifyall() 19 Use a while loop ecause we can t predict exactly which thread will wake up next wait() waits on the same oject that is used for synchronizing (in our example, this, which is this instance of the ounded uffer) Method notify() wakes up one waiting thread, notifyall() wakes all of them up 20 A thread that holds a lock on oject OB and is executing in its synchronized code can make (at least) these calls. 1. wait(); It is put into set 2. Another thread from set 1 gets the lock. 2. wait(n); It is put into set 2 and stays there for at least n millisecs. Another thread from set 1 gets the lock. 3. notify(); Move one possile thread from set 2 to set 1. 4. notifyall(); Move all threads from set 2 to set 1. 1. Runnale waiting to get the OB lock. 2. Waiting that called wait and are waiting to e notified Aout wait(), wait(n), notify(), notifyall() Should one use notify() or notifyall() 21 A thread that executing in its synchronized code can make (at least) these calls. 1. wait(); It is put into set 2. Another thread from set 1 gets the lock. 3. notify(); Move one possile thread from set 2 to set 1. 4. notifyall(); Move all threads from set 2 to set 1. Notify() a lot less expensive than notifyall(). Why not use it all the time? Because the wrong thread may e notified, giving deadlock 1. Runnale waiting to get the OB lock. 2. Waiting that called wait and are waiting to e notified 22 But suppose there are two kinds of read on the shelf and one still picks the head of the queue, if it s the right kind of read. Using notify() can lead to a situation in which no one can make progress. We illustrate with a project in Eclipse, which is on the course wesite. notifyall() always works; you need to write documentation if you optimize y using notify() 23 WHY use of notify() may hang. 1. Runnale: Work with a ounded uffer of length 1. 1. Consumer W gets lock, wants White read, threads finds uffer empty, and wait()s: is put in set 2. waiting to 2. Consumer R gets lock, wants Rye read, get lock. finds uffer empty, wait()s: is put in set 2. 2. Waiting: 3. Producer gets lock, puts Rye in the uffer, does notify(), gives up lock. threads 4. The notify() causes one waiting thread to e waiting to moved from set 2 to set 1. Choose W. e notified 5. No one has lock, so one Runnale thread, W, is given lock. W wants white, not rye, so wait()s: is put in set 2. 6. Producer gets lock, finds uffer full, wait()s: is put in set 2. All 3 threads are waiting in set 2. Nothing more happens. 24 Using Concurrent Collections... Java has a unch of classes to make synchronization easier. It has synchronized versions of some of the Collections classes It has an Atomic counter. 4

From spec for HashSet Using Concurrent Collections... 25 26 this implementation is not synchronized. If multiple threads access a hash set concurrently, and at least one of the threads modifies the set, it must e synchronized externally. This is typically accomplished y synchronizing on some oject that naturally encapsulates the set. If no such oject exists, the set should e "wrapped" using method Collections.synchronizedSet This is est done at creation time, to prevent accidental unsynchronized access to the set: Set s = Collections.synchronizedSet(new HashSet(...)); import java.util.concurrent.atomic.*; pulic class Counter { private static AtomicInteger counter; pulic Counter() { counter= new AtomicInteger(0); pulic static int getcount() { return counter.getandincrement(); Summary 27 Use of multiple processes and multiple threads within each process can exploit concurrency n may e real (multicore) or virtual (an illusion) Be careful when using threads: n synchronize shared memory to avoid race conditions n avoid deadlock Even with proper locking concurrent programs can have other prolems such as livelock Serious treatment of concurrency is a complex topic (covered in more detail in cs3410 and cs4410) Nice tutorial at http://docs.oracle.com/javase/tutorial/essential/concurrency/ index.html 5