Sharing is the Key. Lecture 25: Parallelism. Canonical Example. Bad Interleavings. Common to have: CS 62 Fall 2016 Kim Bruce & Peter Mawhorter

Similar documents
Program Graph. Lecture 25: Parallelism & Concurrency. Performance. What does it mean?

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

CSE332: Data Abstractions Lecture 19: Mutual Exclusion and Locking

A Sophomoric Introduction to Shared-Memory Parallelism and Concurrency Lecture 4 Shared-Memory Concurrency & Mutual Exclusion

Programmazione di sistemi multicore

CSE 332: Data Structures & Parallelism Lecture 17: Shared-Memory Concurrency & Mutual Exclusion. Ruth Anderson Winter 2019

3/25/14. Lecture 25: Concurrency. + Today. n Reading. n P&C Section 6. n Objectives. n Concurrency

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

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

CSE332: Data Abstractions Lecture 22: Shared-Memory Concurrency and Mutual Exclusion. Tyler Robison Summer 2010

A Sophomoric Introduction to Shared-Memory Parallelism and Concurrency Lecture 5 Programming with Locks and Critical Sections

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

CSE 332: Data Structures & Parallelism Lecture 18: Race Conditions & Deadlock. Ruth Anderson Autumn 2017

Races. Example. A race condi-on occurs when the computa-on result depends on scheduling (how threads are interleaved)

Topics in Parallel and Distributed Computing: Introducing Concurrency in Undergraduate Courses 1,2

CSE 332: Locks and Deadlocks. Richard Anderson, Steve Seitz Winter 2014

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

CSE332: Data Abstractions Lecture 25: Deadlocks and Additional Concurrency Issues. Tyler Robison Summer 2010

CSE 374 Programming Concepts & Tools

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

Synchronization I. Jo, Heeseung

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

Introducing Shared-Memory Concurrency

CMSC 132: Object-Oriented Programming II

Synchronization in Concurrent Programming. Amit Gupta

Thread-unsafe code. Synchronized blocks

Programmazione di sistemi multicore

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Hank Levy 412 Sieg Hall

Synchronization

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

Synchronization I. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Monitors CSCI 201 Principles of Software Development

Lecture 23: Parallelism. To Use Library. Getting Good Results. CS 62 Fall 2016 Kim Bruce & Peter Mawhorter

Lecture 5: Synchronization w/locks

Synchronization Lecture 24 Fall 2018

What is a thread anyway?

Dealing with Issues for Interprocess Communication

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

Quiz on Tuesday April 13. CS 361 Concurrent programming Drexel University Fall 2004 Lecture 4. Java facts and questions. Things to try in Java

Stacks and Queues

Threads Synchronization

Lab & Assignment 1. Lecture 3: ArrayList & Standard Java Graphics. Random Number Generator. Read Lab & Assignment Before Lab Wednesday!

Lecture 23: Parallelism. Parallelism Idea. Parallel Programming in Java. CS 62 Fall 2015 Kim Bruce & Michael Bannister

Multi-threaded programming in Java

THREADS AND CONCURRENCY

Implementing Mutual Exclusion. Sarah Diesburg Operating Systems CS 3430

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

CS 455: INTRODUCTION TO DISTRIBUTED SYSTEMS [THREADS] Frequently asked questions from the previous class survey

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

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

CSCD 330 Network Programming

CMSC 132: Object-Oriented Programming II. Threads in Java

CSE332: Data Abstractions Lecture 23: Data Races and Memory Reordering Deadlock Readers/Writer Locks Condition Variables. Dan Grossman Spring 2012

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

Synchronization Lecture 23 Fall 2017

Data Races and Locks

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

1. Introduction to Concurrent Programming

Module 4 Multi threaded Programming, Event Handling. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

CSE 153 Design of Operating Systems

Pre- and post- CS protocols. CS 361 Concurrent programming Drexel University Fall 2004 Lecture 7. Other requirements for a mutual exclusion algorithm

CSCD 330 Network Programming

Problems with Concurrency. February 19, 2014

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

CS 153 Design of Operating Systems Winter 2016

Lecture 19 GUI Events

CS510 Advanced Topics in Concurrency. Jonathan Walpole

Computation Abstractions. Processes vs. Threads. So, What Is a Thread? CMSC 433 Programming Language Technologies and Paradigms Spring 2007

The New Java Technology Memory Model

Synchronization SPL/2010 SPL/20 1

Review: Easy Piece 1

CSE 332 Data Abstractions: Data Races and Memory, Reordering, Deadlock, Readers/Writer Locks, and Condition Variables (oh my!) Kate Deibel Summer 2012

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

RACE CONDITIONS AND SYNCHRONIZATION

Results of prelim 2 on Piazza

CSE 331 Software Design & Implementation

The Cache-Coherence Problem

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

CSE 331 Software Design and Implementation. Lecture 19 GUI Events

CMPSCI 187: Programming With Data Structures. Lecture 12: Implementing Stacks With Linked Lists 5 October 2011

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

CSE 153 Design of Operating Systems Fall 2018

10/17/ Gribble, Lazowska, Levy, Zahorjan 2. 10/17/ Gribble, Lazowska, Levy, Zahorjan 4

CS 31: Introduction to Computer Systems : Threads & Synchronization April 16-18, 2019

Lecture 8: September 30

Lecture 3: Java Graphics & Events

Synchronization COMPSCI 386

THREADS & CONCURRENCY

ITEC 120 4/14/11. Review. Need. Objectives. GUI basics JFrame JPanel JLabel, JButton, JTextField Layout managers. Lecture 38 GUI Interactivity

Operating Systems. Synchronization

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

COMP346 Winter Tutorial 4 Synchronization Semaphores

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

Faculty of Computers & Information Computer Science Department

CS18000: Programming I

Parallel Programming: Background Information

Parallel Programming: Background Information

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

Sharing Objects Ch. 3

CS180 Review. Recitation Week 15

Transcription:

Sharing is the Key Lecture 25: Parallelism CS 62 Fall 2016 Kim Bruce & Peter Mawhorter Some slides based on those from Dan Grossman, U. of Washington Common to have: Different threads access the same resources in an unpredictable order or even at about the same time But program correctness requires that simultaneous access be prevented using synchronization Simultaneous access is rare Makes testing difficult Must be much more disciplined when designing / implementing a concurrent program Will discuss common idioms known to work Canonical Example Bad Interleavings Interleaved changebalance(-100) calls on the same account Assume initial balance 150 Several ATM s accessing same account. See ATM2 Time Thread 1 Thread 2 int nb = b + amount; if(nb < 0) throw new ; balance = nb; Lost withdraw unhappy bank int nb = b + amount; if(nb < 0) throw new ; balance = nb;

Interleaving is the Problem Suppose: Thread T1 calls changebalance(-100) Thread T2 calls changebalance(-100) If second call starts before first finishes, we say the calls interleave Could happen even with one processor since a thread can be pre-empted at any point for time-slicing If x and y refer to different accounts, no problem You cook in your kitchen while I cook in mine But if x and y alias, possible trouble Problems with Account Get wrong answers! Try to fix by getting balance again, rather than using newbalance. Still can have interleaving, though less likely Can go negative w/ wrong interleaving! Solve with Mutual Exclusion At most one thread withdraws from account A at one time. Areas where don t want two threads executing called critical sections. Programmer needs to decide where, as compiler doesn t know intentions. Java Solution Re-entrant locks via synchronized blocks Syntax: synchronized (expression) {statements Evaluates expression to an object and tries to grab it as a lock If no other process is holding it, grabs it and executes statements. Releasing when finishes statements. If another process is holding it, waits until it is released. Net result: Only one thread at a time can execute a synchronized block w/same lock

Correct Code public class Account { private mylock = new Object( );... // return balance public int getbalance() { synchronized(mylock){ return balance; // update balance by adding amount public void changebalance(int amount) { synchronized(mylock) { int newbalance = balance + amount; display.settext("" + newbalance); balance = newbalance; Better Code public class Account {... // return balance public int getbalance() { synchronized(this){ return balance; // update balance by adding amount public void changebalance(int amount) { synchronized(this) { int newbalance = balance + amount; display.settext("" + newbalance); balance = newbalance; Best Code Reentrant Locks public class Account {... // return balance synchronized public int getbalance() { return balance; // update balance by adding amount synchronized public void changebalance(int amount) { int newbalance = balance + amount; display.settext("" + newbalance); balance = newbalance; If thread holds lock when executing code, then further method calls within block don t need to reacquire same lock. E.g., Methods m and n are both synchronized with same lock (e.g., with this), and execution of m results in calling n. Then once thread has the lock executing m, no delay in calling n.

Maze Program Responsiveness Uses stack to solve a maze. When user clicks solve maze button, spawns Thread to solve maze. What happens if send run instead of start? Non-Event-Driven Programming Event-Driven Programming Program in control. Program can ask for input at any point, with program control depending on input. But user can t interrupt program Only give input when program ready Control inverted. User takes action, program responds GUI components (buttons, mouse, etc.) have listeners associated with them that are to be notified when component generates an event. Listeners then take action to respond to event.

Event-Driven Programming in Java Example: Maze-Solver When an event occurs, it is posted to appropriate event queue. Java GUI components share an event queue. Any thread can post to the queue Only the event thread can remove event from the queue. When event removed from queue, thread executes the appropriate method of listener w/ event as parameter. Start button StartListener object Clear button ClearAndChooseListener Maze choice ClearAndChooseListener Speed slider SpeedListener Listeners Event Thread Different kinds of GUI items require different kinds of listeners: Button -- ActionListener Mouse -- MouseListener, MouseMotionListener Slider -- ChangeListener See GUI cheatsheet on documentation web page Removes events from queue Executes appropriate methods in listeners Also handles repaint events Must remain responsive! Code must complete and return quickly If not, then spawn new thread!

Why did Maze Freeze? Off to the Races Solver animation was being run by event thread Because didn t return until solved, was not available to remove events from queue. Could not respond to GUI controls Could not paint screen A race condition occurs when the computation result depends on scheduling (how threads are interleaved). Answer depends on shared state. Bugs that exist only due to concurrency No interleaved scheduling with 1 thread Typically, problem is some intermediate state that messes up a concurrent thread that sees that state Example class Stack<E> { synchronized void push(e val) { synchronized E pop() { if(isempty()) throw new StackEmptyException(); E peek() { E ans = pop(); push(ans); return ans; Sequentially Fine Correct in sequential world May need to write this way, if only have access to push, pop, & isempty methods. peek() has no overall effect on data structure reads rather than writes