Design Approaches for Concurrent Systems. CSCI 5828: Foundations of Software Engineering Lecture 08 02/09/2012

Similar documents
Favoring Isolated Mutability The Actor Model of Concurrency. CSCI 5828: Foundations of Software Engineering Lecture 24 04/11/2012

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

Grand Central Dispatch and NSOperation. CSCI 5828: Foundations of Software Engineering Lecture 28 12/03/2015

Clojure Concurrency Constructs. CSCI 5828: Foundations of Software Engineering Lecture 12 10/02/2014

Threads and Locks, Part 2. CSCI 5828: Foundations of Software Engineering Lecture 08 09/18/2014

Computer Science 210: Data Structures

Good-Enough Design. Kenneth M. Anderson University of Colorado, Boulder CSCI 5828 Lecture 11 02/17/2009

Thread Synchronization: Foundations. Properties. Safety properties. Edsger s perspective. Nothing bad happens

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

Thread Safety. Review. Today o Confinement o Threadsafe datatypes Required reading. Concurrency Wrapper Collections

1 ICS 161: Design and Analysis of Algorithms Lecture notes for January 23, Bucket Sorting

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

WHAT IS SHAREPOINT BRANDING AND UI DESIGN? COPYRIGHTED MATERIAL

Object Fundamentals, Part One. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/5448 Lecture 2 08/27/2009

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

COMP1730/COMP6730 Programming for Scientists. Testing and Debugging.

Grafting Functional Support on Top of an Imperative Language

Originality is Overrated: OO Design Principles

Remodeling Your Office A New Look for the SAS Add-In for Microsoft Office

COMP 161 Lecture Notes 16 Analyzing Search and Sort

Chapter 5: Synchronization 1

Soda Machine Laboratory

CSci Introduction to Operating Systems. Administrivia, Intro

COMP 250 Winter stacks Feb. 2, 2016

Information Coding / Computer Graphics, ISY, LiTH

Reference types in Clojure. April 2, 2014

For your convenience Apress has placed some of the front matter material after the index. Please use the Bookmarks and Contents at a Glance links to

CISC-124. Passing Parameters. A Java method cannot change the value of any of the arguments passed to its parameters.

CS125 : Introduction to Computer Science. Lecture Notes #11 Procedural Composition and Abstraction. c 2005, 2004 Jason Zych

Midterm Exam Solutions Amy Murphy 28 February 2001

Some Notes on R Event Handling

CS 106 Winter 2016 Craig S. Kaplan. Module 07 Recursion and fractals Topics. Recursion as an extension of hierarchical modelling Simple fractals

A Comparison of Unified Parallel C, Titanium and Co-Array Fortran. The purpose of this paper is to compare Unified Parallel C, Titanium and Co-

Lecture 7: Primitive Recursion is Turing Computable. Michael Beeson

Executive Summary. It is important for a Java Programmer to understand the power and limitations of concurrent programming in Java using threads.

HASH TABLES. Hash Tables Page 1

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

Administrivia. Remote Procedure Calls. Reminder about last time. Building up to today

CSCI-1200 Data Structures Fall 2009 Lecture 25 Concurrency & Asynchronous Computing

Functions that return lists

Expanding Our Horizons. CSCI 4448/5448: Object-Oriented Analysis & Design Lecture 9 09/25/2011

Introduction to Concurrent Software Systems. CSCI 5828: Foundations of Software Engineering Lecture 08 09/17/2015

Monads. Functional Programming (CS4011) Monads

Presented By Andrew Butt

Array Based Lists. Collections

The Actor Model, Part Two. CSCI 5828: Foundations of Software Engineering Lecture 18 10/23/2014

D Programming Language

CS61C : Machine Structures

Introduction to Concurrent Software Systems. CSCI 5828: Foundations of Software Engineering Lecture 12 09/29/2016

6.001 Notes: Section 17.5

Introduction. A Brief Description of Our Journey

COSC 2P95. Introduction. Week 1. Brock University. Brock University (Week 1) Introduction 1 / 18

Linked lists Tutorial 5b

Concurrent & Distributed Systems Supervision Exercises

CS 161 Computer Security

Functional Programming in Java. CSE 219 Department of Computer Science, Stony Brook University

The fringe of a binary tree are the values in left-to-right order. For example, the fringe of the following tree:

Notes to Instructors Concerning the BLITZ Projects

Extending BPEL with transitions that can loop

Basic Reliable Transport Protocols

Earthquake data in geonet.org.nz

Programming language design and analysis

ENGR 40M Project 3c: Switch debouncing

Announcements. Assignment 2 due next class in CSIL assignment boxes

C Pointers 2013 Author Riko H i

COMP 321: Introduction to Computer Systems

COMP 213. Advanced Object-oriented Programming. Lecture 23. Shared Variables and Synchronization

CSCI-1200 Data Structures Spring 2018 Lecture 14 Associative Containers (Maps), Part 1 (and Problem Solving Too)

Introduction to Test Driven Development (To be used throughout the course)

Threads and Locks. CSCI 5828: Foundations of Software Engineering Lecture 09 09/22/2015

Lecture 10 Notes Linked Lists

THE PRAGMATIC INTRO TO REACT. Clayton Anderson thebhwgroup.com WEB AND MOBILE APP DEVELOPMENT AUSTIN, TX

A simple map: Hashtable

Midterm Exam Solutions March 7, 2001 CS162 Operating Systems

CPS 310 first midterm exam, 10/6/2014

Get out, you will, of this bind If, your objects, you have confined

Building up to today. Remote Procedure Calls. Reminder about last time. Threads - impl

If you don t, it will return the same thing as == But this may not be what you want... Several different kinds of equality to consider:

14.1 Encoding for different models of computation

CSE332: Data Abstractions Lecture 19: Mutual Exclusion and Locking

Concurrent Programming in the D Programming Language. by Walter Bright Digital Mars

PAIRS AND LISTS 6. GEORGE WANG Department of Electrical Engineering and Computer Sciences University of California, Berkeley

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Summary: Issues / Open Questions:

Part 1: Understanding Windows XP Basics

Testing Driven Development. Advanced Programming

Programmazione di sistemi multicore

Mutable Data Types. Prof. Clarkson Fall A New Despair Mutability Strikes Back Return of Imperative Programming

Patterns of Parallel Programming with.net 4. Ade Miller Microsoft patterns & practices

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

Composable Shared Memory Transactions Lecture 20-2

Search Lesson Outline

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

The New Java Technology Memory Model

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

CS-537: Midterm Exam (Fall 2013) Professor McFlub

4 KEY FACTORS FOR DATA QUALITY ON A DATA LAKE (OR: HOW TO AVOID THE DATA SWAMP) JOSH HERRITZ MIOSOFT CORPORATION MIOsoft Corporation.

Persistent Data Structures and Managed References

If Statements, For Loops, Functions

Lecture 10 Notes Linked Lists

Transcription:

Design Approaches for Concurrent Systems CSCI 5828: Foundations of Software Engineering Lecture 08 02/09/2012 1

Goals Discuss material in Chapter 3 of our concurrency textbook Design Approaches for Concurrent Systems Dealing with State Shared Mutable Design Isolated Mutable Design Purely Immutable Design Persistent/Immutable Data Structures Selecting an approach 2

Dealing with State (I) In any concurrent program, you have three choices when dealing with state shared mutability isolated mutability pure immutability The book describes each approach using a simple example Imagine a room with a whiteboard and a group of people The task is to get the people to tally up the total number of years the people have been working in industry In each case, let s imagine that the whiteboard is an instance variable and each person in the room is a separate thread 3

Dealing with State (II) Shared mutability is the most familiar for programmers We have a set of objects that have methods and instance variables We have a set of threads that are accessing these objects in some fashion As a result, we have the potential for two or more threads to be accessing the same object at the same time and potentially updating the same instance variable; this can lead to interference Example Someone writes 0 on the whiteboard and then asks everyone to come up and update the total with their years of experience People jump up and form a line and update the value one at a time Analogous to a synchronized increment() method; no true concurrency 4

Dealing with State (III) Isolated mutability is less familiar but straightforward to understand For each mutable variable in a concurrent program, you design the program such that only one thread has access to it As a result, the behavior of that variable matches what we would expect in a single threaded program, even though multiple threads may be running Previous Examples The total variable in the Concurrent portfolio calculator is an example of isolated mutability The widgets in the ConsiderateWindow example from Lecture 4 are examples of isolated mutability; they only ever get updated by the GUI thread Current Example: each member texts their years of experience to one person who updates the total as the texts arrive; highly concurrent; queue does serialization 5

Dealing with State (IV) Pure immutability is even less familiar to most developers and the hardest to understand The basic idea is that you design your program such that a variable can be assigned at most one value and that value never changes It then doesn t matter if that variable is accessible to multiple threads It s value will never change on any of them. It is read only For those developers comfortable with shared mutability and isolated mutability, this seems like an impossible goal How can you write a program (at least an interesting program) that does not change the state of at least one variable?! 6

Immutable Values (I) Most programmers are familiar with immutable values String foo = Software Engineering String bar = foo; foo and bar point at a value (a string) that will never change. foo = foo + is cool! Foo now points at a new string; the previous value did not change Instead, the original string was copied and the copy was combined with the is cool! part to create a new string value (which itself will never change) You can t change the value; if you call foo.replace(), it returns a NEW string 7

Immutable Values (II) Consider extending this idea to other types of values Imagine you created a Car class and designed it such that each value is immutable; Car a = new Car( blue, 4, Chevy, Volt ); #blue, Chevy Volt (4 wheels) Car b = a.setcolor( red ); #red, Chevy Volt (4 wheels) If Car s values were immutable, then setcolor() would return a completely new instance of Car that copied all of the values from a except for blue, set the color to red, and returned the copy The value pointed at by a would still exist and that particular value would never change; likewise the value of b would never change the word above that makes developers cringe is copy 8

Immutable Values (III) Do I really want to make an entire copy of a car each time I change something about it? Maybe, but imagine an immutable linked list that is 10,000 elements long If I add a new element to the front of the list, do I really want to copy all 10,000 elements to a new list? List a = < code to create large list > List b = a.insert(0, CU Boulder ); If you don t do this efficiently, after this simple operation, you ll have two lists in memory with 20,001 elements and 20,000 of those elements will be duplicated Not good 9

Immutable Values (IV) To address this problem of inefficiency, i.e., duplication of elements as lots of copies are made of an immutable value computer scientists developed the idea of persistent data structures a fancy way of indicating that the data structure is used to represent lots of immutable values but it shares as much structure between the copies as possible If our linked list was implemented as a persistent data structure, then List a and List b would both be treated as separate lists that are immutable BUT in memory we would have allocated only 10,001 items b would point to the head of the list; a would point to the second element of the list 10

B CU Boulder UC Irvine A 11

Persistent Data Structures The book discusses how persistent tries can be used to represent numerous immutable values of various types of other data structures including trees, maps (hashtables), and lists Such data structures can hold one million elements in a tree structure that is only four levels deep; any particular element can be accessed very quickly This is important because it means that you can efficiently use a persistent trie to model common data structures but make use of the pure immutability approach to design More importantly model multiple immutable values of a common data structure over time with maximal sharing of common structure This ensures that your immutable system is as efficient as possible 12

More on Pure Immutability Note: It s not just that you have immutable values but also your variable assignments are immutable This means, you can t do this String foo = cat ; foo = dog ; Even though foo points at two different immutable objects, this would violate immutability In a pure immutable design, foo is not allowed to shift from cat to dog after it has been assigned Instead, you must use function composition to ensure that new immutable values are constructed from previous immutable values and that the result is only stored one (or not at all) 13

Example For our example of totaling years of experience, we ask the people in the room to form a chain the first person in the chain hands their years of experience to the next person in the chain all others take the years of experience given to them, adds that number to their own, and passes the total to the next person in the chain int yearsofexperience = firstperson.getexperience(); where public void getexperience() consists of return numyearsofexperience + self.nextperson().getexperience(); 14

Selecting an Approach Most problems associated with concurrency go away if you design for isolated mutability or pure immutability This aspects makes them more desirable approaches over shared mutability Shared mutability is the most difficult design approach to adopt given the high number of problems associated with it and the complexity of synchronization methods It is however the easiest to encounter since all you need to do is write a single threaded program and then add multiple threads to it (!) We have seen some examples of isolated mutability so far (with more on the way) and we will see in future chapters examples of designing for pure immutability 15

Summary We have reviewed the three design approaches available for designing concurrent software systems Shared Mutable Design Easiest to create, hardest to debug Isolated Mutable Design Easiest safe approach to understand java.util.concurrent has classes that encourage this style Purely Immutable Design The hardest to understand and implement but the safest style of all requires a new style of programming based on the use of immutable values, persistent data structures, recursive structures and functional composition 16

Coming Up Next Lecture 9: Behavior-Driven Development and Cucumber Lecture 10: Agile Project Inception 17